我知道C++中的空类大小为1个字节.但是,我注意到sizeof()为此类的对象返回0(以g ++为单位):
class Boom{
int n[0];
}
Run Code Online (Sandbox Code Playgroud)
我可以为Boom对象打印有效的内存位置:
Boom b;
cout<<&b;
Run Code Online (Sandbox Code Playgroud)
这个特定的内存位置是否占用?如果我稍后在程序中分配内存,是否有可能使用此位置?
C中的char [] s和char*s有什么区别?我知道两者都创建了make's'指向字符数组的指针.然而,
char s[] = "hello";
s[3] = 'a';
printf("\n%s\n", s);
Run Code Online (Sandbox Code Playgroud)
打印helao,而
char * s = "hello";
s[3] = 'a';
printf("\n%s\n", s);
Run Code Online (Sandbox Code Playgroud)
给我一个分段错误.为什么会有这样的差异?我在Ubuntu 12.04上使用gcc.
我有一个不寻常的情况.这是片段:
int i, j;
short ** s = (short **)malloc(128);
for(i = 0; i < 14; i++){
s[i] = (short *)malloc(128);
for(j = 0; j < 128; j++)
s[i][j] = 0;
}
printf("Value of s[%d][%d] = %d\n",2,40,s[2][40]);
s[1][108] = 99;
printf("Value of s[%d][%d] = %d\n",2,40,s[2][40]);
Run Code Online (Sandbox Code Playgroud)
我运行时获得的输出是:Value of s[2][40] = 0
Value of s[2][40] = 99
消除循环并写短s [14] [128]产生正确的输出(两个打印中s [2] [40]的值为0)
为什么我能用s [1] [108]访问s [2] [40]?我在Ubuntu 12.04上使用gcc.