Thread: Memory allocation for structures

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    Memory allocation for structures

    Why do i get a Seg Fault if I run:
    Code:
    #include <stdio.h>
    
    struct rec
    {
        int i;
        float f;
        char c;
    };
    
    int main()
    {
        struct rec *p;
        
        (*p).i=10;
        (*p).f=3.14;
        (*p).c='a';
        printf("%d %f %c\n",(*p).i,(*p).f,(*p).c);
        free(p);
        return 0;
    }
    but not
    Code:
    #include <stdio.h>
    
    struct rec
    {
        int i;
        float f;
        char c;
    };
    
    int main()
    {
        struct rec *p;
        p=(struct rec *) malloc (sizeof(struct rec));
        (*p).i=10;
        (*p).f=3.14;
        (*p).c='a';
        printf("%d %f %c\n",(*p).i,(*p).f,(*p).c);
        free(p);
        return 0;
    }
    can someone explain why exactly.
    and do I always have to do p=(struct rec *) malloc(sizeof(struct rec)); ?
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    struct rec *p;
    (*p).i=10;
    You have absolutely no idea where you just tried to write that integer - usually somewhere unpleasant, which is why the OS steps in with a seg fault.
    If you're unfortunate, it will work, thus suckering you into thinking your program is correct, when in fact it isn't.


    struct rec *p;
    p=(struct rec *) malloc (sizeof(struct rec));
    (*p).i=10;
    This on the other hand is better - you know exactly where that int is going - into the memory returned by malloc

    > and do I always have to do p=(struct rec *) malloc(sizeof(struct rec)); ?
    You've always got to initialise your pointers, and malloc is a good way to do it

    A less useful way is
    struct rec tmp;
    struct rec *p = &tmp;

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Salem
    struct rec *p;
    p=(struct rec *) malloc (sizeof(struct rec));
    (*p).i=10;
    This on the other hand is better - you know exactly where that int is going - into the memory returned by malloc
    Not necessarily better tho if malloc fails. Best do some error checking before using that pointer!

    Code:
    if ((p = (struct rec *) malloc (sizeof(struct rec))) == NULL)
    {
    /* malloc has failed, bail out or somthing */
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    thanks
    yeah, I usually have ERROR checks in my programs
    I am going to write a new function
    void bail_out(char* error[]);
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 11:41 PM
  2. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  4. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 05:56 AM
  5. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM