Question about "define" and "malloc"

C

Chen Shusheng

CSS white here:
Simply strange, I found "define" can not work with "malloc". Together my
complier will say "parse error".
Could anyone tell me why?

-------------------------
#define MAX 10000
.......
int main(void){
.....
char * temp=(char *)malloc(MAX*sizeof(char));
.....
system("pause");
return 0;
}
when complying, wrong.
look for explanation.
 
N

Nick Keighley

Chen said:
CSS white here:
what?

Simply strange, I found "define" can not work with "malloc".

you are mistaken

Together my complier will say "parse error".
Could anyone tell me why?

in future please post a *comple* *compilable* (well as close as you
can!)
program. Don't put "...." in small programs.

This mildly edited version of your code compiles for me

#include <stdlib.h>

#define MAX 10000

int main(void)
{
char *temp = (char*)malloc (MAX * sizeof(char));

// system("pause");
return 0;
}

-------------------------
#define MAX 10000
......
int main(void){
....
char * temp=(char *)malloc(MAX*sizeof(char));

don't cast malloc. sizeof of char is 1 by definition so write the above

char * temp = malloc (MAX);

I also fooled with your layout as I prefer more whitespace than you
use.
....
system("pause");
non-standard

return 0;
}

when complying, wrong.
look for explanation.

I suspect you ommitted to include stlib.h
 
M

monnand

it works on my host very well...
could you show your source code?

here is my test code:

#include <stdlib.h>
#include <stdio.h>

#define MAX 10000

int main()
{
char *tmp;
tmp = (char *)malloc(MAX*sizeof(char));
printf("done\n");
return 0;
}

and compile it on Linux with gcc version 3.4.4

[monnand@monnand-host test]$ gcc 1.c
[monnand@monnand-host test]$ ./a.out
done
[monnand@monnand-host test]$
 
K

Keith Thompson

Chen Shusheng said:
Simply strange, I found "define" can not work with "malloc". Together my
complier will say "parse error".
Could anyone tell me why?

-------------------------
#define MAX 10000
......
int main(void){
....
char * temp=(char *)malloc(MAX*sizeof(char));
....
system("pause");
return 0;
}
when complying, wrong.
look for explanation.

We can't help you based on what you've posted.

If I delete the "....." lines, the code you posted compiles without
error. Apparently you're having problems with some other piece of
code; since you didn't show it to us, we can't guess what the problem
might be.

Post some actual code (don't re-type it, copy and paste it), and show
us the exact error message.
 
L

lyra

Maybe my IDE run away. I restart my IDE again and rewrite the code as
Nick Keighley suggested. It works. Thank you!
 
E

Eric Sosman

Chen said:
CSS white here:
Simply strange, I found "define" can not work with "malloc". Together my
complier will say "parse error".
Could anyone tell me why?

This line is not legal C syntax.
int main(void){
....

This line is not legal C syntax.
char * temp=(char *)malloc(MAX*sizeof(char));

No declaration of the malloc() function is in scope.

This line is not legal C syntax.
system("pause");

No declaration of the system() function is in scope.
return 0;
}
when complying, wrong.
look for explanation.

"Doctor, I'm sick!"

"What seems to be the trouble?"

"Never mind that, just give me a pill!"
 
C

Chen Shusheng

you are really funny guy........

Eric Sosman said:
This line is not legal C syntax.


This line is not legal C syntax.


No declaration of the malloc() function is in scope.


This line is not legal C syntax.


No declaration of the system() function is in scope.


"Doctor, I'm sick!"

"What seems to be the trouble?"

"Never mind that, just give me a pill!"
 
C

Chris Dollin

Chen Shusheng wrote:

[no context]
you are really funny guy........

[late quote deleted]

Please don't top-post in comp.lang.c.

And please take Eric's remarks seriously. If you don't post
real code, don't expect real answers.
 
B

Barry Schwarz

CSS white here:
Simply strange, I found "define" can not work with "malloc". Together my
complier will say "parse error".
Could anyone tell me why?

One common reason for this is that your real code has a semicolon in
this line. Post a compilable example that demonstrates the problem.
......
int main(void){
....
char * temp=(char *)malloc(MAX*sizeof(char));
....
system("pause");
return 0;
}
when complying, wrong.
look for explanation.


Remove del for email
 
H

Herbert Rosenau

CSS white here:
Simply strange, I found "define" can not work with "malloc". Together my
complier will say "parse error".
Could anyone tell me why?

-------------------------
#define MAX 10000
......
int main(void){
....
char * temp=(char *)malloc(MAX*sizeof(char));


Never ever cast the result of malloc. At best you'll end up in the
lands of undefined behavior, at worsest you'll suppress any diagnostic
the compiler will give you.
....
system("pause");
return 0;
}
when complying, wrong.
look for explanation.


--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
K

Keith Thompson

Herbert Rosenau said:
Never ever cast the result of malloc. At best you'll end up in the
lands of undefined behavior, at worsest you'll suppress any diagnostic
the compiler will give you.

Agreed, it's (almost) never a good idea to cast the result of malloc()
in C. (The rare exceptions have been discussed here before.)

At best, it's harmless (if the cast is to the correct type *and*
you've remembered to "#include <stdlib.h>"). At worst, yes, it can
suppress diagnostics from the compiler, but any undefined behavior
most likely would have been there with or without the cast.
 
J

Jordan Abel

2006-08-18 said:
Never ever cast the result of malloc. At best you'll end up in the
lands of undefined behavior, at worsest you'll suppress any diagnostic
the compiler will give you.

Actually, at best nothing bad will happen. Sure, there are plenty of
_possible_ problems, but if any of those happen it wouldn't be "at best"
 
A

Alex

I think the error is you define the point "temp" after some code you
omitted.
Maybe it's the reason of "parse error"
 
A

Alex

Alex said:
I think the error is you define the point "temp" after some code you
omitted.
Maybe it's the reason of "parse error"

if the define is at the beginning of the function,it will be ok
 
C

CBFalconer

Alex said:
I think the error is you define the point "temp" after some
code you omitted. Maybe it's the reason of "parse error"

If MAX is a magnitude that will not overstrain the automatic
storage (i.e. overload the stack in most implementations) temp
should be simply defined as:

char temp[MAX];

since it is in the main function, and exists for the life of the
program (assuming no recursive calls to main). Otherwise the
proper way to malloc it is:

char *temp; /* in the declarations */
...
temp = malloc(MAX * sizeof *temp);

which is proof against alteration of the type of temp, or against
insertion of code in the ... segment. It also gives the compiler
the maximum chance of complaining about failure to #include
<stdlib.h>, which is fatal, yet hidden by an unnecessary cast.
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top