Memory allocation problem

This is a discussion on Memory allocation problem within the C Programming forums, part of the General Programming Boards category; i have the following structure: Code: typedef struct in { char *nume; double val; } in; in main i have: ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 03: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, 07:36 AM
  5. Memory Problem - I think...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-24-2001, 12:14 PM

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