Pointer and segfaults question

This is a discussion on Pointer and segfaults question within the C Programming forums, part of the General Programming Boards category; Hi in my program for some reason when I assign somthing to a certain pointer my program segfaults. I have ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    114

    Question Pointer and segfaults question

    Hi in my program for some reason when I assign somthing to a certain pointer my program segfaults. I have tested this by calling exit(0) just before the assignment and it exits fine, just after the line and it segfaults. I have tried making a variable with a different name that is of the same type, and assigning that and it works fine.

    So my question is, when could setting a pointer to point at somthing directly cause a segfault?

    Ovbiously if I assigned it wrong and then tried to use what it pointed to it could segfault but actualy making it point at somthing is causing the problem!

    This crashes:
    Code:
    struct Slevel *level;struct Slevel *test_level;
    
        /* Load the map if needed*/
        if (level == NULL)
        {
           test_level = load_level("levels/test.txt");
           level = test_level;
           exit(0);
        }
    but this doesn't
    Code:
    struct Slevel *level;struct Slevel *test_level;
    
        /* Load the map if needed*/
        if (level == NULL)
        {
           test_level = load_level("levels/test.txt");
           exit(0);
           level = test_level;
        }
    My code is an absolute mess at the moment, so if you could tell me an example that just setting a pointer could cause a segfault it would get me started as to what to look for. Thanks

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    The pointer assignment can never segfault. ( It probably can in special circumstances, but I think it's pretty safe to say that it's not the case here. ).

    Try replacing the single / by a double // in your string literal. As it stands now, there is a tab ( /t ) in your path. That will probably fail somewhere down the line.

    Edit: forget that tab stuff, I thought it was the other slash: \t
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    114
    Man this is wierd, didn't think it coiuld really cause it! I think I'm going to try and split my code up into seperate files before trying to debug it. (its like 1100 lines long and its getting hard to find stuff)

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The two versions with exit(0) don't prove that the assignment causes a segfault.( as nvoigt pointed out this is safe ).
    It proves that the segfault happens in the function load_level(..). guess the file "levels/test.txt" doesn't exist or is somewhere else.
    Kurt

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    114
    Your probably right that the level loading stuff is to blame although your wrong about the file name, it definately finds that and I have it printing out variables as it loads them from files. Also I put an exit() at the end of load_level() just before it returned and it didnt segfault then.

    The thing that really confuses me is I put the exit() a few lines further down after a function (and after the pointer is assigned) and it stops it from seg faulting! Wish I could get the dev-c++ debuger working.
    Last edited by kzar; 09-15-2005 at 09:56 AM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ZuK
    It proves that the segfault happens in the function load_level(..).
    This doesn't really mean anything though. Just because this is where it crashes, doesn't mean this is where the problem is. If you've trashed your memory by munging up your pointers some place else, it could just be that this is where it happens to manifest itsefl. The actual problem could be, and most likely is, elsewhere.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct Pointer Problems. (Warning: Long Post.)
    By Phoenix940 in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 10:04 PM
  2. Basic program design, passing pointer to a function
    By heras in forum C Programming
    Replies: 14
    Last Post: 04-02-2008, 04:21 AM
  3. Segfaults
    By mike_g in forum C Programming
    Replies: 3
    Last Post: 08-16-2007, 12:47 PM
  4. pointer problem
    By R.Stiltskin in forum C Programming
    Replies: 25
    Last Post: 10-20-2005, 06:02 PM
  5. Need some advice on evil double pointer...
    By jhopper in forum C Programming
    Replies: 8
    Last Post: 02-27-2002, 01:16 PM

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