creation of an object

R

Rahul

Hi Everyone,

I have the following code,

class Test
{
public : Test()
{
}
};

int main()
{
Test object;
return(0);
}

If my understanding is correct, the constructor isn't responsible for
the allocation of the memory for the object, so how does the actual
memory allocation occur?

What exactly happens beneath the following statement,

Test object;

Thanks in advance!!!
 
R

Rolf Magnus

Rahul said:
Hi Everyone,

I have the following code,

class Test
{
public : Test()
{
}
};

int main()
{
Test object;
return(0);
}

If my understanding is correct, the constructor isn't responsible for
the allocation of the memory for the object, so how does the actual
memory allocation occur?

What exactly happens beneath the following statement,

Test object;

The C++ standard doesn't say much about what happens. First, memory is
reserved for the object in some implementation-specific way, then that raw
memory is turned into a real object by the constructor.
 
S

Scott McPhillips [MVP]

Rahul said:
int main()
{
Test object;
return(0);
}

If my understanding is correct, the constructor isn't responsible for
the allocation of the memory for the object, so how does the actual
memory allocation occur?


In almost all architectures the compiler generates code at the function
entry point to adjust the stack pointer register by the size of the
function's stack variables. It also undoes this adjustment at the
function's return. Both operations are incredibly efficient in machine
language.
 
R

Raymond

Rahul said:
Hi Everyone,

I have the following code,

class Test
{
public : Test()
{
}
};

int main()
{
Test object;
return(0);
}

If my understanding is correct, the constructor isn't responsible for
the allocation of the memory for the object, so how does the actual
memory allocation occur?

What exactly happens beneath the following statement,

Test object;

Thanks in advance!!!

'object' is said to have automatic storage duration.

int main ()
{
auto Test object;
}

A C++ compiler would generate the necessary processor instructions to
allocate storage from a pre-allocated memory area called the stack; a
memory area typically directly accessible to the processor. A C++
compiler would also generate the necessary processor instructions to
free the memory.

int main ()
// Instructions to allocate memory for 'object'.
{
Test object;
}
// Instructions to free memory for 'object'.


int main ()
// Instructions to allocate memory for 'object'.
{
Test object;
// Instructions to allocate memory for 'object2'.
{
Test object2;
}
// Instructions to free memory for 'object2'.
}
// Instructions to free memory for 'object'.


int main ()
// Instructions to allocate memory for 'object'.
{
Test object;
// Instructions to allocate memory for 'object2'.
{
Test object2;
// Instructions to allocate memory for 'object3'.
{
Test object3;
}
// Instructions to free memory for 'object3'.
}
// Instructions to free memory for 'object2'.
}
// Instructions to free memory for 'object'.

The constructor is then called to initialize the memory to a known
internal state/assumption.


Some people want to hide all the underlying pieces, but this is how it
most often will be implemented, to this date, I'm 99% sure. Although,
all you need to know at one level is that C++ deals with allocating and
freeing the memory (thus the name automatic storage duration). So once
object2 and object3 are out of scope, they no longer exist.

What's really going to bake your noodle later on is--what happens if
there's not enough memory? Some systems crash. Others deal with it in
one way or another. It's beyond the scope of C++. If you really care
about control, you would research this on each target system. Though,
statistically, how often would this occur?
 
J

James Kanze

I have the following code,
class Test
{
public : Test()
{
}
};
int main()
{
Test object;
return(0);
}
If my understanding is correct, the constructor isn't
responsible for the allocation of the memory for the object,
so how does the actual memory allocation occur?

Compiler's problem, not yours:). The memory must be allocated
somehow.
What exactly happens beneath the following statement,
Test object;

Depends on the implementation. Typically, the compiler will sum
up the total amount of memory needed in the function, and
allocate it on the stack at the start of the function. All that
is guaranteed, however, is that the memory will be allocated
before the constructor is called, and that it won't be freed
until the destructor has been called (with a few special
exceptions in the latter case).
 

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
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top