Sign in to follow this  
baker

when do you use the new operator?

Recommended Posts

baker    158
you want to use new when you want to store data on the heap. doing so will allow you to access that memory from different locations in the program? is this right? for example if i have a function that just creates some variables. after the function completes, all the variables that i declared are "popped" out of the stack? is this right? if not, please explain. now my next question: i have a map of type int and my own object type. this map will need to be accessed from all my functions and program. now when i insert items into the map, the map will store all of the items even though i dont explicitly call new?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote: you want to use new when you want to store data on the heap. doing so will allow you to access that memory from different locations in the program? is this right?


No, that's not really true. If I use new to create a variable in Class A, Class B can't see that variable because a class can only see a variable that's in scope such as it's own variables, variables declared in it's base class, variables of friends and public variables.

Quote: for example if i have a function that just creates some variables. after the function completes, all the variables that i declared are "popped" out of the stack?


That's not always true. In languages that do garbage collection for you such as Java, C# and several others, that is true but in C++ which makes you clean up the heap after yourself, you will leave a resource in the heap that can't be cleaned, modified or dealt with. Unfortunately, forgetting to clean the heap can cause the system to slow down, or if the program is really sucking on the heap, cause the OS to crash.

When ever you create a variable with new, always delete it and for safety reasons set it to null. That way if you accidentally delete it again, your program won't crash and try to take the OS with it.

Share this post


Link to post
Share on other sites
baker    158
so lets say i had a function and within that function i am creating local variables of int and strings.

how does one do heap cleanage?


thanks

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
you want to use new when you want to store data on the heap. doing so will allow you to access that memory from different locations in the program? is this right?
The first part is more or less correct, but from where you can access this memory is another issue. Consider the following:
{
int* array = new int[10];
/* ...do some stuff with array... */
delete [] array;
}
Although this example uses new, 'array' is only available in local scope. So using new doesn't imply any particular scope or access.
Quote:
for example if i have a function that just creates some variables. after the function completes, all the variables that i declared are "popped" out of the stack?
Variables that are declared in a particular scope have their destructors called and are popped off the stack when they go out of scope. Note however that if you declare a pointer and assign it to some allocated memory via new, the pointer itself is destroyed at the end of the scope, but the memory is leaked. This is one of many reasons not to use raw pointers.
Quote:
i have a map of type int and my own object type. this map will need to be accessed from all my functions and program. now when i insert items into the map, the map will store all of the items even though i dont explicitly call new?
If your objects have appropriate copy semantics and behavior, no 'new' is required; you just add the objects to the map via operator[]() or insert() as you normally would, and they will be copied and stored within the map. Being able to access the map from anywhere in your app is a different issue, having more to do with software design. Possible solutions include globals, singletons, or passing the map around by reference.

Note that I'm not a C++ pro, so the above answers aren't guaranteed to be accurate (or even correct).

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by baker
so lets say i had a function and within that function i am creating local variables of int and strings.

how does one do heap cleanage?
A very easy way to assure proper cleanup in C++ is to always use an appropriate STL container where possible, and in general avoid raw pointers. If that doesn't meet your needs, there are also smart pointers.

Share this post


Link to post
Share on other sites
baker    158
thanks everyone. things are more concrete now.


basically what i was trying to get at was have a local database(map) that can be accessed anywhere in my program. this includes functions that are stored in other .cpp files.

for example lets say i have a map<int, myobject> mylocaldata.

sometimes in functions of another class, i want to check if a certain value is in my map, or insert a new value into my map in another class's function.

class a{
int aa;
check_aa();
}
a::check_aa()
{
if(aa == 3)
{
insert item into my map mylocaldata
}

}

main
{
map<int, sometype> mylocaldata;
class a;
a.aa = 3;
a.getA();

now here the item is stored into my mylocaldata map right?


}








Share this post


Link to post
Share on other sites
Bregma    9214
Just in case it's not clear, a standard container like a std::map stores copies of objects. That means if you create an object of automatic storage duration ("on the stack" in the vernacular) then stick it in you map, you're actually constructing a copy of that object in the map. The local variable can be destroyed on function exit ("popped from the stack"), but the copy in the map just keeps on going.

Ty can also create objects on the free store ("on the heap", or using the new operator) and stick copies in the map. You can do what you want with the pointer, it won;t affect the copy in the map.

This is called value semantics. It's the same as parameter passing in C++. Alternatives used less often include reference semantics, which is used when parameters are passed by reference, and move semantics which is used by std::auto_ptr.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this