Memory inizialization 2

M

mcassiani

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
 
S

Siemel Naran

mcassiani said:
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;
}
}

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

tom_usenet

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

Instead:
void* ptr = ::eek:perator new(size);
memset(ptr,0,size);
return ptr;
}

What about operator new[]?

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
 
M

mcassiani

tom_usenet said:
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);

Instead:
void* ptr = ::eek:perator new(size);
memset(ptr,0,size);
return ptr;
}

What about operator new[]?

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

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

tom_usenet

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

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
 

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

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,474
Latest member
VivianStuk

Latest Threads

Top