Scope of New operator

Started by
5 comments, last by iMalc 14 years, 11 months ago
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.
Advertisement
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?
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.
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.
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.
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.
I don't get it either. Are you trying to write a pool allocator or something?
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement