can you tell me why1?

This is a discussion on can you tell me why1? within the C Programming forums, part of the General Programming Boards category; char *p; char p=(char *)malloc(sizeof(char)); can you tell me the differences for above two sentences??...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    24

    Question can you tell me why1?

    char *p;
    char p=(char *)malloc(sizeof(char));
    can you tell me the differences for above two sentences??

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    The FAQ has information on dynamic memory and allocation (which is what you would want to research for more information) and there are countless posts on this board with similar questions

  3. #3
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Quote Originally Posted by pacific
    char *p;
    char p=(char *)malloc(sizeof(char));
    can you tell me the differences for above two sentences??
    You're just declaring a pointer and allocating space for it. And you shouldn't cast 'malloc'. Check the FAQ to know why.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    24
    thank ! you !

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    24
    after reading the article on Casting malloc
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    just like the writer said
    Originally, the C language did not enjoy the void pointer. In those dark ages the char pointer was used as a generic pointer, so the definition of malloc looked something like this:

    char *malloc(size)
    int size;

    Of course, this tended to cause problems when trying to assign a char pointer to, say, a double pointer. Because of this a cast was required:

    double *p;
    p = (double *)malloc ( n * sizeof ( double ) );

    i do not konw the meaning of the sentences "those dark ages the char pointer was used as a generic pointer" what does writer mean??...
    in my opinon i think it is dangerous for the statement char *p;
    do you agree with me??

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    Although you don't HAVE to cast malloc, it wouldn't hurt so much if you needed to.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you need to, you're doing something wrong.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    The only "needs" you're likely to come across are
    1. You didn't include stdlib.h, in which case the fix is to include the header, not cast malloc
    2. You're compiling C++ code, in which case the fix is to use a C compiler, not cast malloc.

    > those dark ages the char pointer was used as a generic pointer
    Originally C (1970's variety) did not have void *, so char * was used instead.
    In the early 1980's, people started using void * (informally)
    In 1989, the ANSI-C standard made void * the correct way to return generic pointers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    24
    thank you !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 07:51 PM
  2. Please Heelp me ... I gave up
    By NANO in forum C++ Programming
    Replies: 14
    Last Post: 04-21-2002, 09:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21