470,826 Members | 2,021 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,826 developers. It's quick & easy.

malloc constant or not

Hi,
I have seen that the following code compiles in some environments
like devc++ but fails on some env's like gcc on linux.
Can someone tell if "int *au=malloc(sizeof(int)*10);" is a constant
expression and can be used in global namespace/file scope.

Which part of the standard says or describes this .

#include<stdio.h>
#include<stdlib.h>
int *au=malloc(sizeof(int)*10);

int main()
{
if(au==NULL)
printf("memory not allocated\n");
else
printf("memory allocated\n");
return 0;
}

Thank you,
Nik

Apr 22 '06 #1
6 2494
ni*****@gmail.com wrote:
Hi,
I have seen that the following code compiles in some environments
like devc++ but fails on some env's like gcc on linux.
Can someone tell if "int *au=malloc(sizeof(int)*10);" is a constant
expression and can be used in global namespace/file scope.

Which part of the standard says or describes this .

#include<stdio.h>
#include<stdlib.h>
int *au=malloc(sizeof(int)*10);

int main()
{
if(au==NULL)
printf("memory not allocated\n");
else
printf("memory allocated\n");
return 0;
}

This won't compile with a C compiler, but will (with the appropriate
cast) compile as C++.

As it stands, it shouldn't compiler with either.

--
Ian Collins.
Apr 22 '06 #2
On 21 Apr 2006 22:48:11 -0700, in comp.lang.c , ni*****@gmail.com
wrote:
Hi,
I have seen that the following code compiles in some environments
like devc++ but fails on some env's like gcc on linux.
Can someone tell if "int *au=malloc(sizeof(int)*10);" is a constant
expression and can be used in global namespace/file scope.

Which part of the standard says or describes this .

#include<stdio.h>
#include<stdlib.h>
int *au=malloc(sizeof(int)*10);


In C, you can't have runtime-evaluated statements outside a function.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Apr 22 '06 #3
Mark McIntyre <ma**********@spamcop.net> writes:
On 21 Apr 2006 22:48:11 -0700, in comp.lang.c , ni*****@gmail.com
wrote:
I have seen that the following code compiles in some environments
like devc++ but fails on some env's like gcc on linux.
Can someone tell if "int *au=malloc(sizeof(int)*10);" is a constant
expression and can be used in global namespace/file scope.

Which part of the standard says or describes this .

#include<stdio.h>
#include<stdlib.h>
int *au=malloc(sizeof(int)*10);


In C, you can't have runtime-evaluated statements outside a function.
Mark McIntyre


Right, but "int *au=malloc(sizeof(int)*10);" is a declaration, not a
statement.

Any object declared outside a function has static storage duration.
The constraint you're running into here is C99 6.7.8p4:

All the expressions in an initializer for an object that has
static storage duration shall be constant expressions or string
literals.

A function call (malloc(), in this case) cannot be a constant
expression.

Keep in mind that "constant" and "const" are very different things in
C. "const" is a type qualifier that specifies that a declared object
is read-only, though its initial value can be determined dynamically
at run time. A "constant" is a literal, such as 42; a "constant
expression" is (more or less) an expression made up of constants,
whose value can be determined at compile time. The use of such
similar terms is unfortunate.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 22 '06 #4
On Sat, 22 Apr 2006 20:57:54 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
Mark McIntyre <ma**********@spamcop.net> writes:
On 21 Apr 2006 22:48:11 -0700, in comp.lang.c , ni*****@gmail.com

In C, you can't have runtime-evaluated statements outside a function.
Mark McIntyre


All the expressions in an initializer for an object that has
static storage duration shall be constant expressions or string
literals.


Right, but it adds up to the same thing. The constant could have been
evaluated at compile time.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Apr 22 '06 #5
ni*****@gmail.com wrote:
Hi,
I have seen that the following code compiles in some environments
like devc++ but fails on some env's like gcc on linux.
Can someone tell if "int *au=malloc(sizeof(int)*10);" is a constant
expression and can be used in global namespace/file scope.

Which part of the standard says or describes this .

#include<stdio.h>
#include<stdlib.h>
int *au=malloc(sizeof(int)*10);

int main()
{
if(au==NULL)
printf("memory not allocated\n");
else
printf("memory allocated\n");
return 0;
}

I've always thought that an initializer has to be constant. An int is
defined as something like "at least a 16-bit signed integer; at least as
large as /short/. On top of that, malloc(size) returns an object of at
least /size/ or NULL. This is not constant either.

So my understanding is that a compiler that expects this to be a
constant is doing the wrong thing.
Apr 24 '06 #6
"void * clvrmnky()" <cl**************@hotmail.com.invalid> wrote:
ni*****@gmail.com wrote:
I have seen that the following code compiles in some environments
like devc++ but fails on some env's like gcc on linux.
Can someone tell if "int *au=malloc(sizeof(int)*10);" is a constant
expression and can be used in global namespace/file scope.

Which part of the standard says or describes this .

#include<stdio.h>
#include<stdlib.h>
int *au=malloc(sizeof(int)*10);

int main()
{
if(au==NULL)
printf("memory not allocated\n");
else
printf("memory allocated\n");
return 0;
}
I've always thought that an initializer has to be constant.


For a static object, yes. An automatic object wouldn't have this
problem.
An int is defined as something like "at least a 16-bit signed integer;
Yes, but that's not the problem. For each compile of the program, the
size of an int is a fixed number, known at compile-time.
On top of that, malloc(size) returns an object of at
least /size/ or NULL. This is not constant either.
Yes, but that, /per se/, is not the problem either. If the initialised
consisted of a function that always returned a known object, it would
still not be a valid compile-time constant. The problem is that malloc()
is a function, which is not a constant expression.
So my understanding is that a compiler that expects this to be a
constant is doing the wrong thing.


Not entirely. It may accept it as a non-portable extension.

Richard
Apr 25 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by john smith | last post: by
59 posts views Thread by Steve Zimmerman | last post: by
50 posts views Thread by Joseph Casey | last post: by
40 posts views Thread by madireddy | last post: by
41 posts views Thread by jacob navia | last post: by
46 posts views Thread by lovecreatesbea... | last post: by
71 posts views Thread by desktop | last post: by
173 posts views Thread by Marty James | last post: by
25 posts views Thread by aarklon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.