473,379 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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 2800
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: john smith | last post by:
Can someone please explain to me what is happening when I do a malloc(0). This is what I did. int* p = (int*)malloc(0); Then I printed the value of p and of course it was non-null. But...
59
by: Steve Zimmerman | last post by:
This program compiles fine, but are there any hidden dangers in it, or is it ok? Experiment 1 ################################################## #include <stdio.h> #include <stdlib.h>...
50
by: Joseph Casey | last post by:
Greetings. I have read that the mistake of calling free(some_ptr) twice on malloc(some_data) can cause program malfunction. Why is this? With thanks. Joseph Casey.
40
by: madireddy | last post by:
Hi, Inside a program how do i find out what size has been allocated to pointer using malloc. eg... int *p; p=(int*)malloc(1024*sizeof(int)); now how do find how much has been allocated to p?...
41
by: jacob navia | last post by:
In the C tutorial for lcc-win32, I have a small chapter about a debugging implementation of malloc. Here is the code, and the explanations that go with it. I would appreciate your feedback...
46
by: lovecreatesbea... | last post by:
Do you prefer malloc or calloc? p = malloc(size); Which of the following two is right to get same storage same as the above call? p = calloc(1, size); p = calloc(size, 1);
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
173
by: Marty James | last post by:
Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there's no point putting in a check on the return value of malloc....
25
by: aarklon | last post by:
Hi all, suppose i am having a structure as follows typedef struct node { int n; struct node * next; }sn;
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.