Thread: malloc sizeof 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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06: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, 08:18 AM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM