malloc sizeof struct

This is a discussion on malloc sizeof struct within the C Programming forums, part of the General Programming Boards category; How can I use malloc to create memory instead of assigning the pointer below to an instance of this struct? ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    malloc sizeof struct

    How can I use malloc to create memory instead of assigning the pointer below to an instance of this struct?

    Code:
         struct fraction {
             int numerator;
             int denominator;
            };
    
         struct fraction *stanford;
    I am looking to do something like below but I know this dont work

    Code:
      
         stanford = (int *) malloc(sizeof());
    So for now I am stuck doing it this way:

    Code:
         
         struct fraction thistime;
         
         stanford = &thistime;
    And then the goal is to do something like below using malloc (very basic for now):
    Code:
         stanford->numerator = 100;
         stanford->denominator = 200;
        
          printf("%d is stanford numerator and %d\n", stanford->numerator);

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,960
    It would be:
    Code:
    struct fraction *stanford = malloc(sizeof(struct fraction));
    
    /* ... */
    
    free(stanford);
    To be on the safe side, you should check if stanford is NULL before using it. If it is NULL then malloc() failed to allocate memory.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Thank you very much

    That works great and I will check for null as you advised.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    One more question

    If I dont free stanford (which I will always do) what are some of the problems that could occur? Also if I did not check for null and memory was not available what type of problems could arise? Thanks.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,960
    If I dont free stanford (which I will always do) what are some of the problems that could occur?
    You would have a memory leak.

    Also if I did not check for null and memory was not available what type of problems could arise?
    You would dereference a null pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    A good habit to get into when calling malloc/calloc is to use the dereferenced variable you're allocating as the object of the sizeof operator; i.e.,
    Code:
    struct fraction *stanford = malloc(sizeof(*stanford));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  3. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM

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