Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > zero memory

Reply
Thread Tools

zero memory

 
 
Gianni Mariani
Guest
Posts: n/a
 
      04-06-2007
ajk wrote:
> On Thu, 5 Apr 2007 16:49:52 -0500, "Christopher Pisz"
> <(E-Mail Removed)> wrote:
>
>
>>What is the C++ way to zero out memory after calling operator new on a
>>struct?
>>
>>A constructor is not possible in this case nor is a class, because the
>>people using my code c-stlye cast a pointer to the first member of the
>>struct to a pointer to the entrire struct later on in the code.
>>

>
>
> so memset() should do the trick
>
> "the c++ way" would be to have a class and to initialize it in the
> ctor
>
> e.g.
>
> class CMYSTRUCT : public MYSTRUCT
> {
> public:
> CMYSTRUCT() {
> memset(&overlapped,0,sizeof(overlapped)); mamajama=0; next=NULL; }


You really want to avoid using memset. It will bite you one day.

Try this:

CMYSTRUCT() : MYSTRUCT( MYSTRUCT() ) {}

But your problem does not end there

> };
>
> Foo()
> {
> PMYSTRUCT = new CMYSTRUCT;


In this case, you're calling new on CMYSTRUCT and I expect that you'll
call delete on a MYSTRUCT. That's undefined. Bad things will happen if
don't modify that habbit.

> }
>
> or make the MYSTRUCT a class


MYSTRUCT is a class.



I just thought of yet another way - this one will create a default
constructed or zero initialized POD object depending on what type of
pointer you're trying to assign it to.

struct InitObj
{
template <typename T>
operator T * ()
{
return new T();
}
};

// usage - template automagically figures out which type to new
PMYSTRUCT * mys = InitObj();

int * z = InitObj();


Note the lack of a memset call and note that the code will work for POD
types as well as non POD types.
 
Reply With Quote
 
 
 
 
Jacek Dziedzic
Guest
Posts: n/a
 
      04-06-2007
ajk wrote:
> On Thu, 5 Apr 2007 16:49:52 -0500, "Christopher Pisz"
> <(E-Mail Removed)> wrote:
>
>> What is the C++ way to zero out memory after calling operator new on a
>> struct?
>>
>> A constructor is not possible in this case nor is a class, because the
>> people using my code c-stlye cast a pointer to the first member of the
>> struct to a pointer to the entrire struct later on in the code.
>>

>
> so memset() should do the trick
>
> "the c++ way" would be to have a class and to initialize it in the
> ctor
>
> e.g.
>
> class CMYSTRUCT : public MYSTRUCT
> {
> public:
> CMYSTRUCT() {
> memset(&overlapped,0,sizeof(overlapped)); mamajama=0; next=NULL; }
> };
>
> Foo()
> {
> PMYSTRUCT = new CMYSTRUCT;
> }
>
> or make the MYSTRUCT a class


Wait a second. Doesn't adding a c'tor make it non-POD and
hence unsuitable for a memset()?

- J.
 
Reply With Quote
 
 
 
 
Dave Rahardja
Guest
Posts: n/a
 
      04-06-2007
On Thu, 5 Apr 2007 16:49:52 -0500, "Christopher Pisz" <(E-Mail Removed)>
wrote:

>What is the C++ way to zero out memory after calling operator new on a
>struct?
>
>A constructor is not possible in this case nor is a class, because the
>people using my code c-stlye cast a pointer to the first member of the
>struct to a pointer to the entrire struct later on in the code.
>


Since you're essentially doomed to maintaining this code in C anyway, just
forget about C++ and treat this as a C struct.

-dr
 
Reply With Quote
 
Christopher Pisz
Guest
Posts: n/a
 
      04-06-2007

"Ian Collins" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Christopher Pisz wrote:
>> "Victor Bazarov" <(E-Mail Removed)> wrote in message
>> news:ev3rft$9f1$(E-Mail Removed)...
>>
>>>Christopher Pisz wrote:
>>>
>>>>What is the C++ way to zero out memory after calling operator new on a
>>>>struct?
>>>
>>>To zero out what memory? What does "calling operator new on a struct"
>>>mean? Could you post code instead of English?
>>>
>>>
>>>>A constructor is not possible in this case nor is a class, because the
>>>>people using my code c-stlye cast a pointer to the first member of the
>>>>struct to a pointer to the entrire struct later on in the code.
>>>

>>
>> typedef struct _MYSTRUCT
>> {
>> OVERLAPPED overlapped;
>> int mamajama;
>> _MYSTRUCT * next;
>> } *PMYSTRUCT, MYSTRUCT;
>>
>> Foo()
>> {
>> PMYSTRUCT = new MYSTRUCT;
>>
>> // now set all members of the struct to 0
>> }
>>
>>
>> // later, they do this:
>> SomeFunction(OVERLAPPED * overlapped)
>> {
>> PMYSTRUCT happystruct = (PMYSTRUCT) overlapped;
>> int x = happystruct->mamajama;
>>
>> //etc etc.
>> }
>>

> It looks like someone is very confused between C and C++ idioms.
>
> --
> Ian Collins.




It isn't me. I know darn well how I would do it if I didn't have 3rd party
limitations. The problem is that the c style cast, that they force me to
use, in turn forces the structure.


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      04-06-2007
Jack Klein wrote:

> On 5 Apr 2007 22:06:14 GMT, "Default User" <(E-Mail Removed)>
> wrote in comp.lang.c++:


> > If it's a POD, you can use memcpy to set the whole thing to

> ^^^^^^
>
> I know you know that should be memset(). Silly typo.


Man! Yes, of course.

I guess my only defense is that I rarely use memset(), even in C.




Brian
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      04-07-2007
Jacek Dziedzic wrote:
> ajk wrote:

....
>
> Wait a second. Doesn't adding a c'tor make it non-POD and
> hence unsuitable for a memset()?


Yes, but that's not what is happening. memset is being called on a
member which is a POD.
 
Reply With Quote
 
ajk
Guest
Posts: n/a
 
      04-07-2007
On Fri, 06 Apr 2007 05:39:34 -0700, Gianni Mariani
<(E-Mail Removed)> wrote:

>In this case, you're calling new on CMYSTRUCT and I expect that you'll
>call delete on a MYSTRUCT. That's undefined. Bad things will happen if
>don't modify that habbit.


why do you expect that? ok i have not added any virtual dtor as was
not providing a full class. just showing the principle.

>
>> }
>>
>> or make the MYSTRUCT a class

>
>MYSTRUCT is a class.
>


technically you are right, although what I meant was to make it a
"real" class with ctor/dtor etc.

>
>
>I just thought of yet another way - this one will create a default
>constructed or zero initialized POD object depending on what type of
>pointer you're trying to assign it to.
>
>struct InitObj
>{
> template <typename T>
> operator T * ()
> {
> return new T();
> }
>};
>
>// usage - template automagically figures out which type to new
>PMYSTRUCT * mys = InitObj();
>
>int * z = InitObj();
>
>
>Note the lack of a memset call and note that the code will work for POD
> types as well as non POD types.


ok that's in a way elegant, but a bit difficult for maintenance
programmers to troubleshoot
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      04-07-2007
ajk wrote:
> On Fri, 06 Apr 2007 05:39:34 -0700, Gianni Mariani
> <(E-Mail Removed)> wrote:

....
>>Note the lack of a memset call and note that the code will work for POD
>> types as well as non POD types.

>
>
> ok that's in a way elegant, but a bit difficult for maintenance
> programmers to troubleshoot


That's one serious cop-out. Arguing to have mediocre engineers is an
unsupportable argument.


Let's see which one is more maintainable ....
..................
code in common header file....

struct InitObj
{
template <typename T>
operator T * ()
{
return new T();
}
};
................


................
application code.....
PMYSTRUCT mys = InitObj();
................

vs - in every instance ...

memset(&overlapped,0,sizeof(overlapped)) + other mumbo jumbo with all
kinds of potential for programmer errors.



I know what I would like my engineers to maintain.



 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      04-07-2007
Christopher Pisz wrote:
> What is the C++ way to zero out memory after calling operator new on a
> struct?
>
> A constructor is not possible in this case nor is a class, because the
> people using my code c-stlye cast a pointer to the first member of the
> struct to a pointer to the entrire struct later on in the code.
>
>


Note that there are a number of win32 structs that must be initialized
not only with zeros but with a member set to the size of the struct.
e.g. IMAGEHLP_SYMBOL64

The code below (checked on gcc) should automatically initialize an
entire struct to zeros and insert the right value in the "sizeofstruct"
member if it exists.

I did have a problem with visual studio on this technique a while back,
let's hope things have improved.


// This is used to initialize Win32 structs that contain a
// "sizeofstruct" structure.

struct NoMemb { char a[1]; };
struct Memb_SizeOfStruct { char a[2]; };
struct Memb_sizeofstruct { char a[3]; };

template <int w_val>
struct InitSizeOf;

template <>
struct InitSizeOf< sizeof( NoMemb ) >
{
template <typename U>
inline static void ObjInitSel( U & obj )
{
}
};

template <>
struct InitSizeOf< sizeof( Memb_SizeOfStruct ) >
{
template <typename U>
inline static void ObjInitSel( U & obj )
{
obj.SizeOfStruct = sizeof( obj );
}
};

template <>
struct InitSizeOf< sizeof( Memb_sizeofstruct ) >
{
template <typename U>
inline static void ObjInitSel( U & obj )
{
obj.sizeofstruct = sizeof( obj );
}
};

template <typename T>
struct InitObject
{
private:

struct xA {};
struct xB : xA {};

template <int w_size>
struct Detect
{
};

public:

template <typename U>
inline static Memb_sizeofstruct ObjInitSel(
U & obj, xB * b, Detect< sizeof(&U::sizeofstruct) > * = 0
);

template <typename U>
inline static Memb_SizeOfStruct ObjInitSel(
U & obj, xB * b, Detect< sizeof(&U::SizeOfStruct) > * = 0
);

template <typename U>
inline static NoMemb ObjInitSel( U & obj, xA * a );

inline static void ObjInit( T & obj )
{
typedef xB * bp;

InitSizeOf<sizeof( ObjInitSel(obj, bp()))>::ObjInitSel( obj );
}
};

struct InitStruct
{

template <typename T>
inline operator T ()
{
T obj = T();
InitObject<T>().ObjInit( obj );

return obj;
}

template <typename T>
inline operator T * ()
{
T * obj = new T();

InitObject<T>().ObjInit( * obj );

return obj;
}

};


/////////// test code

struct A
{
int a;
char x[10];
};


struct B
{
int SizeOfStruct;
char x[15];
};


struct C
{
int sizeofstruct;
char x[22];
};

B Bfoo()
{
return InitStruct();
}

C Cfoo()
{
return InitStruct();
}

A Afoo()
{
return InitStruct();
}


int main()
{

// make dynamically allocated version
C * c = InitStruct();

delete c;

Bfoo();
Cfoo();
Afoo();
}
 
Reply With Quote
 
ajk
Guest
Posts: n/a
 
      04-08-2007
On Fri, 06 Apr 2007 22:45:16 -0700, Gianni Mariani
<(E-Mail Removed)> wrote:

>ajk wrote:
>> On Fri, 06 Apr 2007 05:39:34 -0700, Gianni Mariani
>> <(E-Mail Removed)> wrote:

>...
>>>Note the lack of a memset call and note that the code will work for POD
>>> types as well as non POD types.

>>
>>
>> ok that's in a way elegant, but a bit difficult for maintenance
>> programmers to troubleshoot

>
>That's one serious cop-out. Arguing to have mediocre engineers is an
>unsupportable argument.
>


no, its not an unsuportable argument - coding so that its clear is
what its about.

>
>Let's see which one is more maintainable ....
>.................
>code in common header file....
>
>struct InitObj
>{
> template <typename T>
> operator T * ()
> {
> return new T();
> }
>};
>...............
>
>
>...............
>application code.....
>PMYSTRUCT mys = InitObj();
>...............
>
>vs - in every instance ...
>
>memset(&overlapped,0,sizeof(overlapped)) + other mumbo jumbo with all
>kinds of potential for programmer errors.
>


mumbo jumbo? lol whatever

>
>
>I know what I would like my engineers to maintain.
>
>


your solution has two drawbacks as it allocates memory on heap:
it requires whoever uses it to know that memory is allocated second
second allocating memory on heap just because you want to initialize
it isn't effective.


 
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
zero up memory Mark C Programming 44 03-14-2012 03:17 PM
Can you set a class instance's attributes to zero by setting the instance to zero? Gerard Flanagan Python 3 11-19-2005 06:58 PM
Wireless Zero Configuration Memory Leak?? =?Utf-8?B?Umlja3NjaHVsdHox?= Wireless Networking 3 01-19-2005 11:26 PM
Doubles and zero/negative zero Christopher Benson-Manica C Programming 4 07-01-2004 05:44 PM
memset all bits to zero will let float/double to zero? Zhiqiang Ye C Programming 53 06-28-2004 01:23 PM



Advertisments