malloc constant or not

N

niklaus

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
 
I

Ian Collins

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.
 
M

Mark McIntyre

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
 
K

Keith Thompson

Mark McIntyre said:
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.
 
M

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
 
V

void * clvrmnky()

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.
 
R

Richard Bos

void * clvrmnky() said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top