Connecting Tech Pros Worldwide Forums | Help | Site Map

Memory inizialization 2

mcassiani
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi, again

I have readed the answare, I don't like it.
What do you think about this solutions? obiusly it works
only for dynamic allocated data.

class{
int a1;
..
...
int a100
char v1[100]
...
char v100[100]

void *operator new(size_t size){
void *ptr=malloc(size);
memset(ptr,0,size);
return ptr;
}
}

MarcoC



Siemel Naran
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Memory inizialization 2


"mcassiani" <mcassiani@virgilio.it> wrote in message news:rrEAc.492860
[color=blue]
> I have readed the answare, I don't like it.
> What do you think about this solutions? obiusly it works
> only for dynamic allocated data.
>
> class{
> int a1;
> ..
> ..
> int a100
> char v1[100]
> ...
> char v100[100]
>
> void *operator new(size_t size){
> void *ptr=malloc(size);
> memset(ptr,0,size);
> return ptr;
> }
> }[/color]

Remember to provide operator delete too as the system operator delete might
not call free.

It works for objects created on the heap, but what about objects created on
the stack?



tom_usenet
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Memory inizialization 2


On Fri, 18 Jun 2004 16:07:51 GMT, "mcassiani" <mcassiani@virgilio.it>
wrote:
[color=blue]
>Hi, again
>
>I have readed the answare, I don't like it.
>What do you think about this solutions? obiusly it works
>only for dynamic allocated data.
>
>class{
> int a1;
> ..
>..
> int a100
> char v1[100]
> ...
> char v100[100]
>
> void *operator new(size_t size){
> void *ptr=malloc(size);[/color]

Instead:
void* ptr = ::operator new(size);
[color=blue]
> memset(ptr,0,size);
> return ptr;
> }[/color]

What about operator new[]?
[color=blue]
>}[/color]

Again, setting all bytes to 0 won't necessarily work for all POD
types, particularly for doubles (and theoretically for pointers too).
How about this:

class test
{
private:
struct data_t
{
int mem1;
double mem2;
char mem3;
double mem4[50];
} data;
public:
test()
:data(data_t()) //zero initialises data
{
}
};

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
mcassiani
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Memory inizialization 2



"tom_usenet" <tom_usenet@hotmail.com> ha scritto nel messaggio
news:4486d0hid5llmg5kj83d6ojkqg37ol36p9@4ax.com...[color=blue]
> On Fri, 18 Jun 2004 16:07:51 GMT, "mcassiani" <mcassiani@virgilio.it>
> wrote:
>[color=green]
> >Hi, again
> >
> >I have readed the answare, I don't like it.
> >What do you think about this solutions? obiusly it works
> >only for dynamic allocated data.
> >
> >class{
> > int a1;
> > ..
> >..
> > int a100
> > char v1[100]
> > ...
> > char v100[100]
> >
> > void *operator new(size_t size){
> > void *ptr=malloc(size);[/color]
>
> Instead:
> void* ptr = ::operator new(size);
>[color=green]
> > memset(ptr,0,size);
> > return ptr;
> > }[/color]
>
> What about operator new[]?
>[color=green]
> >}[/color]
>
> Again, setting all bytes to 0 won't necessarily work for all POD
> types, particularly for doubles (and theoretically for pointers too).
> How about this:
>
> class test
> {
> private:
> struct data_t
> {
> int mem1;
> double mem2;
> char mem3;
> double mem4[50];
> } data;
> public:
> test()
> :data(data_t()) //zero initialises data
> {
> }
> };
>
> Tom
> --
> C++ FAQ: http://www.parashift.com/c++-faq-lite/
> C FAQ: http://www.eskimo.com/~scs/C-faq/top.html[/color]

I have tested the binary double composition for x86 processor and HP-PARISC
and 0 init
work well.



tom_usenet
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Memory inizialization 2


On Sat, 19 Jun 2004 07:33:47 GMT, "mcassiani" <mcassiani@virgilio.it>
wrote:
[color=blue]
>I have tested the binary double composition for x86 processor and HP-PARISC
>and 0 init
>work well.[/color]

Yes, in practice it works on many platforms, but if you leave the bug
in some code that's meant to be portable, it may come back to bite
you. If it's not meant to be portable (and the non-portable parts are
clearly marked), you may be ok.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Closed Thread


Similar C / C++ bytes