![]() |
| | #1 |
| Registered User Join Date: Aug 2008
Posts: 15
| MALLOC function usage prob Code: typedef struct stack {
char data[SIZE_MAX]; // SIZE_MAX #defined as 100
int size;
} stack;
stack *s=(stack*)malloc(sizeof(stack)); //line# 11
Code: prg1.c:11: error: initializer element is not constant |
| nishkarsh is offline | |
| | #2 | |
| The larch Join Date: May 2006
Posts: 3,082
| I think that this is because C doesn't allow to use non-constants for initialization of global variables. The pointer that malloc returns is naturally not a compile-time constant.
__________________ I might be wrong. Quote:
| |
| anon is offline | |
| | #3 |
| Registered User Join Date: Jun 2008
Posts: 1,134
| Give us more code. I believe the above error could occur if you call malloc in the global scope (outside main and/or another function). Is that the case? The global variables are initialized in compile time, not at run time, so you cannot use malloc to intialize |
| C_ntua is offline | |
| | #4 |
| Registered User Join Date: Aug 2008
Posts: 15
| here's the code Code: ...
#define SIZE_MAX 100
typedef struct stack {
char data[SIZE_MAX];
int size;
} stack;
stack *s=(stack*)malloc(sizeof(stack));
void init_stack() {
s->size=0;
return;
}
void push(char c) {
if(s->size<SIZE_MAX) {
s->data[(s->size)++]=c;
}
else printf("\nStack Full!\n");
return;
}
char pop() {
if(s->size==0) {
printf("\nStack Empty!\n");
return('~');
}
else return s->data[--(s->size)];
}
int main(void) {
int t,i,j=0;
char expressions[100][400],ch;
printf("no. of expressions:");
scanf("%d",&t);
printf("enter the expressions");
for(i=0;i<t;i++) {
printf("expr[%d]:",i+1);
scanf("%s",expressions[i]);
}
for(i=0;i<t;i++) {
init_stack();
while(j<strlen(expressions[i])) {
ch=expressions[i][j++];
if(isalpha(ch)) {
printf("%c",ch);
}
else if(ch==')') {
ch=pop();
if(ch!='~') printf("%c",ch);
}
else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^') {
push(ch);
}
}
}
return 0;
}
|
| nishkarsh is offline | |
| | #5 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| As anon says, it's a global variable, they can not be calling functions to initialize their value. In this particular case, it looks like you only ever have one stack, so why not just make it a global variable, not a pointer - there is no benefit here to have a pointer (it just uses a bit more memory, and makes the code longer - I don't see any benefit in that - and global variables can be almost as large as you like). -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #6 |
| Registered User Join Date: Jun 2008
Posts: 1,134
| So, the correct way to dynamically allocate memory for a global variable is this: Code: ....
stack *s;
int main(void) {
...
s = (stack *)malloc(sizeof(stack));
...
}
Code: ....
stack s;
int main(void) {
...
}
|
| C_ntua is offline | |
| | #7 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| The correct way is not to cast the return of malloc when compiling as C.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #8 |
| Registered User Join Date: Jun 2008
Posts: 1,134
| |
| C_ntua is offline | |
| | #10 |
| Registered User Join Date: Aug 2008
Posts: 15
| Malloc should be used in global scope...! Yes, as matp and others said malloc should not be used in global scope. I've re-arranged your code to make it work...! Code: ...
#define SIZE_MAX 100
typedef struct stack
{
char data[SIZE_MAX];
int size;
} stack;
stack *s;
void init_stack()
{
s->size=0;
return;
}
void push(char c)
{
if(s->size<SIZE_MAX)
{
s->data[(s->size)++]=c;
}
else
printf("\nStack Full!\n");
return;
}
char pop()
{
if(s->size==0)
{
printf("\nStack Empty!\n");
return('~');
}
else
return s->data[--(s->size)];
}
int main(void)
{
int t,i,j=0;
char expressions[100][400],ch;
s = (stack *) malloc(sizeof(stack));
printf("no. of expressions:");
scanf("%d",&t);
printf("enter the expressions");
for(i=0;i<t;i++)
{
printf("expr[%d]:",i+1);
scanf("%s",expressions[i]);
}
for(i=0;i<t;i++)
{
init_stack();
while(j<strlen(expressions[i]))
{
ch=expressions[i][j++];
if(isalpha(ch))
{
printf("%c",ch);
}
else if(ch==')')
{
ch=pop();
if(ch!='~') printf("%c",ch);
}
else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^')
{
push(ch);
}
}
}
return 0;
}
|
| patil.vishwa is offline | |
| | #11 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Still, why do you need to use malloc? You are also committing two sins. First, you cast the return of malloc. Do not do this. This was explained earlier! And two, you are not freeing what you malloc.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #12 | |
| Registered User Join Date: Aug 2008
Posts: 15
| Hi Elysia, Quote:
I feel we should, since the malloc returns void pointer. I've been doing this and it works well with C...! | |
| patil.vishwa is offline | |
| | #13 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| And it will continue to do so, until the day when it suddenly stops. But: all those things in red here are links -- so when people say "It's in the FAQ", and the word FAQ is red, that means you can click on it to go to that entry in the FAQ. |
| tabstop is offline | |
| | #14 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| It's explained very well in the FAQ! It's there for a reason.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #15 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
Code: int x; void *p = &x; int *pi = p; double *pd = p; But that's not the reason you shouldn't cast malloc. The reason is that without a cast, compilers will notice the fact that you have not included the correct header-file for malloc. This can be more critical in some compilers than others - the example I usually give is a 68K compiler that returns a pointer in A0, and integer types in D0. If you miss out the prototype for malloc, then the compiler will assume that it returns an int. It is permissible by code to cast an int into a void *, so the compiler will happily use whatever is in D0 to form your pointer. Unfortunately, the D0 value may well be a sort of sane address [because quite often, there is a bit of "calculation" going on inside the malloc function, which may well cause D0 to be loaded with an integer value representing nearly the right address - this would cause the system to crash only when you try to free the block or some such - which may be many many lines from where it went wrong. And the difference between debugging the problem with a compiler error/warning, and debugging a "some memory used by malloc and free is messed up is a factor of 100 in difficulty - the compiler will point at exactly the right line, whilst free will just crash/misbehave in some rather random way (if you are really unlucky, it is not the first, second or third call to free that crashes either - but something else somewhere else completely, so you don't even know that it's free that is causing the problem - it could take someone fairly experienced days to figure that one out)]. I'm sure there are other compilers that return pointers in a different way than integer values (64-bit systems are also in this category, for example - and again, a cast may hide the problem for quite some time, so the problem only gets visible when you allocate enough memory to get over a 2GB boundary, for example). So in summary: It's a case of converting a potential bug into a compiler error/warning. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. | |
| matsp is offline | |
![]() |
| Tags |
| intialize, malloc |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Getting an error with OpenGL: collect2: ld returned 1 exit status | Lorgon Jortle | C++ Programming | 6 | 05-08-2009 08:18 PM |
| Seg Fault in Compare Function | tytelizgal | C Programming | 1 | 10-25-2008 03:06 PM |
| We Got _DEBUG Errors | Tonto | Windows Programming | 5 | 12-22-2006 05:45 PM |
| const at the end of a sub routine? | Kleid-0 | C++ Programming | 14 | 10-23-2005 06:44 PM |
| Dikumud | maxorator | C++ Programming | 1 | 10-01-2005 06:39 AM |