views:

142

answers:

5

hey,

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 recives a list of cells to insert in the matrix. the size of the list is unknown in compilation 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 dont know how many objects are meant to be inserted into the vector?

thanx in advance kevin

+1  A: 

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?

Richard Cook
+1  A: 

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

Yassin
alloca is not exactly portable...
Jim Brissom
Wrap such resource management up. Doing memory management by hand == bad.
GMan
Portability concerns aside, is the pointer returned by `alloca` guaranteed to be correctly aligned for `Whatever`?
James McNellis
@Jim Brissom: yes, that's right, +1
Yassin
The class destructor won't be called when the function returns. If the class has any pointers you're probably making a memory leak.
Matt Kane
+1  A: 

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.

wheaties
IIRC, placement new as you describe is pretty common in the Windows Printer DDK.
Matt Kane
In fact, I think that is the Right Way to put an object onto the heap at a location where it can't be paged out, if that's at all necessary.
Matt Kane
+1  A: 

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?

foo
+2  A: 

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

Vintharas
`buffer` is not guaranteed to be suitably aligned for `int`.
James McNellis
Thanks for the tip. It felt strange seeing that example in the book :)
Vintharas
But this uses the word 'new'. OP wants no 'new'-ness or 'delete'-ness. Not downvoting for now!
Chubsdad
I think what he meant by that statement was that he did not want to use the heap, and thus the standard dynamic memory allocator new and deallocator delete. With placement new you don't necessarilly allocate dynamic memory and you don't need to use delete to free you memory.
Vintharas