Sign in to follow this  
VprMatrix89

Scope of New operator

Recommended Posts

Is this true: in the scope of a function the new operator is forgotten after the function ends? I'm trying to find a way to allocate permanent memory that isn't initiated in the code in any way. (ie no pointers or vectors ). I want to create a permanent vector from nothing, is this possible ? In essence what I'm trying to do is register a type but use it in a vector container.. But vectors seem to need a pre-defined type.

Share this post


Link to post
Share on other sites
When posting, it is best to be as specific as possible. For example, you never said what language you were using.

Guessing C++, an instance allocated with new will outlive any scope, it must be explicitly destroyed by a corresponding call to delete. Though we often use certain tricks to implicitly call delete [wink]

In other languages, objects allocated with new act similarly, except without needing explicit notification of when you are finished with the object.

Aside from that, I actually don't understand your question. There is something about vectors, pointers and memory but I can't decipher it. Can you show code, or give us a high level overview of what you are trying to achieve?

Share this post


Link to post
Share on other sites
Tbh I thought new was only in c++. Why do you want to know? I'm probably doing something that's been done 100 times (unfortunately). Its far from finished but I said before all it does now is act as a single buffer with dedicated sections.

This particular question was to find out if I could use a Template to create new instances of memory that aren't being overwritten randomly, I didn't know the new operator worked so diligently.

Share this post


Link to post
Share on other sites
No, lots of languages have the new keyword. Ex: C# and Java. In any case, I don't know why you're so afraid of showing your code, but in multiple threads you've tried to explain in English what you're trying to accomplish and failed. This would be one of them. Show code for what you're trying to accomplish.

Share this post


Link to post
Share on other sites
I'm not sure what you are asking. If you want the memory to be around and not overwritten that's what the heap is for. Are you afraid of an accidental overwrite? You can also allocate from pieces of a static array or something like that, but then you are limited by the initial size of your array that is compiled into your code. You can allocate from wherever you want if you overwrite new, but the memory has to come from somewhere. If you are talking about allocation off the stack, that memory is gone once the function ends. Maybe you are thinking of a language like scheme.

Share this post


Link to post
Share on other sites
Quote:
I want to create a permanent vector from nothing, is this possible ?
Yes it is.

Quote:
This particular question was to find out if I could use a Template to create new instances of memory that aren't being overwritten randomly

Yes, you could.

Quote:
I'm probably doing something that's been done 100 times (unfortunately)
Very likely.

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