Pointer to Structure leads to segfault

Joined
Jan 13, 2009
Messages
1
Reaction score
0
Hi all...first time poster, long time reader.

I've been experiencing some strange behavior on a Linux development system (Red Hat 5) compiling a C++ App. I've tried to reduce the source to something as simple as possible so that I can fit all source that would still causes my problem. Below are the three simple source files for class FooClass:

FooClass.hpp:
Code:
class FooClass
{
public:
[INDENT]void fooMethod();[/INDENT]
private:
[INDENT]typedef struct {[/INDENT]
[INDENT][INDENT]int fooData;[/INDENT][/INDENT]
[INDENT]} FooStruct;[/INDENT]
};

FooClass.cpp
Code:
#include "FooClass.hpp"

void FooClass::fooMethod()
{
[INDENT]FooStruct* foo;[/INDENT]
[INDENT]foo->fooData = 5;[/INDENT]
}

main.cpp
Code:
#include "FooClass.hpp"
int main(int argc, char** argv)
{
[INDENT]FooClass* foo = new FooClass();[/INDENT]
[INDENT]foo->fooMethod();[/INDENT]
[INDENT]delete foo;[/INDENT]
[INDENT]return 0;[/INDENT]
}

I get no compiler errors but everytime i run this, I get a segmentation fault. The fault happens at the point when I try to initialize a member of the data structure, FooStruct (see line 6 in FooClass.cpp: foo->fooData = 5; )

Please help as I am stumped.

Oh...and for a side note, this runs with no problem on a different system running an older version of Linux (Red Hat 4).

Thanks in advance!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top