For a project I need to have a vector array of pointers to a struct, I have no problem with the vector array, but when I try to set the values of member in the struct the program crashes. Here is some basic code that I am having the problem with:
[code]
struct point // grid point
{
int x;
int y;
int z;
};
int main()
{
point* new_point;
new_point->x = 5; // Crashes on this line, Segmentation Fault
return 0;
}
[/code]
I tried looking this up but I found examples that were also using the -> operator to set the values (i have not tried copy-pasting the examples yet).
Anyone have any ideas of what could be going wrong/ any alternate solutions?
You need to create the struct, not just the pointer to it.
[cpp]
point* new_point;
[/cpp]
This only creates an [I]uninitialized[/I] pointer.
[cpp]
point new_point;
[/cpp]
This creates a new [I]uninitialized[/I] struct on the stack. As this is uninitialized, you need to set all its members:
[cpp]
point new_point;
new_point.x = 1;
new_point.y = 2;
new_point.z = 3;
/*now fully initialized*/
[/cpp]
If you want to create an uninitialized struct on the heap so you can use it outside the current scope, you can do:
[cpp]
point* new_point = malloc(sizeof(*new_point));
//or, preferably, since you're using C++,
point* new_point = new point();
[/cpp]
In C++, you can do the initialization in a constructor.
You haven't allocated any memory to the struct, you're dereferencing invalid memory.
C: point* new_point = malloc(sizeof(point));
C++: point* new_point = new point;
Short answer.
[cpp]point* new_point = new point();
new_point->x = 5;[/cpp]
Your pointer wasn't pointing to anything. (well, anything valid..)
Ah I see what's going on there. Thanks :)
I thought you had to declare struct objects like:
[code]struct point *new_point;[/code]
Is that only a C thing?
[QUOTE=PvtCupcakes;16624495]I thought you had to declare struct objects like:
[code]struct point *new_point;[/code]
Is that only a C thing?[/QUOTE]
You can do that in both C and C++, but if you want to use it without the "struct" keyword in C you need to typedef it:
[cpp]
struct A_t
{
...
};
typedef struct A_t A;
/*or simply;*/
typedef struct
{
...
} A;
[/cpp]
[QUOTE=Jallen;16623238]Short answer.
[cpp]point* new_point = new point();
new_point->x = 5;[/cpp]
Your pointer wasn't pointing to anything. (well, anything valid..)[/QUOTE]
Poor timing for a "short answer" really, as he should be using a constructor in that case...
It's all working now, but after closing the SFML window and closing the console output, I'm getting a memory read error from somewhere. Even though the broken line appears to be AFTER the 'return EXIT_SUCCESS'. Very confusing indeed.
[QUOTE=Mattz333;16627428]Even though the broken line appears to be AFTER the 'return EXIT_SUCCESS'. Very confusing indeed.[/QUOTE]
You should really confirm that.
[QUOTE=Mattz333;16627428]It's all working now, but after closing the SFML window and closing the console output, I'm getting a memory read error from somewhere. Even though the broken line appears to be AFTER the 'return EXIT_SUCCESS'. Very confusing indeed.[/QUOTE]
Why did you give up SDL Matt. Why oh why.
[QUOTE=nos217;16627680]Why did you give up SDL Matt. Why oh why.[/QUOTE]
SDL is in C, he's using C++.
Choosing SFML is perfectly natural, and should improve his productivity quite a bit :)
Yeah you're right. I just don't want to change from SDL now though.
Hey Matt, making anything interesting?
After seeing all those Isometric projects in the 'What are you working on' thread, I decided to try and make an isometric terrain editor that draws everything with polygons.
Also just tried running it again and the error isn't coming up anymore.
[QUOTE=Mattz333;16642331]Also just tried running it again and the error isn't coming up anymore.[/QUOTE]
That doesn't mean it won't come back.
You were probably just lucky this time around running it. Runtime errors are never consistent, especially ones to do with memory management.
Nobody even bothered about delete?
Jeez
Anyway, at the end of the program don't forget to
[code]
delete new_point;
[/code]
Or you'll be having lots of memory leaks in the future.
I'm not very good with pointers, but don't you have to set it to null or something before deleting it?
[QUOTE=Agent766;16646957]I'm not very good with pointers, but don't you have to set it to null or something before deleting it?[/QUOTE]
No, otherwise how could delete know where to delete?
You can set it to NULL after you use delete, but that's not necessary. It can help avoid errors.
[QUOTE=Agent766;16646957]I'm not very good with pointers, but don't you have to set it to null or something before deleting it?[/QUOTE]
Lol no.
I am pointing at a random space.
I now point at this new item I have created.
I'm finished with it, I stop pointing at it.
I delete it... wait, I'm not pointing to it WHERE IS IT!??
Point the pointer to 0 [I]after[/I] deleting it.
:argh: gparent! :ninja:
Sorry, you need to Log In to post a reply to this thread.