Re: C++ Objects, Stack or Heap? [Noob question]

From: JKop (NULL_at_NULL.NULL)
Date: 09/19/04


Date: Sun, 19 Sep 2004 19:23:44 GMT

Dave Townsend posted:

>
> >
>> While I, being a proficient C++ programmer, do:
>>
>> FIND_DATA find_data = FIND_DATA();
>>
>>
>
>
> Its a bit early on a Sunday for me to remember the distinction between
> default initialization and the other sort, but the line of code
> above, isn't it the same as
>
> FIND_DATA find_data;
>
> or for that matter....
> FIND_DATA find_data( FIND_DATA() );

BULL***.

Okay here goes:

int i;
char* p_blah;

Right now those two variables contain white noise, no particular value. A
piece of memory was just allocated, it wasn't zeroed.

struct Poo
{
            int i;
            char* p_blah;
};

Poo black;

Right now, "black.i" and "black.p_blah" contain white noise.

Here's how to get things set to their default value:

int i = int();
char* p_blah = char*();

Poo black = Poo();

Note that the above are all POD types.

When you're dealing with classes, then:

std::string k;
std::string k = string();

are identical, because the Constructor takes over.

 
> The original requires that you have a assignment operator, whereas
> the second form does not. ( Well, I suppose the last one requires you
> have a copy constructor).

As regards assignment operator... BULL***, it's not involved at all.

std::string k = std::string();

is identical in every way to:

std::string k( std::string() );

As regards Copy Constructor, you're correct. If there's no copy contructor
defined, then the miranda one takes over.
If there's a copy constructor defined but it's private, then it doesn't
work.

So if you do:

FIND_DATA find_data;

Then it contains white noise. While if you do:

FIND_DATA find_data = FIND_DATA();

Then everything gets their default values, which for ints is 0, and for
pointers is the NULL pointer value, which may or may not be "all bits zero".

-JKop