C++ - Pointer to Structure leads to segfault

D

dmangal

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:
class FooClass
{
public:
void fooMethod();
private:
typedef struct {
int fooData;
} FooStruct;
};

FooClass.cpp:
#include "FooClass.hpp"

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

main.cpp:
#include "FooClass.hpp"

int main(int argc, char** argv)
{
FooClass* foo = new FooClass();
foo->fooMethod();
delete foo;
return 0;
}

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!

dmangal
 
Z

Zachary Turner

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:
class FooClass
{
public:
     void fooMethod();
private:
     typedef struct {
          int fooData;
     } FooStruct;

};

FooClass.cpp:
#include "FooClass.hpp"

void FooClass::fooMethod()
{
     FooStruct* foo;
     foo->fooData = 5;

}

main.cpp:
#include "FooClass.hpp"

int main(int argc, char** argv)
{
     FooClass* foo = new FooClass();
     foo->fooMethod();
     delete foo;
     return 0;

}

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!

dmangal

Change to this:

void FooClass::fooMethod()
{
this->fooData = 5;
}

or this:

void FooClass::fooMethod()
{
fooData = 5;
}

When you are setting foo->fooData, foo refers to a pointer that has
never been allocated. That is a local variable to the fooMethod()
function, totally different than the FooClass* foo you've declared in
your main function.
 
E

Erik Wikström

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:
class FooClass
{
public:
void fooMethod();
private:
typedef struct {
int fooData;
} FooStruct;

C-ism, in C++ you don't need the typedef:

struct FooStruct {
int fooData;
};
FooClass.cpp:
#include "FooClass.hpp"

void FooClass::fooMethod()
{
FooStruct* foo;

You create a pointer to a FooStruct, but you do not initialise it to
anything, so it could point to just about anything.
foo->fooData = 5;

And here you take the piece of memory that the pointer happens to point
to and try to treat it as if it pointed to a FooStruct object.

You forgot to initialise the pointer, replace the FooStruct* foo; line
with this:

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

Just luck. I suppose a change in RH5 caused to pointer to be initialised
to a different value which happens to point to a unallocated address
while in RH4 you were lucky and the pointer pointed to some allocated
memory.
 
Z

Zachary Turner

Change to this:

void FooClass::fooMethod()
{
   this->fooData = 5;

}

or this:

void FooClass::fooMethod()
{
   fooData = 5;

}

When you are setting foo->fooData, foo refers to a pointer that has
never been allocated.  That is a local variable to the fooMethod()
function, totally different than the FooClass* foo you've declared in
your main function.

Oops, as the others have pointed out, I didn't notice that fooData was
inside a struct. Either way, the problem is you never created an
instance of FooStruct. You only created a POINTER to a FooStruct
which had no memory associated with it. You allocated space for a 4
or 8 byte pointer, but not the space for the object being pointed to.
If you want this instance of FooStruct to stick around after the
function returns, change it to

FooStruct* foo = new FooStruct();
foo->fooData = 5;

If you don't need FooStruct after the function returns, change it to:

FooStruct foo;
foo.fooData = 5;
 
R

Rolf Magnus

dmangal said:
Hi all...first time poster, long time reader.

I've been experiencing some strange behavior on a Linux development
system (Red Hat 5)

Red Hat 5? That's over 10 years old.
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:
class FooClass
{
public:
void fooMethod();
private:
typedef struct {
int fooData;
} FooStruct;

You should throw out that typedef. Just define a struct with the name you
want instead of making a nameless struct and giving it an "alias" then.
};

FooClass.cpp:
#include "FooClass.hpp"

void FooClass::fooMethod()
{
FooStruct* foo;

Here, you define a pointer, but keep it uninitialized. It points to some
random - possibly non-existant - memory location.
foo->fooData = 5;

And here you try to write to that memory location. You first need to let your
pointer point to an object before using it.
 
D

dmangal

Thank you all for clearing things up for me. new-ing the pointer to
structure solved my problem.
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top