A question about memory allocation

T

Tony Johansson

Hello Experts!!

I have an easy question when you allocate memory by using new you allocate
memory dynamically but what is it called
when you allocate memory without using new.

Here is an example of allocating memory dynamically. You instansiate object
obj of class Test. The class is called Test
Test obj = new Test();

Here is an example of allocating memory without using new. You instansiate
object obj of class Test. The class is called Test.
You know you can allocate memory in different scope.(local(block),
file(global), function or class)

Test obj;

Many thanks!!

//Tony
 
G

Gregor Razdrtic

I'm not skilled in formal language expresion so I hope it will help you.

When you initialize
"Test* test = new Test();"
you have a test Pointer, that is size of 4 bytes, that points to
location where real test object is located.

If you initialize
"Test test;"
You get "realtime" an object, that is directly accessed, not thro pointer.

The difference is, that if you initialize it with new,
you have to delete it somewhere, and you have to set pointer back to NULL.

First example to correctly delete, clean it from memory is.
int main()
{
{
Test* test = new Test();
// Do whatever
delete test; // Delete class object
test = NULL;
// If you don't delete this object here ...
// you will lose object, pointer,
// -> MEMORY LEAK
}
return 666;
}
Without new
int main()
{

{
Test test;
// This object is only valid in this block
// When you leave this block, descructor is automaticly
// called
}
// here object is deleted

return 666;
}
 
M

Mark P

Tony said:
Hello Experts!!

I have an easy question when you allocate memory by using new you allocate
memory dynamically but what is it called
when you allocate memory without using new.

There may be other names, but this is known as allocating memory on the
stack.

If you're familiar with the stack data structure you can see the logic
of this name. When, for example, you call a function, you may create
some new objects on top of the stack (function arguments, local
variables, temporaries, etc.) and all of these will in general be lost
(popped) when you return from the function.

Other objects may have existed on the stack prior to the function call
(say from the calling scope). Those objects were "buried under" by the
function's additions to the stack and were then "uncovered" when the
function returned. Likewise if this function calls another function,
the second function may add to the top of the stack, but these additions
will be popped when the second function returns.

This is all consistent with the last-in, first-out nature of a stack.

Hope that helps,
Mark
 
D

David Hilsee

Tony Johansson said:
Hello Experts!!

I have an easy question when you allocate memory by using new you allocate
memory dynamically but what is it called
when you allocate memory without using new.

Here is an example of allocating memory dynamically. You instansiate object
obj of class Test. The class is called Test
Test obj = new Test();

Here is an example of allocating memory without using new. You instansiate
object obj of class Test. The class is called Test.
You know you can allocate memory in different scope.(local(block),
file(global), function or class)

Test obj;

Many thanks!!

The standard describes the differences in terms of storage duration. Local
objects that are destroyed automatically at the end of the block have
automatic storage duration, while objects allocated dynamically using new
have dynamic storage duration. Those that are neither and last for the
duration of the program have static storage duration.
 
N

Nick Keighley

I have corrected your top post

Gregor said:
Tony Johansson wrote:

don't look at me!


the standard doesn't say. The compiler just brings automatic variables
when
needed and removed them when not. Typically a stack is used. You don't
usually need to know.

I'm not skilled in formal language expresion so I hope it will help you.

When you initialize
"Test* test = new Test();"
you have a test Pointer, that is size of 4 bytes,

doesn't have to be 4-bytes. And a byte may not be 8-bits.

that points to location where real test object is located.

If you initialize
"Test test;"
You get "realtime" an object, that is directly accessed, not thro pointer.

The difference is, that if you initialize it with new,
you have to delete it somewhere, and you have to set pointer back to
NULL.

no, you don't have to set the pointer to NULL.

First example to correctly delete, clean it from memory is.

int main()
{
{
Test* test = new Test();
// Do whatever
delete test; // Delete class object
test = NULL;

this is not necessary; "test" is about to go out of scope so why set
it to NULL?

// If you don't delete this object here ...
// you will lose object, pointer,
// -> MEMORY LEAK
}
return 666;
}

<snip>
 

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,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top