Stas Jaro
Stas Jaro

Reputation: 4885

How do I get an object at a memory location?

If I have the memory location of something stored in an integer how can I get the object stored at the memory location?

How I need to use this:

#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);
    }
}

How am I supposed to get the object at that location, and why am I getting a warning at _buffer = &buffer;?

EDIT: The variable buffer is an array of structs, not just a single value so I need to have the memory location of it. Once I have that, how can I receive the object at that location (Line:Vertex v = (Vertex)(_renderData.vertex + i * _renderData.vertexStride + _renderData.vertexOffset);)?

EDIT: So to get the location of the data would I use _buffer = &(*buffer);?

EDIT Information passed onto method:

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));
}

Upvotes: 0

Views: 308

Answers (2)

user405725
user405725

Reputation:

A pointer (int *buffer) is a variable that contains a memory address. Reading the memory at an address pointed by a pointer is called dereferencing a pointer. What you are doing is actually taking an address of the pointer itself. So instead of this:

_buffer = &buffer;

.. you have to dereference a pointer to read a value pointed by this buffer pointer:

_buffer = *buffer;

I recommend you to read Pointer Basics article, it can help you wrap your head around these things a little bit.

Hope it helps. Good luck!

UPDATE:

In your case, you are better off having a pointer of the same type as the object that is being passed in.. Otherwise it is quite a lot confusing. Here is an example of working with that pointer:

#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;
}

As you can see, a little bit of pointer arithmetics does the job. However, there is a bit of a problem with your function - what you are trying to do is to pass an array of VertexData objects. But the problem is that function celBindBuffer() does not know how many elements are there in that array... You have to pass a number to that function to tells how many elements are there in a vector. Otherwise you can easily run into undefined behavior.

Upvotes: 2

Adam Liss
Adam Liss

Reputation: 48290

The line

_buffer = &buffer;

is trying to assign the address of the variable buffer (which happens to be a pointer) into the integer variable _buffer. In other words, it's assigning type "pointer to pointer" into an integer variable. (The & operator means "address of.") You probably want

_buffer = *buffer;

Which assigns the contents at the address in buffer (which will be an integer) to the integer _buffer. The * operator means "contents at this address."

Upvotes: 0

Related Questions