Code Comments
Programming Forum and web based access to our favorite programming groups.int main()
{
int *p;
p = (int*)malloc(sizeof(int));
if(p == NULL)
{
Error("Could not allocate the memory\n");
Error("Quitting....\n");
exit(1);
}
Is it correct to check in gcc , ( compare with NULL )
Post Follow-up to this messageOn 1 Apr 2008 at 9:47, parag_paul@hotmail.com wrote:
> int main()
> {
> int *p;
> p = (int*)malloc(sizeof(int));
This is fine, though people round here hate this malloc idiom with a
passion.
> if(p == NULL)
> {
> Error("Could not allocate the memory\n");
> Error("Quitting....\n");
> exit(1);
> }
>
>
> Is it correct to check in gcc , ( compare with NULL )
Yes. if(!p) is also fine, and saves several characters.
Before long, someone will be along to tell you that Error() isn't a
standard function, and that 1 isn't a portable argument to exit().
Post Follow-up to this messageparag_paul@hotmail.com wrote:
> int main()
> {
> int *p;
> p = (int*)malloc(sizeof(int));
> if(p == NULL)
> {
> Error("Could not allocate the memory\n");
> Error("Quitting....\n");
> exit(1);
> }
>
>
> Is it correct to check in gcc , ( compare with NULL )
Your program is broken in oh-so-many ways. It is not correct with gcc
or with any other compiler for C. Compare it to the following:
#include <stdlib.h> /* mha: for malloc, exit, and NULL.
Your code implicitly declares
(incorrectly) malloc to return an
int if you don't have a C99
compiler. With a C99 compiler your
code is illegal rather than
erroneous */
#include <stdio.h> /* mha: for my macro to give your
'Error' identifier a meaning. */
/* mha: Since you use the completely undefined function 'Error' I have
supplied a macro for it. But be careful about identifiers beginning
with 'E' */
#define Error(s) fputs(s,stderr)
int main(void)
{
int *p;
p = malloc(sizeof *p); /* mha: Your original line 'p = (int *)
malloc(sizeof(int));' is not wrong,
but it is not best. The cast is
superfluous typing practice, and
useof sizeof(int) supposes that the
variable p will never have its type
changed. */
if (p == NULL) {
Error("Could not allocate the memory\n");
Error("Quitting....\n");
exit(EXIT_FAILURE); /* mha: your argument to exit, 1, has
no portably efined meaning. I have
replaced it with one of thr three
having a defined meaning, which are
0, EXIT_SUCCESS, and EXIT_FAILURE */
}
free(p); /* mha: always free any memory you
dynamically allocate. */
return 0; /* mha: You promised to return an int,
so do so. Without this the program
status on exit is not portably
defined unless you have a C99
compiler (gcc isn't one, even in C99
mode) */
} /* mha: added a closing brace. */
Post Follow-up to this messageIn article <65eherF2f094rU1@mid.individual.net>, Martin Ambuhl <mambuhl@earthlink.net> wrote: >parag_paul@hotmail.com wrote: > >Your program is broken in oh-so-many ways. It is not correct with gcc >or with any other compiler for C. Compare it to the following: The great Antonious Twink. Knows all. Tells all.
Post Follow-up to this messageOn Apr 1, 2:47=A0pm, "parag_p...@hotmail.com" <parag_p...@hotmail.com>
wrote:
> =A0 int main()
> =A0 {
> =A0 =A0 =A0 int *p;
> =A0 =A0 =A0 p =3D (int*)malloc(sizeof(int));
Do not type cast malloc as the C standard states.
> =A0 =A0 =A0 if(p =3D=3D NULL)
> =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 Error("Could not allocate the memory\n");
> =A0 =A0 =A0 =A0 =A0 Error("Quitting....\n");
> =A0 =A0 =A0 =A0 =A0 exit(1);
> =A0 =A0 =A0 }
>
> Is it correct to check in gcc , ( compare with NULL )
Always free the dynamically allocated memory before exitting from the
program
~Jack
--------------------------------------
http://programmingsite.googlepages.com
Post Follow-up to this messageJack.Thomson.v3@gmail.com said: > On Apr 1, 2:47 pm, "parag_p...@hotmail.com" <parag_p...@hotmail.com> > wrote: > Do not type cast malloc as the C standard states. The C Standard does not say either to cast the value returned by malloc, or not to cast that value. Casting the value returned by malloc is pointless and can hide a bug, but it is neither forbidden nor compulsory. Rather more importantly, if you *must* cast the result of malloc for some silly reason, make sure that you #include <stdlib.h>, because (in C90) the cast removes the obligation on your implementation to diagnose a symptom of failing to include that header. -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999
Post Follow-up to this messageIn article <PO2dnYb_kfjmtm_anZ2dnUVZ8tDinZ2d@bt.com>, Richard Heathfield <rjh@see.sig.invalid> wrote: >Jack.Thomson.v3@gmail.com said: > > >The C Standard does not say either to cast the value returned by malloc, or >not to cast that value. > >Casting the value returned by malloc is pointless and can hide a bug, but >it is neither forbidden nor compulsory. > >Rather more importantly, if you *must* cast the result of malloc for some >silly reason, make sure that you #include <stdlib.h>, because (in C90) the >cast removes the obligation on your implementation to diagnose a symptom >of failing to include that header. Well, then. We've got that settled. Good show, old man! Shall we move on now to how to prototype main()?
Post Follow-up to this messageKenny McCormack wrote:
> Shall we move on now to how to prototype main()?
I find the best prototype `main` is
#include <stdio.h>
int main(void)
{
printf( "#{my_application} v0.1" );
return 0;
}
It's extensible in so many different directions.
--
"Your world, Colonel, and I wish you the best of it!" /Witch Wor
ld/
Hewlett-Packard Limited registered office: Cain Road, Brackne
ll,
registered no: 690597 England Berks RG12
1HN
Post Follow-up to this messageOn 1 Apr 2008 at 13:22, Chris Dollin wrote:
> Kenny McCormack wrote:
>
>
> I find the best prototype `main` is
>
> #include <stdio.h>
>
> int main(void)
> {
> printf( "#{my_application} v0.1" );
> return 0;
> }
>
> It's extensible in so many different directions.
Interesting! I often start with
int main(void)
but then later find I need to process command-line arguments, so I
change it to
int main(int argc, char **argv).
Sometimes for quick throwaway programs, I naughtily use implicit int and
just have
main().
Does anyone else have any insights to share on this important point?
Post Follow-up to this messageAntoninus Twink <nospam@nospam.invalid> writes: > On 1 Apr 2008 at 13:22, Chris Dollin wrote: > > Interesting! I often start with > int main(void) > but then later find I need to process command-line arguments, so I > change it to > int main(int argc, char **argv). > > Sometimes for quick throwaway programs, I naughtily use implicit int and > just have > main(). > > Does anyone else have any insights to share on this important point? I think it should all be on one line to make debugging impossible and encourage people to get their code right first time like Chuck and others. It will also compile faster with no white space .... (ps I always include argc, argv in the main).
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.