由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C array
相关主题
有谁知道怎么把matlab和VC联接着一起用? (转载)ask a simple question about int pointer.
A C++ compiler related interview question关于Makefile的一个问题
Help: Another C++ compilation error on GCC关于C++ STL编译的疑问
C++ questionquestion for C++ constant
C语言的变量都一定要放在stack上吗?a question about CAST
A tech question (转载)c++标准函数传递一问
An interesting C++ compile errorC++ 的 问题
谁来解释一下这个是compiler问题吗?Question about a C++ compilation error on Visual Studio 2005
相关话题的讨论汇总
话题: nn话题: compiler话题: int话题: char话题: array
进入Programming版参与讨论
1 (共1页)
a*******e
发帖数: 346
1
在网上下的一个c package。作者是在unix or linux下编的。
有个很奇怪的语法:
int exercise_8(void){
int nn = 255;
unsigned char block[nn],tblock[nn];
int errlocs[nn],derrlocs[nn];
数组大小难道可以用变量来定义?!
什么时候可以这样用?我用Microsoft Visual C++编译不通过
作者很厉害,应该不会错吧?
k****f
发帖数: 3794
2
最新的gcc是可以的

【在 a*******e 的大作中提到】
: 在网上下的一个c package。作者是在unix or linux下编的。
: 有个很奇怪的语法:
: int exercise_8(void){
: int nn = 255;
: unsigned char block[nn],tblock[nn];
: int errlocs[nn],derrlocs[nn];
: 数组大小难道可以用变量来定义?!
: 什么时候可以这样用?我用Microsoft Visual C++编译不通过
: 作者很厉害,应该不会错吧?

y*w
发帖数: 238
3
我很好奇作者是谁?

【在 a*******e 的大作中提到】
: 在网上下的一个c package。作者是在unix or linux下编的。
: 有个很奇怪的语法:
: int exercise_8(void){
: int nn = 255;
: unsigned char block[nn],tblock[nn];
: int errlocs[nn],derrlocs[nn];
: 数组大小难道可以用变量来定义?!
: 什么时候可以这样用?我用Microsoft Visual C++编译不通过
: 作者很厉害,应该不会错吧?

v*****x
发帖数: 8
4
First, in this piece of code, nn is actually a constant. The code is
equivalent to:
unsigned char block[255], ...
Second, as kukutf pointed out, newer compilers can handle this. For example:
void foo(const char * bar) {
int len = strlen(bar);
char buffer[len+1];
// manipulate buffer
}
is perfectly OK with gcc3.4.x and up.
When we enter a function body, the compiler will adjust the stack pointer to
allocate space for local variables. Say we are on an x86 platform, the
corresponding mac
v*****x
发帖数: 8
5
A small mistake above.
It's apparent that your version of MSVC does not support scenario 2. However
, for scenario 1, you may need to change the code to:
const int nn = 255;
such that it compiles.
z********0
发帖数: 9013
6
In this example:
void foo(const char * bar) {
int len = strlen(bar);
char buffer[len+1];
int t1,t2,t3...
...
t1=3
}
now the offset of t1,t2,t3... are not fixed. They need runtime calculation.
I believe that this is possible for a compiler.
However, for each reference of variable "t1", the compiler has to emit code
to calculate the address t1 or save the address in somewhere.
Either approach will hurt the performance significantly. And it may also
lead to some safty problems.
z********0
发帖数: 9013
7
Portability is also an issue: a library compiler by such compiler may cause
troubles when linked with other codes by other C compilers
e*****w
发帖数: 144
8
c99 允许。

【在 a*******e 的大作中提到】
: 在网上下的一个c package。作者是在unix or linux下编的。
: 有个很奇怪的语法:
: int exercise_8(void){
: int nn = 255;
: unsigned char block[nn],tblock[nn];
: int errlocs[nn],derrlocs[nn];
: 数组大小难道可以用变量来定义?!
: 什么时候可以这样用?我用Microsoft Visual C++编译不通过
: 作者很厉害,应该不会错吧?

p***o
发帖数: 1252
9
Why?

cause

【在 z********0 的大作中提到】
: Portability is also an issue: a library compiler by such compiler may cause
: troubles when linked with other codes by other C compilers

v*****x
发帖数: 8
10
Well, there is no requirement that buffer should appear before t(i) in the
compiled image :)

.

【在 z********0 的大作中提到】
: In this example:
: void foo(const char * bar) {
: int len = strlen(bar);
: char buffer[len+1];
: int t1,t2,t3...
: ...
: t1=3
: }
: now the offset of t1,t2,t3... are not fixed. They need runtime calculation.
: I believe that this is possible for a compiler.

相关主题
A tech question (转载)ask a simple question about int pointer.
An interesting C++ compile error关于Makefile的一个问题
谁来解释一下这个是compiler问题吗?关于C++ STL编译的疑问
进入Programming版参与讨论
v*****x
发帖数: 8
11
pp is right.
No matter how the library was generated, as long as the interface is correct
(such that we can link) and the stack remains unchanged after the
invocation, we'll be OK.

【在 p***o 的大作中提到】
: Why?
:
: cause

a*******e
发帖数: 346
12
谢谢大家的解答。
作者是Phil Karn of Qualcomm..
这个code是RS decoder
应该很多人都用他的程序
不过他编程方面没有那么厉害吧?我猜

【在 y*w 的大作中提到】
: 我很好奇作者是谁?
p***o
发帖数: 1252
13
他懒得关心可移植性罢了。

【在 a*******e 的大作中提到】
: 谢谢大家的解答。
: 作者是Phil Karn of Qualcomm..
: 这个code是RS decoder
: 应该很多人都用他的程序
: 不过他编程方面没有那么厉害吧?我猜

z********0
发帖数: 9013
14
嗯,不错,学到新东西了,本以为C就那样了,没想到还有新花样
和malloc相比是方便些了,不用惦记释放了,不过万一Malloc失败,还可以返回null来
告诉Programmer,还有一层Safety Net, 可要是Variable Length Array失败,比如参
数太大或者太小(负数),估计就直接Seg Fault了,程序都捕捉不到,这样的潜在BUG
在Testing的时候也未必能暴露出来
d*******d
发帖数: 2050
15
depends on compiler。
有的compiler会自己去猜着这个变量是不是compile time available,如果是的话,com
piler自己就fold in了。不是的话,compile会记下这个变量是数组,自己猜个长度。

【在 a*******e 的大作中提到】
: 在网上下的一个c package。作者是在unix or linux下编的。
: 有个很奇怪的语法:
: int exercise_8(void){
: int nn = 255;
: unsigned char block[nn],tblock[nn];
: int errlocs[nn],derrlocs[nn];
: 数组大小难道可以用变量来定义?!
: 什么时候可以这样用?我用Microsoft Visual C++编译不通过
: 作者很厉害,应该不会错吧?

P********e
发帖数: 2610
16
re
去年讨论过

【在 k****f 的大作中提到】
: 最新的gcc是可以的
O*******d
发帖数: 20343
17
如果是multiple platform, 就必须遵守标准的C. array size必须是compile time
constant. 我的工作给多个平台写驱动, 公司规定必须这样作,否则就不能在所有平
台上汇编。
1 (共1页)
进入Programming版参与讨论
相关主题
Question about a C++ compilation error on Visual Studio 2005C语言的变量都一定要放在stack上吗?
弱问mcc和mex的区别A tech question (转载)
C++ optimization questionAn interesting C++ compile error
关于 gcc 和 g++ 的问题谁来解释一下这个是compiler问题吗?
有谁知道怎么把matlab和VC联接着一起用? (转载)ask a simple question about int pointer.
A C++ compiler related interview question关于Makefile的一个问题
Help: Another C++ compilation error on GCC关于C++ STL编译的疑问
C++ questionquestion for C++ constant
相关话题的讨论汇总
话题: nn话题: compiler话题: int话题: char话题: array