Thread: malloc segmentation fault

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    4

    malloc segmentation fault

    hello everybody,
    I have written a simple program with malloc ,and I am getting segmentation fault . Can anyone tell me why its so?

    Thanks in advance

    Code:
    #include<stdio.h>
    #include<malloc.h>
    
    int main()
    {
            int **a;
            a[0]=(int *)malloc(2*(sizeof(int)));
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by BharathKumar View Post
    Can anyone tell me why its so?
    You're accessing memory your program does not own.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    1) <malloc.h> is a nonstandard header. When using malloc(), you should #include <stdlib.h> instead.

    2) You don't have to cast malloc() - see

    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    3) You need to allocate space for a[0], a[1], etc. BEFORE using malloc() to assign memory to each of them. For example, if you wanted to allocate space for a[0], a[1], and a[2], you could write
    Code:
      int **a;
      a = malloc(3*sizeof(int *)); /* allocates space for a[0], a[1], a[2] */
      a[0] = malloc(2*sizeof(int)); /* allocates space for a[0][0], a[0][1] */
    4) Of course you should check for NULL after calling each malloc() - I neglected to do this in the example above.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    4
    Thank You Robatino .

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    And you should free the memory you've allocated, once your done with it

    Eg:
    Code:
    #include <stdlib.h>
    
    int main(void)
    {
        const size_t rows = 3, cols = 5;
        int ** a;
        size_t c;
        
        /* allocate rows */
        if((a = malloc(rows * sizeof(int *))) == NULL)
        {
            perror("Failed to allocate rows");
            return 1;
        }
    
        /* allocate cols */
        for(c = 0; c < rows; c++)
        {
            if((a[c] = malloc(cols * sizeof(int))) == NULL)
            {
                perror("Failed to allocate cols");
                return 2;   /* ideally you should free the cols and rows, but cbf :) */
            }
        }
    
        /* do whatever with rows and cols */
    
        /* free cols */
        for(c = 0; c < rows; c++)
        {
            free(a[c]);
            a[c] = NULL;
        }
    
        /* free rows */
        free(a);
        a = NULL;
        return 0;
    }
    Last edited by zacs7; 06-26-2007 at 05:25 AM.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    4
    Thank You Zacs7 ,as I had missed that part.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM