C 中内存地址的总体结构
我在使用地址引用填充结构成员时遇到问题,但是当使用它自己的成员完成后就可以了。
with 结构体成员
memcpy(&(AVPFieldStructureObj->resource_value),data_start,actual_data_length);
With Memory Address
memcpy((&AVPFieldStructureObj+fieldOffset),data_start,actual_data_length);
其中,actual_data_length 是变量的大小,data_start 是指向数据缓冲区的指针。
当我打印字段并执行整个操作后,内存给出垃圾值,但当使用 GDB 调试时,程序正常退出。没有分段错误,
请提出建议,
提前致谢
, 索赫布
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_data_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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信
fieldOffset
是以字节为单位的偏移量?如果您正在执行以下操作:
则
pointer
向前移动3*sizeof(Type)
字节 - 因此向前移动 3 个Type
对象。因此,在您的代码中,在后一种变体中,您不会将地址偏移
fieldOffset
字节,而是偏移fieldOffset*sizeof(AVPFieldStructureObj)
字节。您可以通过暂时转换为
char*
指针来解决这个问题。I believe
fieldOffset
is the offset in bytes?If you're doing something like:
then
pointer
gets moved forward by3*sizeof(Type)
bytes - so 3Type
objects forward.So in your code, in the latter variant, you're not offsetting the address by
fieldOffset
bytes, but byfieldOffset*sizeof(AVPFieldStructureObj)
bytes.You can work around that by casting to the pointer to
char*
temporarily.