zero memory

  • Thread starter Christopher Pisz
  • Start date
G

Gianni Mariani

ajk said:
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
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.
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.
 
B

bark

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.
 
G

Gianni Mariani

bark said:
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.
 
G

Gianni Mariani

Siddhartha said:
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.
 
B

bark

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top