quick malloc/pointer Q

This is a discussion on quick malloc/pointer Q within the Linux Programming forums, part of the Platform Specific Boards category; Hi, I'm curious about something... sometimes in code I'll people use malloc on a structure, and they use '*my_struct' instead ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    17

    quick malloc/pointer Q

    Hi,

    I'm curious about something...

    sometimes in code I'll people use malloc on a structure, and they use '*my_struct' instead of just 'my_struct'... why is that?

    Also, when returning a pointer to a structure, I've seen the structure defined as 'static MY_STRUCT *my_struct;', again, why is that?

    Thanks
    -Jase

    p.s. Anyone have the link to a site that explains pointers, linked lists, and memory allocation in detail? Thanks.
    [ Jase]

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    19
    malloc returns a pointer to a chunk of memory of the requested size. Hence, you must declare a pointer like:

    my_struct *m;

    and use it to hold the return value. Also, malloc returns a (void *), that is, pointer to void, and you must cast it into the required type before using:

    my_struct *m = (my_struct *)malloc(sizeof(my_struct));

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    148
    Originally posted by vikasgp
    Also, malloc returns a (void *), that is, pointer to void, and you must cast it into the required type before using:
    In C++,Yes,In C no.
    Sometimes it can be dangerous to cast the return value.
    From comp.lang.c
    Standard C does not require a cast for assignments from a (void *) type to
    some other pointer type. The "narrowing" is done for you. Since malloc()
    returns a `void *', no cast is required.

    As has been repeated here before, an explicit cast is discouraged,
    because if there is not a prototype in scope, malloc() is assumed to be
    a function returning an `int'. Without a prototype in scope, the cast
    will hide the fact that malloc() has returned an `int' rather than a
    `void *', and your compiler will not have had the opportunity to warn
    you about the mistake. If the size of an `int' is not the same size as
    a `void *' on your machine, then `bad things will happen'. If it does
    happen to work on your machine, then you will become terribly confused
    when the identical code does not work on someone else's machine.
    ....
    If you get a warning about there being no prototype for malloc() in
    scope then your code is broken, pure and simple. No amount of casting
    will cure it.

    This is the reason for advising against casting the result of malloc().
    If you cast the result of malloc then *some* compilers will still detect
    the error of not including stdlib.h. If you don't cast the result
    of malloc then *all* compilers will detect it.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    17
    See, I'm well aware of how to delcare a pointer...

    take a look at the way i malloc my data.

    struct MY_DATA *my_Data;

    my_Data=malloc(sizeof(*my_Data));

    I was curious about the second line of code, why you have to use a *my_Data, and why not just do...

    my_Data=malloc(sizeof(my_data));

    or does it not matter? and yes, I' using C.
    [ Jase]

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    148
    See,you must allocate memory for your struct,and thats sizeof(*myData),or sizeof(MY_DATA).
    The other one only allocates memory for a pointer to a MY_DATA struct
    Ex.
    Code:
    struct Foo
    {
        int array[10];
    };
    ...
    struct Foo* pFoo;
    sizeof(*pFoo); /* around 40 */
    sizeof(pFoo); /* around 4 */
    /*Size depends on your compiler */

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    17
    So what does it matter if I use...

    my_Data=malloc(sizeof(*my_Data));

    or

    my_Data=malloc(sizeof(MY_DATA));

    ?

    I'm assuming... sizeof(*my_Data) returnes the size of the address that it points to? and sizeof(MY_DATA) returns the size of the structure?

    So which of the two above is the proper way to malloc?
    Last edited by Jase; 05-25-2003 at 06:09 AM.
    [ Jase]

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    17
    Thank you very much vVv, you've brought malloc much more into focus. I apologize for using the wrong forum, I simpley used this forum because I am working under linux.

    Thanks again.
    [ Jase]

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    19
    In C++,Yes,In C no.
    Sometimes it can be dangerous to cast the return value.
    Thanks for pointing that out. However...
    From the libc info:
    You can store the result of `malloc' into any pointer variable
    without a cast, because ISO C automatically converts the type `void *'
    to another type of pointer when necessary. But the cast is necessary
    in contexts other than assignment operators or if you might want your
    code to run in traditional C.
    Also, in all the code I've seen, the result is casted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 11:33 AM
  2. recursive quick sort - stack overflow
    By Micko in forum C Programming
    Replies: 9
    Last Post: 01-01-2005, 05:51 PM
  3. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 10:06 AM
  4. Quick Sort Help
    By NavyBlue in forum C Programming
    Replies: 1
    Last Post: 03-02-2003, 10:34 PM
  5. Replies: 0
    Last Post: 04-30-2002, 08:24 PM

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