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

Question about "define" and "malloc"

P: n/a
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.
Aug 17 '06 #1
Share this Question
Share on Google+
14 Replies


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

--
Nick Keighley

Aug 17 '06 #2

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

Aug 17 '06 #3

P: n/a
"Chen Shusheng" <ly*******@hotmail.tomwrites:
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.

--
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.
Aug 17 '06 #4

P: n/a
Maybe my IDE run away. I restart my IDE again and rewrite the code as
Nick Keighley suggested. It works. Thank you!

monnand wrote:
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]$
Aug 17 '06 #5

P: n/a
Chen Shusheng wrote:
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
......
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!"

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 17 '06 #6

P: n/a
you are really funny guy........

"Eric Sosman" <es*****@acm-dot-org.invalid>
??????:y7******************************@comcast.co m...
Chen Shusheng wrote:
>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
......

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!"

--
Eric Sosman
es*****@acm-dot-org.invalid

Aug 17 '06 #7

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

--
Chris "17.0" Dollin
"The path to the web becomes deeper and wider" - October Project

Aug 17 '06 #8

P: n/a
On Thu, 17 Aug 2006 15:52:00 +0800, "Chen Shusheng"
<ly*******@hotmail.tomwrote:
>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
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
Aug 18 '06 #9

P: n/a
On Thu, 17 Aug 2006 07:52:00 UTC, "Chen Shusheng"
<ly*******@hotmail.tomwrote:
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!
Aug 18 '06 #10

P: n/a
"Herbert Rosenau" <os****@pc-rosenau.dewrites:
On Thu, 17 Aug 2006 07:52:00 UTC, "Chen Shusheng"
<ly*******@hotmail.tomwrote:
>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.
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.

--
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.
Aug 18 '06 #11

P: n/a
2006-08-18 <wm***************************@JUPITER1.PC-ROSENAU.DE>,
Herbert Rosenau wrote:
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"
Aug 18 '06 #12

P: n/a

Herbert Rosenau wrote:
On Thu, 17 Aug 2006 07:52:00 UTC, "Chen Shusheng"
<ly*******@hotmail.tomwrote:
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));
I think the error is you define the point "temp" after some code you
omitted.
Maybe it's the reason of "parse error"
>
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!
Aug 25 '06 #13

P: n/a

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

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

Aug 25 '06 #14

P: n/a
Alex wrote:
Herbert Rosenau wrote:
><ly*******@hotmail.tomwrote:
>>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));

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.

--
Chuck F (cb********@yahoo.com) (cb********@maineline.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.netUSE maineline address!
Aug 25 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.