开发者

population structure by memory address in C

i am having a problem while populating the structure members with address reference but when it is done using the member it self then its fine.

with structure memmber

memcpy(&(AVPFieldStructureObj->resource_value),data_start,actual_data_length);

With Memory Address

memcpy((&AVPFieldStructureObj+fieldOffset),data_start,actual_da开发者_JAVA百科ta_length);

where actual_data_length is the size of varibale and data_start is pointer pointing to the data buffer.

with memory its giving garbage value when i print the field and after executing the whole i am getting a segmentation fault but when debug with GDB the program exited normally.there was no segmentation fault

please suggest

Thanks in advance

Regards, Soheb


I believe fieldOffset is the offset in bytes?

If you're doing something like:

Type* pointer = somewhere;
pointer += 3;

then pointer gets moved forward by 3*sizeof(Type) bytes - so 3 Type objects forward.

So in your code, in the latter variant, you're not offsetting the address by fieldOffset bytes, but by fieldOffset*sizeof(AVPFieldStructureObj) bytes.

You can work around that by casting to the pointer to char* temporarily.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜