pointer question
-
so i got a pointer void *p; and a structure struct d{ blah....blah.. }; d st; and i use it with memcpy(p,&st,sizeof(d)); so my trouble is: after using memcpy i need to advance the pointer with sizeof(d), so i can copy another one. tryed p+=sizeof(d) but got : error C2036: 'void *' : unknown size help pls .
-
so i got a pointer void *p; and a structure struct d{ blah....blah.. }; d st; and i use it with memcpy(p,&st,sizeof(d)); so my trouble is: after using memcpy i need to advance the pointer with sizeof(d), so i can copy another one. tryed p+=sizeof(d) but got : error C2036: 'void *' : unknown size help pls .
-
If you want to use the pointer that way just declare the pointer as char* instead of void *. You could also just cast the pointer variable to char* if you really want to declare the pointer as void *.
Chipper Martin
-
so i got a pointer void *p; and a structure struct d{ blah....blah.. }; d st; and i use it with memcpy(p,&st,sizeof(d)); so my trouble is: after using memcpy i need to advance the pointer with sizeof(d), so i can copy another one. tryed p+=sizeof(d) but got : error C2036: 'void *' : unknown size help pls .
have you noticed that in your sample, p is not assigned, so memcpy() is writing nowhere (and there, will crash in a memory violation access) ? do
p = new d;
first...
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
have you noticed that in your sample, p is not assigned, so memcpy() is writing nowhere (and there, will crash in a memory violation access) ? do
p = new d;
first...
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
so i got a pointer void *p; and a structure struct d{ blah....blah.. }; d st; and i use it with memcpy(p,&st,sizeof(d)); so my trouble is: after using memcpy i need to advance the pointer with sizeof(d), so i can copy another one. tryed p+=sizeof(d) but got : error C2036: 'void *' : unknown size help pls .
Since you will probably be doing this in an array (I assume this is for DirectX vertices?) if st is an array of d's instead of single ones, just call it this way:
d st[100]; // fill in each st somewhere // p is allocated in a Vertex buffer memcpy(p, &st, sizeof(d) * 100);
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
Since you will probably be doing this in an array (I assume this is for DirectX vertices?) if st is an array of d's instead of single ones, just call it this way:
d st[100]; // fill in each st somewhere // p is allocated in a Vertex buffer memcpy(p, &st, sizeof(d) * 100);
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
I'm not sure if this is what he's after; I think he wants to fill the array with multiple copies of the same element. On a side note, I'd do what you're trying to do like this:
d st[100]; // fill in each st somewhere // p is allocated in a Vertex buffer memcpy(p, &st, sizeof(st));
This way you can change the size of the array in one place and thus it's harder to make a mistake by only changing it in one.Steve
-
so i got a pointer void *p; and a structure struct d{ blah....blah.. }; d st; and i use it with memcpy(p,&st,sizeof(d)); so my trouble is: after using memcpy i need to advance the pointer with sizeof(d), so i can copy another one. tryed p+=sizeof(d) but got : error C2036: 'void *' : unknown size help pls .
Don't use a
void
pointer, use a pointer of the correct type then use the++
operator.Steve
-
I'm not sure if this is what he's after; I think he wants to fill the array with multiple copies of the same element. On a side note, I'd do what you're trying to do like this:
d st[100]; // fill in each st somewhere // p is allocated in a Vertex buffer memcpy(p, &st, sizeof(st));
This way you can change the size of the array in one place and thus it's harder to make a mistake by only changing it in one.Steve
Stephen Hewitt wrote:
d st[100];// fill in each st somewhere// p is allocated in a Vertex buffermemcpy(p, &st, sizeof(st));
That works until you do something like this:
d* st = new d[100]; memcpy(p, st, sizeof(st)); // oops! won't work memcpy(p, st, sizeof(d) * 100); // works!
If you want to change the size of the array, it is better to do this:
const unsigned long ARRAY_SIZE = 100; d st[ARRAY_SIZE]; memcpy(p, st, sizeof(d) * ARRAY_SIZE);
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac