473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

zero memory

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.
Apr 5 '07 #1
25 13831
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.
Huh?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 5 '07 #2
Christopher Pisz wrote:
What is the C++ way to zero out memory after calling operator new on
a struct?
Why do you want to? What problem does this solve?
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.
If it's a POD, you can use memcpy to set the whole thing to
all-bits-zero. That's not always what you want, and frankly is usually
a poor design.

Brian
Apr 5 '07 #3
Victor Bazarov wrote:
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.


Huh?
I'm guessing but Mr Pisz could probably benefit from knowing what this does:

struct A
{
int X;
int Y;
char Z[30];
// other POD members ...
};

A x = A();
Apr 5 '07 #4
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:ev**********@news.datemas.de...
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.

Huh?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

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.
}
Apr 5 '07 #5
Christopher Pisz wrote:
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:ev**********@news.datemas.de...
>>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.
Apr 6 '07 #6
Christopher Pisz wrote:
>

typedef struct _MYSTRUCT
Ill formed program. Any identifier with a leading underscore, followed
by an uppercase letter is reserved to the implementation.
{
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.
}
Also, as someone else pointed out, this is essentially C, except for the
use of new.
Apr 6 '07 #7
Christopher Pisz wrote:
....
Foo()
{
PMYSTRUCT = new MYSTRUCT;
// doing it the hard way
>
// now set all members of the struct to 0
PMYSTRUCT = new MYSTRUCT(); // this will initialize all to 0
....
}
....
Apr 6 '07 #8
On 5 Apr 2007 22:06:14 GMT, "Default User" <de***********@yahoo.com>
wrote in comp.lang.c++:
Christopher Pisz wrote:
What is the C++ way to zero out memory after calling operator new on
a struct?

Why do you want to? What problem does this solve?
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.

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.
all-bits-zero. That's not always what you want, and frankly is usually
a poor design.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Apr 6 '07 #9
ajk
On Thu, 5 Apr 2007 16:49:52 -0500, "Christopher Pisz"
<so*****@somewhere.netwrote:
>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

Apr 6 '07 #10
ajk wrote:
On Thu, 5 Apr 2007 16:49:52 -0500, "Christopher Pisz"
<so*****@somewhere.netwrote:

>>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.
Apr 6 '07 #11
ajk wrote:
On Thu, 5 Apr 2007 16:49:52 -0500, "Christopher Pisz"
<so*****@somewhere.netwrote:
>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.
Apr 6 '07 #12
On Thu, 5 Apr 2007 16:49:52 -0500, "Christopher Pisz" <so*****@somewhere.net>
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
Apr 6 '07 #13

"Ian Collins" <ia******@hotmail.comwrote in message
news:57**************@mid.individual.net...
Christopher Pisz wrote:
>"Victor Bazarov" <v.********@comAcast.netwrote in message
news:ev**********@news.datemas.de...
>>>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.
Apr 6 '07 #14
Jack Klein wrote:
On 5 Apr 2007 22:06:14 GMT, "Default User" <de***********@yahoo.com>
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
Apr 6 '07 #15
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.
Apr 7 '07 #16
ajk
On Fri, 06 Apr 2007 05:39:34 -0700, Gianni Mariani
<gi*******@mariani.wswrote:
>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
Apr 7 '07 #17
ajk wrote:
On Fri, 06 Apr 2007 05:39:34 -0700, Gianni Mariani
<gi*******@mariani.wswrote:
....
>>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.

Apr 7 '07 #18
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();
}
Apr 7 '07 #19
ajk
On Fri, 06 Apr 2007 22:45:16 -0700, Gianni Mariani
<gi*******@mariani.wswrote:
>ajk wrote:
>On Fri, 06 Apr 2007 05:39:34 -0700, Gianni Mariani
<gi*******@mariani.wswrote:
...
>>>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.
Apr 8 '07 #20
ajk wrote:
On Fri, 06 Apr 2007 22:45:16 -0700, Gianni Mariani
<gi*******@mariani.wswrote:

>>ajk wrote:
>>>On Fri, 06 Apr 2007 05:39:34 -0700, Gianni Mariani
<gi*******@mariani.wswrote:

...
>>>>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 me see:

PMYSTRUCT mys = InitObj();

Oh yeah.... not very clear.

How about this:

PMYSTRUCT mys = NewInitializedObject(); // creates a new zero MYSTRUCT
>
>>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
Is that a nervous laugh ? Or perhaps one of ignorance ? Certainly it
shows a lack of being able to form a coherent argument.

Bugs in calls to memset or memcpy are high on the list of ones that have
worn out their welcome. Eliminating calls to these in mainline code is
a good thing. I don't see how you can argue otherwise if you have had
enough experience.
>
>>
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.
Are you telling me that you can't extend this design to somthing that is
allocated statically or automatically ? If you can't, look at the
previous post of mine on this thread.

Eliminating repetitive code helps build a solid, easier to maintain code
base. C++ does have some very neat features (some unintentional) that
allows a good programmer to remove alot of repetitive, error prone code.

Admitedly, that one probably (not tested of VC7.X) breaks some buggy
compilers but it shows you what you should be expecting from the language.
Apr 8 '07 #21
On Apr 6, 8:39 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
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 amemsetcall and note that the code will work for POD
types as well as non POD types.
If in the class there is a C-character array, how does one initialize
such an array to 0's?

tia
Anders.

Apr 9 '07 #22
On Apr 8, 8:14 pm, "bark" <ander...@gmail.comwrote:
On Apr 6, 8:39 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
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 amemsetcall and note that the code will work for POD
types as well as non POD types.

If in the class there is a C-character array, how does one initialize
such an array to 0's?

tia
Anders.
In the constructor, use memset()?

Apr 9 '07 #23
bark wrote:
On Apr 6, 8:39 pm, Gianni Mariani <gi3nos...@mariani.wswrote:

>>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 amemsetcall and note that the code will work for POD
types as well as non POD types.


If in the class there is a C-character array, how does one initialize
such an array to 0's?
The standard requires a conforming compiler to do that. I suspect a
compiler will do whatever is best for the platform.

This code is safe, in the sense that if you call memset on just any
class, you're likely to run into some interesting problems. This code
will only initialize to zero those types that are POD. It means that if
one day you change MYSTRUCT to have a default constructor, the code
above will do what you expect.

Apr 9 '07 #24
Siddhartha Gandhi wrote:
On Apr 8, 8:14 pm, "bark" <ander...@gmail.comwrote:
>>On Apr 6, 8:39 pm, Gianni Mariani <gi3nos...@mariani.wswrote:

>>>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 amemsetcall and note that the code will work for POD
types as well as non POD types.

If in the class there is a C-character array, how does one initialize
such an array to 0's?

tia
Anders.


In the constructor, use memset()?
That's the point, the code above does not need to use memset.

i.e.
struct A { int a; char b[333]; };
A * foo() { return new A(); } // note the () after the A

The A object created above is guarenteed to be initialized by the C++
standard.

GCC creates the following code:

..globl _Z3foov
.type _Z3foov, @function
_Z3foov:
..LFB2:
pushl %ebp
..LCFI0:
movl %esp, %ebp
..LCFI1:
pushl %ebx
..LCFI2:
subl $16, %esp
..LCFI3:
pushl $340
..LCFI4:
call _Znwj
movl %eax, %ebx
addl $12, %esp
pushl $340
pushl $0
pushl %eax
call memset
movl %ebx, %eax
movl -4(%ebp), %ebx
leave
ret

Note the call to memset by the compiler.
The code below is just a simplification to make it easier to use...

struct NewInitializedObject
{
// conversion operator does automagic detection
// of which type to create.
template <typename T>
operator T * ()
{
return new T();
}
};

// usage
PMYSTRUCT * mys = NewInitializedObject();

mys points to a new MYSTRUCT correctly initialized.


Apr 9 '07 #25
On Apr 9, 10:13 am, Gianni Mariani <gi3nos...@mariani.wswrote:
Siddhartha Gandhi wrote:
On Apr 8, 8:14 pm, "bark" <ander...@gmail.comwrote:
>On Apr 6, 8:39 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
>>I just thought of yet another way - this one will create a default
constructed orzeroinitialized 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 amemsetcall and note that the code will work for POD
types as well as non POD types.
>If in the class there is a C-character array, how does one initialize
such an array to 0's?
>tia
Anders.
In the constructor, use memset()?

That's the point, the code above does not need to use memset.

i.e.
struct A { int a; char b[333]; };

A * foo() { return new A(); } // note the () after the A

The A object created above is guarenteed to be initialized by the C++
standard.

GCC creates the following code:

.globl _Z3foov
.type _Z3foov, @function
_Z3foov:
.LFB2:
pushl %ebp
.LCFI0:
movl %esp, %ebp
.LCFI1:
pushl %ebx
.LCFI2:
subl $16, %esp
.LCFI3:
pushl $340
.LCFI4:
call _Znwj
movl %eax, %ebx
addl $12, %esp
pushl $340
pushl $0
pushl %eax
call memset
movl %ebx, %eax
movl -4(%ebp), %ebx
leave
ret

Note the call to memset by the compiler.

The code below is just a simplification to make it easier to use...

struct NewInitializedObject
{
// conversion operator does automagic detection
// of which type to create.
template <typename T>
operator T * ()
{
return new T();
}

};

// usage
PMYSTRUCT * mys = NewInitializedObject();

mys points to a new MYSTRUCT correctly initialized.
thanks, i'll try this out.
br/anders

Apr 9 '07 #26

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gary Harvey | last post by:
I have a data intensive program that requires all data to be present in memory. I keep running out of memory at about 2G whenever I run my program. I tried using a 64 bit version of Perl and hit...
13
by: Michael B Allen | last post by:
Hi, I've tried to write the *simplest* memory allocator possible. I think it would be useful in many cases such as allocating memory on stack as a poor man's garbage collection perhaps. I was...
18
by: jacob navia | last post by:
In C, we have read-only memory (const), read/write memory (normal data), and write only memory. Let's look at the third one in more detail. Write only memory is a piece of RAM that can only...
1
by: Brandon Langley | last post by:
I have this structure that I am using in conjunction with NetLocalGroupAddMembers: public struct LOCALGROUP_MEMBERS_INFO_3 { public string lgrmi3_domainandname; } I am having failures...
0
by: Brian Keating | last post by:
hi there i've a test program that creates a treeview and destroys it over and over, i keep track of the gdi object count for the process and see if they are ok. However when i switch on...
15
by: Tomás | last post by:
Is the following fully legal and fully portable for all the unsigned types? The aim of the function is to take an array by reference and set each element's value to zero. #include <... ...
2
by: =?Utf-8?B?SXJmYW4=?= | last post by:
Hello, It may be a repeated question but I don't find the solution to the situation that I encounter in it. My application is monitoring another application that is built in VB6. The...
11
by: michelqa | last post by:
Hello, I can retrieve column text from a ListView in another process but I cant figure out how to access to structure elements (LVCOLUMN) <code> //Handle variable is a valid ListView handle ...
66
by: Why Tea | last post by:
typedef struct some_struct { int i; short k, int m; char s; } some_struct_t; Assuming 16 bit or 32-bit alignment, can I assume that s always gets 4 or 8 bytes of allocation due to padding
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.