小明思考

高性能服务器端计算
posts - 70, comments - 428, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

奇怪的g++的行为

Posted on 2008-08-13 16:56 小明 阅读(3537) 评论(18)  编辑 收藏 引用 所属分类: C/C++
下面的C++代码能编译么?

#include <stdio.h>
#define NUM  getnum()

int getnum()
{
    
int x = 0;
    scanf(
"%d"&x);
    printf(
"%d\n", x);
    
return x;
}

int main()
{
        
int array[NUM];
        printf(
"array size =%d\n",sizeof(array));
        
return 0;
}

在g++中居然可以编译。
输入10,返回array size=40
输入20,返回array size=80
输入-1,返回array size =-4!!

问题:
1.这样的做法符合C++标准么?连sizeof成了运行期计算
2.这个array的空间应该分配在heap上,而不是stack上。g++做了什么手脚?


答案是:
1. C99的标准:
变长数组,不过支持大小为负数的数组就有些奇怪了
Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C89 mode and in c++. (However, GCC's implementation of variable-length arrays does not yet conform in detail to the ISO C99 standard.) These arrays are declared like any other automatic arrays, but with a length that is not a constant expression. The storage is allocated at the point of declaration and deallocated when the brace-level is exited.

2. 其实是分配在stack上面,动态的调整esp就可以了

Feedback

# re: 奇怪的g++的行为  回复  更多评论   

2008-08-13 17:51 by cexer
确实有点古怪,那个 getnum() 明明是返回运行期数值。

# re: 奇怪的g++的行为  回复  更多评论   

2008-08-13 17:51 by shaker(太子)
真实奇思妙想啊~佩服佩服

# re: 奇怪的g++的行为  回复  更多评论   

2008-08-13 17:59 by zzemu
array的空间应该分配在stack上。c99标准

# re: 奇怪的g++的行为  回复  更多评论   

2008-08-13 18:10 by 阿福
g++ -o main.S -S main.cpp
这样可以生成汇编代码

汇编我不太懂,通过其中调用的call __alloca大约猜到:数组被定义到堆上去了。

# re: 奇怪的g++的行为  回复  更多评论   

2008-08-13 18:12 by 空明流转
gcc的汇编表明,那个stack是一个有着作用域的堆上变量,至于sizeof,是直接在编译期生成了一个placehold后再运行期返回的。

# re: 奇怪的g++的行为  回复  更多评论   

2008-08-13 19:02 by re
你这是把C程序当C++编译,自然会有问题的.

# re: 奇怪的g++的行为  回复  更多评论   

2008-08-13 19:10 by 沈臻豪(foxtail)
晕死

# re: 奇怪的g++的行为  回复  更多评论   

2008-08-13 19:35 by 过客
传给main的参数都错了~~~~

# re: 奇怪的g++的行为  回复  更多评论   

2008-08-13 19:43 by DraculaW
Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C89 mode and in C++. (However, GCC's implementation of variable-length arrays does not yet conform in detail to the ISO C99 standard.) These arrays are declared like any other automatic arrays, but with a length that is not a constant expression. The storage is allocated at the point of declaration and deallocated when the brace-level is exited.

http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html

# re: 奇怪的g++的行为  回复  更多评论   

2008-08-13 22:35 by 陈梓瀚(vczh)
问题是为什么int Array[getnum()]居然可以编译过去。

# re: 奇怪的g++的行为  回复  更多评论   

2008-08-13 22:35 by 陈梓瀚(vczh)
原来是新特性啊。这是好事。

# re: 奇怪的g++的行为  回复  更多评论   

2008-08-13 23:25 by ZelluX
c99的特性而已

# re: 奇怪的g++的行为  回复  更多评论   

2008-08-13 23:52 by 踏雪赤兔
不是很正常吗?栈上的数组都是在运行时“分配”的,反正就加一个偏移嘛,看不出有什么不妥

# re: 奇怪的g++的行为[未登录]  回复  更多评论   

2008-08-14 00:06 by jarod
int num;
scanf("%d", &num);
int aaa[num];

也可以啊?哪用写个宏和函数。。。

# re: 奇怪的g++的行为  回复  更多评论   

2008-08-14 09:49 by bugs_killer
fuck 都乱套了..

# re: 奇怪的g++的行为  回复  更多评论   

2008-08-21 13:19 by Icat
应该是
%ud 而不是%d

sizeof 返回的是 unsigned int,你当作int在处理,
一个极大的数就变成负数了

# re: 奇怪的g++的行为  回复  更多评论   

2008-08-27 11:10 by abettor
@Icat
同意。
我觉得也是因为变长数组把-1转成无符号型了,但问题是,变长数组支持的长度有多大?如果没记错,原来数组大小貌似不能超过65535。

# re: 奇怪的g++的行为  回复  更多评论   

2008-09-30 12:20 by 某人
65535 那是16位的系统,32为系统是4294967295个

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理