【问题标题】:How do I get an object at a memory location?如何在内存位置获取对象?
【发布时间】:2012-03-05 03:10:02
【问题描述】:

如果我的内存位置存储在整数中,我如何才能将对象存储在内存位置?

我需要如何使用它:

#include <stdio.h>
#include "CelGL.h"

/*
 Stride: The size of the data structure containing the per-vertex data
 Offset: Offset within the structure to find this data
 */

// Example structs that store the data
typedef struct {
    float x;
    float y;
    float z;
} Vertex;

typedef struct {
    float x;
    float y;
    float z;
} Normal;

typedef struct {
    float r;
    float g;
    float b;
} Color;

// Info for rendering
typedef struct {
    int vertex;
    int vertexStride;
    int vertexOffset;
    int normal;
    int normalStride;
    int normalOffset;
    int index;
} RenderData;


RenderData _renderData;
int _buffer; // Memory location of array with data

// sets the integer to the memory location of the data
void celBindBuffer(int *buffer)
{
    _buffer = &buffer; // Alert:Incompatible pointer to integer conversion assigning to 'int' from 'int **'
}

void celVertexAttribPointer(CelVertexAttrib attrib, int stride/*bytes*/, int offset/*bytes*/)
{
    switch (attrib) {
        case CelVertexAttribPosition:
            _renderData.vertex = _buffer;
            _renderData.vertexStride = stride;
            _renderData.vertexOffset = offset;
            break;
        case CelVertexAttribNormal:
            _renderData.normal = _buffer;
            _renderData.normalStride = stride;
            _renderData.normalOffset = offset;
        default:
            break;
    }
}

void printVertex(Vertex v)
{
    printf("Vertex[%f,%f,%f]\n", v.x, v.y, v.z);
}

void testPrint(int size/*size in bytes*/)
{
    for (int i = 0; i < size; i++) {
        // Gets the initial location of the data in which it is stored, gets the size of the struct and multiplies it by the index and then offsets to the 
        Vertex v = (Vertex)(_renderData.vertex + i * _renderData.vertexStride + _renderData.vertexOffset); // How can I do this?
        printVertex(v);
    }
}

我应该如何在该位置获取对象,为什么我会在 _buffer = &amp;buffer; 收到警告?

编辑: 变量缓冲区是一个结构数组,而不仅仅是一个值,所以我需要它的内存位置。一旦我有了那个,我怎样才能在那个位置接收对象(线路:Vertex v = (Vertex)(_renderData.vertex + i * _renderData.vertexStride + _renderData.vertexOffset);)?

编辑: 所以要获取数据的位置,我会使用 _buffer = &(*buffer); 吗?

编辑 传递给方法的信息:

typedef struct {
    Vertex position;
    Normal normal;
} VertexData;

static const VertexData mesh[] = {
    {/*v:*/{-0.000000, -1.125000, 0.000000}, /*n:*/{0.059877, -0.998169, 0.007874} },
    {/*v:*/{-0.000000, -0.986528, -0.475087}, /*n:*/{0.114078, -0.863674, -0.490890} },
    {/*v:*/{0.357184, -0.948670, -0.284845}, /*n:*/{0.427045, -0.850368, -0.307321} },
    {/*v:*/{-0.000000, -1.125000, 0.000000}, /*n:*/{0.059877, -0.998169, 0.007874} },
    {/*v:*/{0.357184, -0.948670, -0.284845}, /*n:*/{0.427045, -0.850368, -0.307321} },
    {/*v:*/{0.449795, -0.958029, 0.102663}, /*n:*/{0.477462, -0.877438, 0.045442} },
    {/*v:*/{-0.000000, -1.125000, 0.000000}, /*n:*/{0.059877, -0.998169, 0.007874} },
    {/*v:*/{0.449795, -0.958029, 0.102663}, /*n:*/{0.477462, -0.877438, 0.045442} },
        ...
};

void render() {
    celBindBuffer(mesh);
    celVertexAttribPointer(CelVertexAttribPosition, sizeof(VertexData), offsetof(VertexData, position));
    testPrint(sizeof(mesh) / sizeof(VertexData));
}

【问题讨论】:

  • 请问是什么原因让您将_bufferRenderdata.vertex 声明为int 而不是int *
  • @tia:因为我不想有一个常规指针。我需要将数据的字节位置存储在内存中,以便手动控制更改的字节数。例如,如果我有一个指向 int 数组的指针,则指针 + 1 = 指针 + 3 个字节。如果我有一个 int 来存储内存位置,那么指针 + 1 = 指针 + 1 字节。
  • 那么改用char *void * 怎么样?

标签: c arrays memory pointers struct


【解决方案1】:

指针 (int *buffer) 是一个包含内存地址的变量。在指针指向的地址处读取内存称为解引用指针。您正在做的实际上是获取指针本身的地址。所以不要这样:

_buffer = &amp;buffer;

.. 您必须取消引用指针才能读取此 buffer 指针指向的值:

_buffer = *buffer;

我建议你阅读Pointer Basics 文章,它可以帮助你稍微了解一下这些东西。

希望对您有所帮助。祝你好运!

更新:

在您的情况下,最好使用与传入的对象相同类型的指针。否则会很混乱。这是使用该指针的示例:

#include <stdio.h>

typedef struct {
    double x;
    double y;
    double z;
} Vertex; /* Just to make a simple code compile.. */
typedef Vertex Normal; /* Same story... */

typedef struct {
    Vertex position;
    Normal normal;
} VertexData;

static const VertexData mesh[] = {
    { {-0.000000, -1.125000, 0.000000}, {0.059877, -0.998169, 0.007874} },
    { {-0.000000, -0.986528, -0.475087}, {0.114078, -0.863674, -0.490890} },
    { {0.357184, -0.948670, -0.284845}, {0.427045, -0.850368, -0.307321} },
    { {-0.000000, -1.125000, 0.000000}, {0.059877, -0.998169, 0.007874} },
    { {0.357184, -0.948670, -0.284845}, {0.427045, -0.850368, -0.307321} },
    { {0.449795, -0.958029, 0.102663}, {0.477462, -0.877438, 0.045442} },
    { {-0.000000, -1.125000, 0.000000}, {0.059877, -0.998169, 0.007874} },
    { {0.449795, -0.958029, 0.102663}, {0.477462, -0.877438, 0.045442} }
};

void celBindBuffer(const VertexData *data)
{
    const Vertex *v0 = &data[0].position;
    const Normal *n0 = &data[0].normal;

    const Vertex *v1 = &data[1].position;
    const Normal *n1 = &data[1].normal;

    printf("Vertex#1: %f/%f/%f...\n", v0->x, v0->y, v0->z);
    printf("Vertex#2: %f/%f/%f...\n", v1->x, v1->y, v1->z);
}

int main(void)
{
    celBindBuffer(mesh);
    return 0;
}

如您所见,一点点 pointer arithmetics 就可以完成这项工作。 但是,您的函数存在一些问题 - 您尝试做的是传递一个 VertexData 对象数组。但问题是函数celBindBuffer() 不知道该数组中有多少元素......您必须将一个数字传递给该函数以告知向量中有多少元素。否则你很容易遇到undefined behavior

【讨论】:

  • 变量缓冲区是一个结构数组,而不仅仅是一个值,所以我需要它的内存位置。一旦我有了它,我怎样才能在那个位置接收对象(Line:Vertex v = (Vertex)(_renderData.vertex + i * _renderData.vertexStride + _renderData.vertexOffset);)?
  • 所以要获取数据的位置,我会使用 _buffer = &(*buffer);?
  • 斯塔斯,如果不查看传递给 celBindBuffer 函数的确切内容,很难回答您的问题。但问题是 buffer 变量是数据的位置。要得到它,你什么都不用做。如果您想从那里读取值 - 只要它是 POD 类型,您就可以取消引用它,甚至可以使用指针算术移动 buffer 指向的多个元素。否则,您可以使用memcpy 复制内存区域。我想...您必须进一步澄清您的问题才能获得更好的答案...
  • 我添加了正在传递给方法的信息
  • @stas:刚刚更新了我的答案。希望能帮助到你。我真的认为你值得花几个小时阅读一些关于 C 中指针和指针算法的教程。它会给你带来很多回报。现在要睡觉了..这里快星期一了,我明天要早点上班:)祝你好运!
【解决方案2】:

线

_buffer = &buffer;

正在尝试将变量buffer(恰好是一个指针)的地址分配给整数变量_buffer。换句话说,它将“指向指针的指针”类型分配给一个整数变量。 (&amp; 运算符的意思是“地址”。)你可能想要

_buffer = *buffer;

它将buffer(将是一个整数)中地址的内容分配给整数_buffer* 运算符表示“此地址的内容”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 2019-08-07
    • 2021-03-15
    • 1970-01-01
    • 2019-11-12
    • 2013-07-14
    • 2018-06-26
    相关资源
    最近更新 更多