473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

A question about memory allocation

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
4 1269

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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: laniik | last post by:
I am relativly new in c++, so Im not entirely sure about how the memory allocation stuff works. My problem is that i am running out of memory after what is seemingly a very small amoune of memory...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
18
by: Peter Smithson | last post by:
Hi, I've read this page - http://devrsrc1.external.hp.com/STK/impacts/i634.html but don't understand it. Here's the text - "Non-standard usage of setjmp() and longjmp() could result in...
1
by: kiplring | last post by:
List<string> effectList = new List<string>(); effectList.Clear(); effectList = null; using (List<string> effectList = new List<string>()) { } If there are so many calls, I should save as...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
3
by: Kane | last post by:
When you create node 1 you allocate memory and link it Again when you create node 2 you would allocate memory for that node in a different section of the code. Is there more efficient way where I...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
19
by: =?ISO-8859-1?Q?Nordl=F6w?= | last post by:
I am currently designing a synchronized queue used to communicate between threads. Is the code given below a good solution? Am I using mutex lock/unlock more than needed? Are there any resources...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.