Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

any error in the code
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 )

Report this thread to moderator Post Follow-up to this message
Old Post
parag_paul@hotmail.com
04-01-08 12:58 PM


Re: any error in the code
On  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().


Report this thread to moderator Post Follow-up to this message
Old Post
Antoninus Twink
04-01-08 12:58 PM


Re: any error in the code
parag_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. */






Report this thread to moderator Post Follow-up to this message
Old Post
Martin Ambuhl
04-01-08 12:58 PM


Re: any error in the code
In 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.


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
04-01-08 12:58 PM


Re: any error in the code
On 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


Report this thread to moderator Post Follow-up to this message
Old Post
Jack.Thomson.v3@gmail.com
04-01-08 12:58 PM


Re: any error in the code
Jack.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

Report this thread to moderator Post Follow-up to this message
Old Post
Richard Heathfield
04-01-08 12:58 PM


Re: any error in the code
In 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()?


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
04-01-08 12:58 PM


Re: any error in the code
Kenny 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


Report this thread to moderator Post Follow-up to this message
Old Post
Chris Dollin
04-02-08 12:00 AM


Re: any error in the code
On  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?


Report this thread to moderator Post Follow-up to this message
Old Post
Antoninus Twink
04-02-08 12:00 AM


Re: any error in the code
Antoninus 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).



Report this thread to moderator Post Follow-up to this message
Old Post
Richard
04-02-08 12:00 AM


Sponsored Links




Last Thread Next Thread Next
Pages (4): [1] 2 3 4 »
Search this forum -> 
Post New Thread

C archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:43 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.