Can anyone tell me what a Segmentation Fault is?

I tried posting this question under the course directly, but got no response, so figured I would ask here.
So I thought maybe I would venture out a little from a lesson on Header Files and Constructors and try separating the code used to draw a 2D map into its own header/source files, just so I make sure I’m understanding how creating separate source files and creating header files works. But when I created the files and called an instance of my source file in the main source file I’m getting an error on the header file constructor that just says “Segmentation Fault” with nothing else to help. I’m pretty sure I set the main function up correctly, here is the code:
#include “raylib.h”
#include “Level.h”

int main()

{

Level level;

level.createWindow();



while(!WindowShouldClose())

{

    level.drawLevel();

}

CloseWindow();

}

#pragma once
#include “raylib.h”

class Level

{

public:

    Level();

    void createWindow();

    void drawLevel();

private:

    int windowWidth{1920};

    int windowHeight{1080};

    Texture2D map{LoadTexture("maps/spellbook_base_level.png")};

    Vector2 mapPos{0.f, 0.f};

    float mapScale{6.f};

};

#include “Level.h”

Level::Level()

{

}

void Level::createWindow()

{

InitWindow(windowWidth, windowHeight, "V2");

}

void Level::drawLevel()

{

BeginDrawing();

ClearBackground(BLACK);

DrawTextureEx(map, mapPos, 0.f, mapScale, WHITE);

}

Everything looks ok but I don’t know why I’m getting that Segmentation Fault. Can someone help and maybe explain what I’m doing wrong?

Thanks!

A “segmentation fault” is basically an attempt to access memory you don’t have. This could be a pointer that has not been assigned to anything yet (a NULL pointer, in Java for example that gives you a dreaded NPE or NullPointerException), or points to an object (or just plain memory) that was free()d again, or a pointer calculation went bad (for example if you have a pointer to the elements of an array and loop over them and your loop has an offset-by-one error and you try to access memory beyond the array; but these days in C++ you’d probably use a smarter array and use an iterator or at least a proper index - still, if you’ve got an array of 8 elements (which normally go from index 0 to index 7) and try to use array[8] you will get an error, and this could be a crash with a “segmentation fault” if your runtime environment has very simple error handling), or try to use a pointer that got overwritten (for example it is at the memory position at array[8] (so its memory is right behind the array), and since that bit of memory also belongs to your program the system won’t have any problems with that (so, no segmentation fault at this time) but now the pointer contains garbage and when you try to use it you hit memory outside of what belongs to your program and crash. This is usually worse than an immediate crash when writing beyond the bound of the array, because now the real cause for the error is much harder to find.

it is called “segmentation fault” because the system divides memory into logical segments (that in early x86 were also part of the hardware memory model), and the memory access reached out of the segment(s) that belong to your program.

I’m relatively new to C++, while I can follow what your explanation is I’m still puzzled. If I create a header file containing the declarations for a source file, and the source file contains a function that performs a basic task, like drawing a picture, and call that function as an instance in the while loop of a main function, I don’t believe I am accessing memory that is null. The preprocess is including my header file code, which has reserved memory for that file. I create an instance, which is reserving a place in memory, and then I’m simply calling a function from that memory. At least that is my understanding so far. Mind you I only have about 2-3 months of learning under my belt, so I’m still adjusting to terminology, references/pointers, etc., so I may be way off here. From how I’m reading it my code should be working, can you or anyone else add on to help me understand what I’m doing wrong here?

My C++ is rather rusty and I don’t know your programming environment (I see you import some raylib headers, and I know of it, but I haven’t used it yet).
That syntax:
int windowWidth{1920};
is something I’m not familiar with.
Is that just a different way of an assignment like int foo = 5; or some sort of iniltialization for an array or list that gets 1920 elements?
From InitWindow() I would say it’s just like an assigment…
From what I understand I wonder if you get an error if that LoadTexture() for initializing the map fails?
It could just be that the map is never filled and when you try to draw it there’s nothing to draw.

Add some debug output that shows how the code is executed…

heckert,
Thanks for taking the time to help me with this, I really want to become proficient in C++ for other aspects beyond game making. I attached some pics of all three files in the project during Debug, and the console error. The weird thing is that I basically rebuilt the project from scratch, doing the exact same code over just to eliminate a VS problem and I can actually execute the code WITHOUT the source for the texture being in the project file. But once I copied and pasted the .png file into the source file for reference it gives me segmentation error. I’m actually really clueless now on what is happening, nothing like this was gone over in my lecturers. The syntax for int windowWidth{ } is member variable initialization for a class member. It can be used to assign values to a variable, similar to the assignment operator ‘=’. And the Raylib functions I’m running are basically just taking a 2D image and drawing it in a designated window.



So, the exception occurs in your Level class’ constructor. which would be the moment when those static initializations happen.
So you say you can run the program when the image file to be loaded isn’t there at all?
Have your tried a different image? Maybe it’s not your code but something about the file being loaded is amiss?
Mind you, I have no idea on how to correctly make use of Raylib. There might be something else missing you would need to do to initialize something…
Also, this static class initialization might not be the best place to do that image loading…

Yeah the program runs fine minus the parameter for the image reference, but as soon as you put the parameter back in it faults. So weird. Can you explain the last part you said about static class initialization? We didn’t go over ‘static’ as a keyword but from what I’m gathering its allocation of memory depending on the use of the static keyword? So using it on a class member allows it to be allocated in memory for the duration of the program execution, correct?

Static initialization as I thought on it in my posting has no connection to the static keyword.
It’s opposed to dynamic initialization which would be happening by assigning values in the constructor.

My terminology might be off though. Keep in mind, when I was using C++, the STL was still in the making.

Anyway, I would say something’s wrong about that initialization loading the image file. So I would move the assignment to the Texture2D map variable into the constructor, put some debugging output before and after the LoadTexture() call, and read up on Raylib’s documentation for that call.

Privacy & Terms