Thread: Memory allocation problem

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    167

    Memory allocation problem

    i have the following structure:
    Code:
    typedef struct in
    {
            char *nume;
            double val;
    } in;
    in main i have:
    Code:
    int main()
    {
    in *t;
    void test(&t);
    return 0;
    }
    and the function test:
    Code:
    void test(in **a)
    {
            int i;
            (*a)=malloc(sizeof(in));
            for(i=0;i<4;i++)
            {
                    (*a)=realloc((*a),(i+1)*sizeof(in));
                    (*a[i]).nume=malloc(sizeof(char));
            }
    }
    the problem is that it segm faults me!
    What could the problem be ?

    Thank you!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Let's assume that the first call to realloc fails. What happens for the second call? Or better yet, what happens when you try to access it to malloc "sizeof(char)" for nume? Also, 'sizeof( char )' is always 1.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    so what is the problem? i really don't understand from that post. sorry but i'm a beginner.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You're subscripting the wrong pointer
    Code:
    void test(in **a)
    {
      int i;
      /*!! (*a)=malloc(sizeof(in)); */
      /*!! realloc is self-starting, you don't need a malloc, just a NULL */
      (*a) = NULL;
      for(i=0;i<4;i++)
      {
        void *temp = realloc((*a),(i+1)*sizeof(in));
        if ( temp != NULL ) {
          (*a)=temp;
          (*a)[i].nume=malloc(sizeof(char));
        }
      }
    }
    Also, pay attention to the way realloc is used.
    If you just do
    p = realloc( p, size );
    you risk losing everything if realloc fails.

    By using a temp pointer, you still have your original memory even if you can't get any more.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with linked list and shared memory
    By Sirfabius in forum C Programming
    Replies: 10
    Last Post: 11-10-2008, 04:45 PM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM
  5. Memory Problem - I think...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-24-2001, 12:14 PM