SegFault because of calloc()

This is a discussion on SegFault because of calloc() within the C Programming forums, part of the General Programming Boards category; I've narrowed down a segfault I've been getting to the following code, but I don't know why it's happening. My ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    11

    SegFault because of calloc()

    I've narrowed down a segfault I've been getting to the following code, but I don't know why it's happening.

    My structs
    Code:
    /*Struct to store point*/
    typedef struct{
      double x;
      double y;
    }point;
    
    /*Struct to store figures*/
    typedef struct{
      char name[FIG_LEN +1];
      point* coords;
    }figure;

    The code at fault
    Code:
      char line[LINE_LEN + 1];
      static figure* newFigure;
      int coord = 1;
      int numCoords = 2;
    
      newFigure->coords = calloc(numCoords, sizeof(point));
    I get a segfault on the last line. I"m not sure if I'm accessing the structs in the correct way, is that the problem?

    Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You are dereferencing a pointer that doesn't point to anything. What does newFigure point to?
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    To the line:

    Code:
    newFigure = calloc(1, sizeof(figure));
    Which was not there...

    Thank you

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by TheDenominater View Post
    To the line:

    Code:
    newFigure = calloc(1, sizeof(figure));
    Which was not there...

    Thank you
    Post a more significant chunk of code then. More than likely you haven't posted the code that causes the segfault. You should also be checking the return value of calloc(), also why not use malloc() for "newFigure"?

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    Hey, sorry about that. When I said "was not there", I meant that I had forgotten to include it in my code, so it was the source of the error.

    So I've found the error and am good now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc() resulting in a SegFault?!
    By cipher82 in forum C++ Programming
    Replies: 21
    Last Post: 09-18-2008, 11:24 AM
  2. use of printf prevents segfault!
    By MK27 in forum C Programming
    Replies: 31
    Last Post: 08-27-2008, 12:38 PM
  3. malloc, calloc from the FAQ
    By salvadoravi in forum C Programming
    Replies: 10
    Last Post: 01-21-2008, 02:29 AM
  4. Segfault from calloc
    By Happy_Reaper in forum C Programming
    Replies: 5
    Last Post: 04-06-2006, 12:53 AM
  5. difference between calloc and malloc
    By saravanan_ts in forum C Programming
    Replies: 4
    Last Post: 07-28-2003, 06:13 AM

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