PDA

View Full Version : segmentation fault



newbie_grg
01-25-2003, 07:43 AM
hi,
i want to know why i get "segmentation fault" when i try to run exe file in linux using './' command after compiling using g++ command .what is the problem??

MethodMan
01-25-2003, 10:02 AM
I believe it gives you that error because there is a problem with memory in your program. Either allocating, or check your boundaries of your arrays, you may have gone over the limit of the array size somewhere.

newbie_grg
01-25-2003, 10:18 AM
thanx.
heres the code.Now wheres the fault ?? PLease let me know.



struct iode
{
char* name ;
};

int main(void)
{
iode* ptr;
cin >> ptr->name ;
}

Hammer
01-25-2003, 06:50 PM
name is a pointer that is uninitialised. You can either change it to be an array, or use dynamic memory allocation. Your choice :)

newbie_grg
01-26-2003, 04:21 AM
hi,
thanx for that but anywayz you say the name isnt initialized. how? its already initialised as pointer to char inside the struct.


struct iode
{
char* name ; // do you want me to initialise it as char* name[] ??
};
int main(void)
{
// now here i declare the object
iode* ptr ;
// and i access name
cin>> ptr->name; // do you mean to say i do
// ptr = new[] char* ??? or what??
}

thanx again dude.

Hammer
01-26-2003, 05:07 AM
>>its already initialised
No, it's not.

>>as pointer to char inside the struct.
Thats what type of pointer it is. To initialise it, you must point it at a specific memory location.

You could change your struct entry to have name like this:

>>char name[100];

That is the simplest route. It gives you an array of 100 bytes to store the name.