Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Anyone knows what can be cause of this problem?

Reply
Thread Tools

Anyone knows what can be cause of this problem?

 
 
Aing
Guest
Posts: n/a
 
      02-19-2004
Anyone knows what can be cause of this problem?

////////////////////////////////////////////////////////////
typedef struct _date_struct {
int date,month,year;
}date_struct;

Class Date {
private :
date_struct m_data;
public :
Date& operator=(const Date& );
//.. Other stuff
};

Date::~Date()
{
FreeStruct(&m_data);
}
Date& Date:perator = (const Date& SrcDate)
{
FreeStruct(&m_Data);
///...
return * this;
}

void Date::FreeStruct(Date& lpData) //<- here is the problem.
{
memset(lpData,0,sizeof(Date));
return;
}
/////////////////////////////////////////////////////

My application crashed because lpData was set to 0x0000014 or some
address around here. (I saw from DrWatSon log) But it doesn't happen
every I run the program. Anyone can tell me where this value from ? Is
there something wrong in my code?
 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      02-19-2004
Aing wrote:
>
> Anyone knows what can be cause of this problem?
>
> ////////////////////////////////////////////////////////////
> typedef struct _date_struct {
> int date,month,year;
> }date_struct;
>
> Class Date {
> private :
> date_struct m_data;
> public :
> Date& operator=(const Date& );
> //.. Other stuff
> };
>
> Date::~Date()
> {
> FreeStruct(&m_data);
> }
> Date& Date:perator = (const Date& SrcDate)
> {
> FreeStruct(&m_Data);
> ///...
> return * this;
> }
>
> void Date::FreeStruct(Date& lpData) //<- here is the problem.
> {
> memset(lpData,0,sizeof(Date));
> return;
> }
> /////////////////////////////////////////////////////
>


Since FreeStruct is a member function of Date, there is no need to
pass it something it can access anyway. There is also no
need to call FreeStruct from the destructor, since the object
is going to be deleted anyway. But this is not related to your problem.

> My application crashed because lpData was set to 0x0000014 or some
> address around here. (I saw from DrWatSon log) But it doesn't happen
> every I run the program. Anyone can tell me where this value from ? Is
> there something wrong in my code?


Definitly. But the problem is located somewhere else in your code.
What you see are just the symptoms. Watch out for array overflows, dangling
pointers etc.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      02-19-2004

"Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>> > My application crashed because lpData was set to 0x0000014 or some

> > address around here. (I saw from DrWatSon log) But it doesn't happen
> > every I run the program. Anyone can tell me where this value from ? Is
> > there something wrong in my code?

>
> Definitly. But the problem is located somewhere else in your code.
> What you see are just the symptoms. Watch out for array overflows, dangling
> pointers etc.


There seems to be some confusion.
FreeStruct takes a Date but it is passed the date_struct*. Even if he
somehow manages to bash it to compile with a cast, he's still memsetting
using the size of the containing object (Date). Depending on what "other stuff"
is in the class that he omitted form the listing, the results could be catastrophic.

 
Reply With Quote
 
lilburne
Guest
Posts: n/a
 
      02-19-2004


Ron Natalie wrote:
> "Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>
>>>>My application crashed because lpData was set to 0x0000014 or some
>>>
>>>address around here. (I saw from DrWatSon log) But it doesn't happen
>>>every I run the program. Anyone can tell me where this value from ? Is
>>>there something wrong in my code?

>>
>>Definitly. But the problem is located somewhere else in your code.
>>What you see are just the symptoms. Watch out for array overflows, dangling
>>pointers etc.

>
>
> There seems to be some confusion.
> FreeStruct takes a Date but it is passed the date_struct*. Even if he
> somehow manages to bash it to compile with a cast, he's still memsetting
> using the size of the containing object (Date). Depending on what "other stuff"
> is in the class that he omitted form the listing, the results could be catastrophic.
>


Even if he was memsetting Date he's likely to get into problems. The
idiom of initializing by memsetting C type structures seems to a MS
thing, which doesn't work with C++ structs and classes where you are
likely to zap the vtable pointer.

 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      02-19-2004
Ron Natalie wrote:
>
> "Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> >> > My application crashed because lpData was set to 0x0000014 or some
> > > address around here. (I saw from DrWatSon log) But it doesn't happen
> > > every I run the program. Anyone can tell me where this value from ? Is
> > > there something wrong in my code?

> >
> > Definitly. But the problem is located somewhere else in your code.
> > What you see are just the symptoms. Watch out for array overflows, dangling
> > pointers etc.

>
> There seems to be some confusion.
> FreeStruct takes a Date but it is passed the date_struct*.


I blamed this to not posting the actual source code but instead retyping
it in the newsreader.

> Even if he
> somehow manages to bash it to compile with a cast, he's still memsetting
> using the size of the containing object (Date). Depending on what "other stuff"
> is in the class that he omitted form the listing, the results could be catastrophic.



--
Karl Heinz Buchegger
(E-Mail Removed)
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      02-19-2004
lilburne wrote:
>
> Ron Natalie wrote:
> > "Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> >
> >>>>My application crashed because lpData was set to 0x0000014 or some
> >>>
> >>>address around here. (I saw from DrWatSon log) But it doesn't happen
> >>>every I run the program. Anyone can tell me where this value from ? Is
> >>>there something wrong in my code?
> >>
> >>Definitly. But the problem is located somewhere else in your code.
> >>What you see are just the symptoms. Watch out for array overflows, dangling
> >>pointers etc.

> >
> >
> > There seems to be some confusion.
> > FreeStruct takes a Date but it is passed the date_struct*. Even if he
> > somehow manages to bash it to compile with a cast, he's still memsetting
> > using the size of the containing object (Date). Depending on what "other stuff"
> > is in the class that he omitted form the listing, the results could be catastrophic.
> >

>
> Even if he was memsetting Date he's likely to get into problems. The
> idiom of initializing by memsetting C type structures seems to a MS
> thing, which doesn't work with C++ structs and classes where you are
> likely to zap the vtable pointer.


true.
But the OP used a POD struct.
memsetting to 0 should not be a problem in this case.


--
Karl Heinz Buchegger
(E-Mail Removed)
 
Reply With Quote
 
Peter Lee
Guest
Posts: n/a
 
      02-19-2004
>>>> Ron Natalie writes:

Ron> There seems to be some confusion. FreeStruct takes a Date
Ron> but it is passed the date_struct*. Even if he somehow
Ron> manages to bash it to compile with a cast, he's still
Ron> memsetting using the size of the containing object (Date).
Ron> Depending on what "other stuff" is in the class that he
Ron> omitted form the listing, the results could be catastrophic.

Even if m_data represented a Date object, he's passing a reference to
the address of that object. Seems like he would get a compile error
since the func expects (Date& lpData).

Also memset requires the address and would need to be
memset(&lpData,0,sizeof(Date));

since a reference is passed in.


Date::~Date()
{
FreeStruct(&m_data);
}
Date& Date:perator = (const Date& SrcDate)
{
FreeStruct(&m_Data);
///...
return * this;
}

void Date::FreeStruct(Date& lpData) //<- here is the problem.
{
memset(lpData,0,sizeof(Date));
return;
}
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      02-20-2004
Aing wrote:
> Anyone knows what can be cause of this problem?


For starters, try posting real code. The code that you posted doesn't
even compile, since you are trying to pass a 'Date*' argument as 'Date&'
parameter.

--
Best regards,
Andrey Tarasevich

 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      02-20-2004
Karl Heinz Buchegger wrote:
>> ...
>> Even if he was memsetting Date he's likely to get into problems. The
>> idiom of initializing by memsetting C type structures seems to a MS
>> thing, which doesn't work with C++ structs and classes where you are
>> likely to zap the vtable pointer.

>
> true.
> But the OP used a POD struct.
> memsetting to 0 should not be a problem in this case.
> ...


No, it is still a problem. You must be thinking about 'memcpy', not
about 'memset'. Strictly speaking, 'memset' cannot be used to
zero-initialize 'int' objects. And 'int' objects is exactly what the OP
is storing in his/her POD struct called 'date_struct'.

--
Best regards,
Andrey Tarasevich

 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      02-20-2004
Andrey Tarasevich wrote in news:(E-Mail Removed):

> Karl Heinz Buchegger wrote:
>>> ...
>>> Even if he was memsetting Date he's likely to get into problems. The
>>> idiom of initializing by memsetting C type structures seems to a MS
>>> thing, which doesn't work with C++ structs and classes where you are
>>> likely to zap the vtable pointer.

>>
>> true.
>> But the OP used a POD struct.
>> memsetting to 0 should not be a problem in this case.
>> ...

>
> No, it is still a problem. You must be thinking about 'memcpy', not
> about 'memset'. Strictly speaking, 'memset' cannot be used to
> zero-initialize 'int' objects. And 'int' objects is exactly what the OP
> is storing in his/her POD struct called 'date_struct'.
>


I was under the impression that all C++ signed types have 1 of
3 formats 2s-compliment, 1s-compliment or signed magnitude, all of
these representation's have value 0 when all bits set to 0,
so why doesn't:

int i;
memset( &i, 0, sizeof( int ) );

work ?

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help! Anyone knows online software(phone) service (with a chosen area code) that can receive real phone calls online honestperson6@hotmail.com Computer Information 6 05-20-2006 03:11 PM
Help! Anyone knows online software(phone) service (with a chosen area code) that can receive real phone calls online honestperson6@hotmail.com Computer Support 6 05-20-2006 03:11 PM
Help! Anyone knows online phone service (with a chosen area code) that can receive real phone calls online honestperson6@hotmail.com VOIP 3 05-20-2006 03:00 PM
will all these messages cause a problem . I am a new subscriber and my computer is downloading 100,000 messages. Will this cause any kind of a problem with my ability to store other items?? Camille White Camille White Computer Support 9 11-08-2004 01:13 AM
Re: Does anyone knows Ctec Academy? ktavera MCSD 3 05-05-2004 08:45 PM



Advertisments