473,394 Members | 1,843 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,394 software developers and data experts.

Question about "define" and "malloc"

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
14 2327
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
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
"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
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
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
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
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
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
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
"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
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

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

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

Similar topics

17
by: Chen Shusheng | last post by:
Hi all, In fact, I want to let my memory run out. And see what will happen. My system is windowsXp. Memory is 256M.I think my cdes will apply more memory than I have. Codes are below: ...
36
by: James Harris | last post by:
Initial issue: read in an arbitrary-length piece of text. Perceived issue: handle variable-length data The code below is a suggestion for implementing a variable length buffer that could be used...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.