Thread: malloc

  1. #1
    Unregistered
    Guest

    Unhappy malloc

    help me !~~~

    how to use malloc!~~

    thank you

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Alright, I'll post some code even though there are a million sites that could help you with this one.
    Code:
    int x = malloc(5*sizeof(int)); //will work on many new compilers...
    it is better practice to do this though:
    Code:
    int x = (int)malloc(5*sizeof(int));
    Last edited by master5001; 09-24-2001 at 01:15 AM.

  3. #3
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    Interesting....

    I would use:

    int *x;
    x=(int *)malloc(5*sizeof(int));

    alex

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Casting the return result of malloc masks your failure to include stdlib.h. In some systems (where pointers are larger than ints, or pointers are returned in different registers?), this WILL cause your code to break.

    Since malloc returns void* in ANSI-C (and void* is the universal pointer type which can be silently cast into any other pointer type), this is (imo) correct

    Code:
    #include <stdlib.h>
    ...
    int *arr = malloc( 5 * sizeof(int) );
    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.

  5. #5
    Unregistered
    Guest
    what about
    int *arr;
    arr=malloc(5*sizeof(*arr));
    so that if type of arr changes you dont have to worry .

  6. #6
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    The type can't change after it has been defined. In a C++ compiler you need to use type casts but in pure C it is not used. Maybe pure C is more compatible but if you advertise you product to work on MS Windows than you can use type casting and a C++ compiler.
    I compile code with:
    Visual Studio.NET beta2

  7. #7
    Unregistered
    Guest

    Post

    what I meant by changing of type is that instead of int suppose you may have to make it long then all you have to do here is to change the first line to

    long *arr;

    arr=malloc(5*sizeof(*arr)); doesnt need to be changed

  8. #8
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    int* a;


    I don't find
    a = malloc(sizeof *a);
    as readable as
    a = malloc(sizeof(int));
    I think if you really wanted to protect yourself from
    changing the type you would use a typedef.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM