Monday, 15 March 2010

segmentation fault - Segfault on memcopy, also can anyone explain C syntax in this code? -



segmentation fault - Segfault on memcopy, also can anyone explain C syntax in this code? -

i trying debug piece of code written else results in segfault sometimes, not time, during memcpy operation.

also, dearly appreciate if give me hand in translating what's going on in piece of code occurs before memcpy.

first off, have function beingness passed void pointer , pointer struct, so:

void examplefunction(void *dest, stuffstruct *buf)

the struct looks this:

typedef struct { char *stuff; unsigned int totalstuff; unsigned int stuffsize; unsigned int validstuff; } stuffstruct;

back examplefunction. within examplefunction, happening:

void *src; int numstuff; numstuff = buf->validstuff; src = (void *)(buf->stuff);

i'm confused above line. happens when char array in buf->stuff gets cast void pointer, set value of src? can't follow supposed happen step.

right after this, memcpy happens:

memcpy(dest, src, buf->bufsize*numstuff)

and that's segfault happens. i've checked dest/src beingness null, neither ever null.

additionally, in function calls examplefunction, array dest declared size of 5000, if matters. however, when printf value in buf->bufsize*numstuff in above code, value high above 5000 -- can go high 80,000 -- without segfaulting, though. is, runs fine length variable (buf->bufsize*numstuff) beingness much higher supposed length dest variable initialized with. however, maybe doesn't matter since cast void pointer?

for various reasons i'm unable utilize dbg or install ide. i'm using basic printf debugging. have ideas explore? give thanks in advance.

first of all, cast , assignment copies address of buf->stuff pointer src. there no magic there.

numstuff = buf->validstuff; src = (void *)(buf->stuff);

if dest has plenty storage 5000 bytes, , trying write beyond length, corrupting programme stack, can lead segfault either on re-create or little later. whether cast void pointer or not makes no difference @ all.

memcpy(dest, src, buf->bufsize*numstuff)

i think need figure out buf->bufsize*numstuff supposed computing, , either prepare if wrong (not intended), truncate re-create size of destination, or increment size of destination array.

c segmentation-fault memcpy void-pointers

No comments:

Post a Comment