Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,261 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,510 people online right now. Registration is fast and FREE... Join Now!




segmentation fault

 
Reply to this topicStart new topic

segmentation fault, segmentation fault

srishekh
10 Dec, 2006 - 11:02 PM
Post #1

New D.I.C Head
*

Joined: 29 Sep, 2006
Posts: 10


My Contributions
Hello every body,

I have a structure having string object, and I have created
structure variable p, and initialized values.
Now before filling new values i want to clear it off using memset, but it gives me seg fault error.
Any solutions for me.

thanks in advance

code:
CODE

#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

typedef enum
{
   UNKNOWN_AVP = -1,
   FIXED_AVP = 0,
   REQUIRED_AVP = 1,
   OPTIONAL_AVP = 2
}DICT_BASED_AVP_TYPES;


struct ABC
{
int a;
string  s1;
DICT_BASED_AVP_TYPES val;

};


int main()
{

struct ABC p,q;
p.a= 100;
p.s1=string("gfdhgf");
p.val=(DICT_BASED_AVP_TYPES)1;
memset(&p,0,sizeof(struct ABC));
cout<<"end"<<endl;
}

User is offlineProfile CardPM
+Quote Post

horace
RE: Segmentation Fault
11 Dec, 2006 - 12:46 AM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
you structure contains string object p, i.e.
CODE

p.s1=string("gfdhgf");

you overwrite p with 0's
CODE

memset(&p,0,sizeof(struct ABC));


at the end of the compound statement an attempt will be made to call the destructor for the string object p.s1 and you get a segmentation error.
User is offlineProfile CardPM
+Quote Post

srishekh
RE: Segmentation Fault
11 Dec, 2006 - 01:09 AM
Post #3

New D.I.C Head
*

Joined: 29 Sep, 2006
Posts: 10


My Contributions
Actualy that statement is required in a loop.
So, before filling new values i need to clear the previous values.
How do I do that ?

User is offlineProfile CardPM
+Quote Post

horace
RE: Segmentation Fault
11 Dec, 2006 - 10:59 PM
Post #4

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions

not sure what you mean by "before filling new values i need to clear the previous values". When you assign new values the previous ones are overwritten.
User is offlineProfile CardPM
+Quote Post

srishekh
RE: Segmentation Fault
13 Dec, 2006 - 07:54 PM
Post #5

New D.I.C Head
*

Joined: 29 Sep, 2006
Posts: 10


My Contributions
ya , it will be overwritten.
But I want to make sure that it is cleared completely
before filling new values.
Any ways ?


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:00PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month