Thread: mallocs giving segmentation fault without returning NULL

  1. #1
    Registered User
    Join Date
    Feb 2012
    Location
    Bangalore, India
    Posts
    12

    mallocs giving segmentation fault without returning NULL

    section of my code is:

    Code:
    printf(" i am upto here %d %d\n", i, j);
    payoffs[i][j] = (double *) malloc(sizeof(double)*temp);
    if( payoffs[i][j] == NULL)
        {
           printf("Mem Error \n");
           exit(-1);
        }
     printf("but not  upto here\n", i,j);

    When value of temps 10^4 this works fine but when temp is 20^4, i get segmentation fault and also error message is not printed. If malloc was not able to allocate meory i should have got Mem Error, why is it only giving segmentation fault.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Are you running this on Linux, by any chance?

    By the way, do not cast the return value of malloc unless you need the code to be compilable as C++.
    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
    Feb 2012
    Location
    Bangalore, India
    Posts
    12
    Quote Originally Posted by laserlight View Post
    Are you running this on Linux, by any chance?
    Yes, I am using Fedora 13, x_86

    By the way, do not cast the return value of malloc unless you need the code to be compilable as C++.
    I removed the casting but it made no difference. In other general case why does casting do potential harm?

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    How is payoff declared ?
    (Guessing) Shouldn't you assign the result of malloc to payoff and not payoff[i][j] ?

  5. #5
    Registered User
    Join Date
    Feb 2012
    Location
    Bangalore, India
    Posts
    12
    Quote Originally Posted by manasij7479 View Post
    How is payoff declared ?
    (Guessing) Shouldn't you assign the result of malloc to payoff and not payoff[i][j] ?
    Thats not the issue, its working for all values of temp less than 10^4.. anyway. My declaration and allocation is ; ( here MEM_ERROR is macro)

    Code:
     double ***payoffs = NULL;  
    ...................................
    for(i =0; i < players; i++)
            {    
                    temp = profile/actions[i];
                    payoffs[i]= (double**)malloc(sizeof(double*));
                    if( payoffs[i] == NULL)
                            MEM_ERROR
                    maximum[i]= (char **)malloc(sizeof(char *));
                    if( maximum[i] == NULL)
                            MEM_ERROR
                    for(j = 0; j < actions[i]; j++)
                    {   
                            maximum[i][j] = (char *) malloc(sizeof(char*)*temp);
                            if ( maximum[i][j] ==NULL)
                                    MEM_ERROR
                            printf(" i am upto here %d %d %d\n", i, j, temp);
                            payoffs[i][j] = malloc(sizeof(double)*temp);    
                            if( payoffs[i][j] == NULL)
                                    MEM_ERROR
                            printf("but not  upto here\n", i,j);
                    }
            }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by durgadatta
    Yes, I am using Fedora 13, x_86
    I don't know if this is the problem that you're facing, but you may be interested to read: What is Optimistic Memory Allocation? How does it affect me?

    Quote Originally Posted by durgadatta
    I removed the casting but it made no difference. In other general case why does casting do potential harm?
    When you forget to #include <stdlib.h>
    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

  7. #7
    Registered User
    Join Date
    Feb 2012
    Location
    Bangalore, India
    Posts
    12
    Quote Originally Posted by laserlight View Post
    I don't know if this is the problem that you're facing, but you may be interested to read: What is Optimistic Memory Allocation? How does it affect me?
    thank you.. Seems like optimisitc memory alocation in linux is the problem. I am working out. Its a fairly large program, will take some time.... its a new information whatsoever. thanks

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > double ***payoffs = NULL;
    So do you do this at some point before trying to use it?

    payoffs = malloc( players * sizeof(*payoffs) );


    And what about this?
    > payoffs[i]= (double**)malloc(sizeof(double*));
    You do payoffs[i][j] later on, so this simply isn't enough memory.

    Try say
    payoffs[i] = malloc( actions[i] * sizeof(*payoffs[i]) );
    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.

  9. #9
    Registered User
    Join Date
    Feb 2012
    Location
    Bangalore, India
    Posts
    12
    Quote Originally Posted by Salem View Post
    > double ***payoffs = NULL;
    So do you do this at some point before trying to use it?

    payoffs = malloc( players * sizeof(*payoffs) );
    I have done this before use:
    Code:
     payoffs = (double ***)malloc(sizeof(double **)*players);
    I think they are same, isnt it?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If everything is correct, then yes. Otherwise, the version without the cast and which takes the sizeof based on the object rather than the type is correct.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fclose operation giving seg fault
    By ritu85 in forum C Programming
    Replies: 14
    Last Post: 07-23-2009, 02:54 AM
  2. passing a pointer to string giving segmentaion fault
    By Alexpo in forum C Programming
    Replies: 15
    Last Post: 10-10-2008, 05:11 AM
  3. CreateDIBSection() returning NULL
    By arjunajay in forum Windows Programming
    Replies: 5
    Last Post: 07-26-2006, 09:54 AM
  4. First record returning null??
    By Kumura in forum C Programming
    Replies: 7
    Last Post: 05-31-2005, 07:10 PM

Tags for this Thread