By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 964 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

A question about memory allocation

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

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;
}
Tony Johansson wrote:
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

--
Gregor Razdrtic [Mufe]
mu**@poizen.org
Jul 23 '05 #2

P: n/a
Tony Johansson wrote:
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
Jul 23 '05 #3

P: n/a
"Tony Johansson" <jo*****************@telia.com> wrote in message
news:W%*******************@newsb.telia.net...
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.

--
David Hilsee
Jul 23 '05 #4

P: n/a
I have corrected your top post

Gregor Razdrtic wrote:
Tony Johansson wrote:
Hello Experts!!
don't look at me!

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.
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.

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;


<snip>

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>

--
Nick Keighley

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.