简体   繁体   中英

Attempt to dereference a generic pointer.

I am trying to work with directly with some low level memory stuff. I am filling a buffer with some data and then copying it to a larger buffer.

I wrote this function to help with some float data:

void copy_float_buffer(void* dest, size_t dest_index, void* src,
                       size_t src_index, size_t num_to_copy) {
    memcpy(&dest[dest_index], &src[src_index], num_to_copy * sizeof(float));
}

I currently have two different buffers and two of each type.

float buffa[12]; // i have two of these
float buffb[12]; // and two of these as well

Then have two larger buffers that will hold the those buffers back to back. float buffera[24]; //holds 2 buffa float bufferb[24] //holds 2 buffb

I have simple for loop that does some calculate and fills buffa and buffb then I use the copy_float_buffers command to fill in the buffers. I do it like this.

typedef struct {
    float buffa[12];
    float buffb[12];
} buffs;
buffs* b;
for (int j = 0; j < 2; j++) {
    b = calloc(1, sizeof(buffs));
}

for (int i = 0; i < 12; i++) {
    b->buffa[i] = vmathRandRange(21);
    b->buffb[i] = vmathRandRange(21);
}
for (int i = 0; i < 12; i++) {
    /* printf("index:%d value:%f \n", i, b->buffa[i]); */
    /* b->buffa[i] = vmathRandRange(21); */
    copy_float_buffer(v_buff, (sizeof(float) * i) * 12, b->buffa,
                      (sizeof(float) * i) * 12, 12);
    copy_float_buffer(v_buff, (sizeof(float) * i) * 12, b->buffb,
                      (sizeof(float) * i) * 12, 12);
}
print_buffer(b->buffa, "original buffer");
print_buffer(v_buff, "vertex buffer");
print_buffer(b->buffb, "original buffer");
print_buffer2(v_buff, "vertex buffer");

the print_buffers function just loops for 0-11 or 12-22 and prints out the values.

I feel like this is a pointer math issue and I was using gdb to try and look at the code and see how it's being copied over but I get that error above.

Attempt to dereference a generic pointer. 

how do I dereference pointers when stepping through code in gdb and also. I think that function to do the memory copy is setup correctly. But it seems to be only overwriting the first 12 float or first 48 bytes and not actually shifting, something like a ring buffer.

As already mentioned in comments, you can't do dest[dest_index] simply because dest is a void*

Your copy function seems strange. In the main-code you spend a lot of effort calculating byte offsets but still you use sizeof(float) inside the copy_float_buffer function. You should either write the function to use floats everywhere or bytes everywhere - that will be much easier to understand.

Since the function is called copy_float_buffer it indicates that you want to copy floats, so it should be:

void copy_float_buffer(float* dest, size_t dest_index, float* src,
                       size_t src_index, size_t num_to_copy) {
    memcpy(&dest[dest_index], &src[src_index], num_to_copy * sizeof(float));
}

to copy num_to_copy floats. If you do like this, you need to change the way you call the function, ie get rid of all the byte-offset calculation. Something like:

    copy_float_buffer(v_buff, 0, b->buffa, 0, 12);  // Ends in v_buff[0..11]
                              ^            ^   ^
                              |            |   Copy 12 floats
                              |            Start offset in buffa
                              Start offset in v_buff

    copy_float_buffer(v_buff, 12, b->buffb, 0, 12); // Ends in v_buff[12..23]
                              ^             ^   ^
                              |             |   Copy 12 floats
                              |             Start offset in buffb
                              Start offset in v_buff

The rest of your code have some issues as well:

// You loop twice but simply overwrite `b` the second time
// so you leak memory
for (int j = 0; j < 2; j++) {
    b = calloc(1, sizeof(buffs));
}

// Why do you loop 12 times?
// Do you want to copy the data in to the main buffer 12 times?
for (int i = 0; i < 12; i++) {
    // The two call of copy_float_buffer is identical except for buffa and buffb
    // So buffb will overwrite buffa
    // Probably not what you want or ...?
    copy_float_buffer(v_buff, (sizeof(float) * i) * 12, b->buffa,
                      (sizeof(float) * i) * 12, 12);
    copy_float_buffer(v_buff, (sizeof(float) * i) * 12, b->buffb,
                      (sizeof(float) * i) * 12, 12);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM