Creating objects in C++ not using "new"
I want to make a program that lets say represents a matrix now the matrix will be represented by a vector that each object in the vector will represent a cell example: vector now when constructing the matrix the constructor receives a list of cells to insert in the matrix. The size of the list is unknown in compila开发者_运维问答tion time
I am interested in creating this matrix without using memory on the heap. In other words not creating object using the word "new" or "delete" is there any way to do that if I don't know how many objects are meant to be inserted into the vector?
There is a special way to use new to allocate memory in the stack or as static storage using what is called the placement new operator. With this version of new, you reserve a chunk of memory and the you explicitly tell new where you want to store a specific variable. It would work as follows:
#include <new>
int main()
{
char buffer[500]; // chunk of memory
int p*;
p = new (buffer) int[10];
}
Note that you need to include the new header in order to use this special new operator. In this case, as you are using automatic storage, the memory will be freed upon leaving the block in which it was declared (the main).
References: C++ Primer plus. Chapter 9. Page 420
There is no standard way to do this without performing direct (and thus platform-dependent) manipulation of the program's/function's stack frame using assembly instructions - which I would heartily discourage. What's stopping you from using the heap?
Use alloca
to obtain a pointer and then use the in-place new
operator:
void *p = alloca(sizeof(Class));
new (p) Whatever(arguments);
However, do read alloca
manual page before using it! Be very careful. As Jim Brissom says, alloca
isn't portable.
You don't need to delete
. The memory will be freed when the function returns
There is a way, it's very limiting and very unorthodox. You'll need to create a statically sized array of unsigned char
which form a memory pool. There will be a limit to the size of the list of objects. You'll need to overload a new
operator (and delete
operator) for that class to specifically target such a memory pool.
That said, there's really no good reason to go this route.
Well, if you don't want to use memory on the heap, where else do you want to get it from?
a) system dependant - you can ask the operating system to allocate some memory for you. But this is bad style (system dependant), and will use the same RAM... just in a different way allocated. For example, ::GlobalAlloc or ::LocalAlloc in Windows 32 will do such things if you are really interested in doing that.
b) memory mapped files - that might be interesting if you are asking because you think you'll have not enough RAM available and access time isn't an issue.
c) resort to C functions like malloc/free and cast the pointers... that is getting memory from the heap, just avoiding the "new" and "delete" keywords.
However, it is hard to tell what a "good" solution without information why you want to avoid new / delete. You have need for dynamic memory allocation, these two are the tools to do that.
Could you please explain/rephrase your question so you can get more precise answers?
精彩评论