Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

zero memory

 
 
Gianni Mariani
Guest
Posts: n/a
 
      04-08-2007
ajk wrote:
> On Fri, 06 Apr 2007 22:45:16 -0700, Gianni Mariani
> <> wrote:
>
>
>>ajk wrote:
>>
>>>On Fri, 06 Apr 2007 05:39:34 -0700, Gianni Mariani
>>><> 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 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.
 
Reply With Quote
 
 
 
 
bark
Guest
Posts: n/a
 
      04-09-2007
On Apr 6, 8:39 pm, Gianni Mariani <gi3nos...@mariani.ws> wrote:

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

 
Reply With Quote
 
 
 
 
Siddhartha Gandhi
Guest
Posts: n/a
 
      04-09-2007
On Apr 8, 8:14 pm, "bark" <ander...@gmail.com> wrote:
> On Apr 6, 8:39 pm, Gianni Mariani <gi3nos...@mariani.ws> wrote:
>
>
>
> > 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()?

 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      04-09-2007
bark wrote:
> On Apr 6, 8:39 pm, Gianni Mariani <gi3nos...@mariani.ws> wrote:
>
>
>>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.

 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      04-09-2007
Siddhartha Gandhi wrote:
> On Apr 8, 8:14 pm, "bark" <ander...@gmail.com> wrote:
>
>>On Apr 6, 8:39 pm, Gianni Mariani <gi3nos...@mariani.ws> wrote:
>>
>>
>>
>>
>>>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.




 
Reply With Quote
 
bark
Guest
Posts: n/a
 
      04-09-2007
On Apr 9, 10:13 am, Gianni Mariani <gi3nos...@mariani.ws> wrote:
> Siddhartha Gandhi wrote:
> > On Apr 8, 8:14 pm, "bark" <ander...@gmail.com> wrote:

>
> >>On Apr 6, 8:39 pm, Gianni Mariani <gi3nos...@mariani.ws> wrote:

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

 
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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57