Thread: New to C, Segmentation fault

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    3

    New to C, Segmentation fault

    Hi! Sorry I just don't really understand why this is throwing a Seg fault.

    I have a typedef'd struct for a Queue that has an int field called size.

    It throws a seg fault after this

    Code:
    Queue *frames;
    frames->size = atoi(argv[1])
    Where a number is passed as an argument through main.

    Does frames need some sort of initialization?

    Please assist @_@

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    3
    Ok so.. I fixed that problem I just hadn't malloc'd the necessary memory for my Queue.
    Now I'm getting another seg fault.
    I currently have two structs Queue's and QNode's
    QNodes have pointers to the next and previous QNode and Queue's have a pointer to the front and back of the queue.

    Why am I getting a seg fault at this

    Code:
     int addNode(int pageNumIn, Queue *queue)
    {
      QNode *nX = createQNode();
      queue->front->next = nX; //Fails here
      queue->front = nX;
    }

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    3
    JK I think I got this one too , the front was null so there was no next for null lol

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    To dereference any pointer, it is necessary for that pointer to point at a valid object. This is recursive.

    So the statement "queue->front->next = nX;" requires both queue to be a valid object AND queue->front to be a valid object.

    YOU need to ensure both that queue points at a valid object AND that queue->front points at a valid object. In that order.

    Random hacking like you are doing won't cut it.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Hint:
    Put some damn sanity checks into your code and you avoid all this.
    And initialise all pointers to NULL when you create them so you get proper errors rather than random errors based on the previous contents of that part of memory where the pointer happens to be created.

    For example, initialising all created pointers to NULL (even before you do anything - e.g. in your CreateNode function, do "returned_pointer = NULL" as the first line - then, NO MATTER what you do in the function, the return(returned_pointer) at the end of it will never give a "false" pointer, and the worst case is it will give a NULL one.

    Then, making sure the parameter's aren't null before you start dereferencing them. It's one damn "if", that you can comment out later, and you won't even notice it being executed if you have it on every call of the function (like I tend to do - anything that is a function that handles some sort of pointer, I put a NULL pointer check in, inside a #define. It's amazing what you find when you start extending the program without thinking and suddenly start getting NULL pointer errors deep inside the program that would otherwise that HOURS to find. And when you're happy that you *don't* do anything stupid, you can always un#define the code and remove those checks - though I've never been confident enough to get that far on any non-trivial program).

    Notice that NULL pointer checks are, however, useless if you are just creating unintialised pointers, because then you're just trashing random memory with your data even if it's not been allocated to you or is in use by some other part of your program.

    And, hell, if you used your debugger, you'd spot the problem immediately anyway - half of those things are going to be NULL or undefined (i.e. have extremely spurious data in their members) when you try to -> them.

    Please realise that a segfault is an attempted trash of memory that was SO stupid that the machine happened to catch it. You can easily do stuff like write over the other data in your program and NOT trigger segfaults, if the random values the unallocated pointers take happen to be unlucky - you can even get to the point where a program should segfault but consistently carries on getting the same false data every time because of the layout of the program by the compiler. Things like DEP etc. WILL NOT protect you against trashing your own program's data in memory.


    Initialise all variables before use (doubly-important for pointers).
    Check that variables are valid inside functions (e.g. do you check the bounds of pageNumIn, or that argv[1] exists, or that atoi(argv[1]) is a valid number?

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault
    By cfanatic in forum C Programming
    Replies: 17
    Last Post: 07-14-2012, 11:03 PM
  2. Segmentation fault!?!?!?
    By Viper187 in forum Windows Programming
    Replies: 57
    Last Post: 10-02-2008, 09:40 PM
  3. Help on Segmentation Fault!!!
    By SweeLeen in forum Linux Programming
    Replies: 4
    Last Post: 02-19-2006, 11:33 AM
  4. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM