By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,745 Members | 1,873 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,745 IT Pros & Developers. It's quick & easy.

malloc constant or not

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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.