【发布时间】:2017-01-26 11:26:15
【问题描述】:
我可以对分配的内存做些什么有限制吗?(标准)
例如
#include <stdio.h>
#include <stdlib.h>
struct str{
long long a;
long b;
};
int main(void)
{
long *x = calloc(4,sizeof(long));
x[0] = 2;
x[3] = 7;
//is anything beyond here legal( if you would exclude possible illegal operations)
long long *y = x;
printf("%lld\n",y[0]);
y[0] = 2;
memset (x,0,16);
struct str *bar = x;
bar->b = 4;
printf("%lld\n",bar->a);
return 0;
}
总结一下:
- 只要大小合适,我可以将指针重新转换为其他数据类型和结构吗?
- 那么,我可以先阅读再写作吗?
- 如果不行,我写完还能看吗?
- 我可以将它与小于分配内存的结构一起使用吗?
【问题讨论】:
标签: c pointers malloc strict-aliasing