CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Aug 2002
    Location
    Tennessee
    Posts
    6

    Statically allocated buffer area!

    Hi,

    If I want to allocate a buffer within stack. but it seems the popular command new is useless since it allocates the memory from heap dynamically. Do you guys know something about that!

    Thanks!

    Jinag

  2. #2
    Join Date
    Jun 2002
    Posts
    1,417
    If you know the size of the buffer:
    Code:
    int foo()
    {
        char buffer[BUFFER_SIZE];
    
        // your code here
    }
    There is another way to dynamically allocate from the stack, but I've never used it. Look in MSDN for HeapAlloc(). It hints that you can allocate memory from any heap -- and the stack is just another type of heap. Personally, I don't think the stack heap should ever be used for that purpose becuase it is too easy to screw up your entire program.

  3. #3
    Join Date
    Aug 2002
    Location
    Tennessee
    Posts
    6

    Thank you for your reply, however the question may be not right understood

    Hi,

    Sorry, maybe I do not state my question clearly, I want to find a way to statically allocate some memory from stack! NOT DYNAMICALLY. That means the address of such buffer should be fixed in the program!

    Anyway, thank you for your reply!

    Jiang

  4. #4
    Join Date
    Jun 2002
    Posts
    1,417
    That's impossible because the stack cannot be used for that purpose! The best you can do his this:
    Code:
    int foo()
    {
       static buffer[BUFFER_SIZE];
    }

  5. #5
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    You could try this syntax but it will only work on the newer compilers and you must remember never to delete it.
    Code:
    class Fred
    {
        // whatever
    };
    main ()
    {
        char buffer[ /* whatever size you wish */];
        
        // Create derf on the stack in buffer
        Fred* derf = new (buffer) Fred;
        
        ...
    
        // deleting derf will cause a crash!!!
        return 0;
    }
    Alternatively, there is a C routine called alloc (Solaris) or _alloca (Microsoft) which will allocate memory from the stack.
    Last edited by cup; September 14th, 2002 at 03:08 AM.
    Succinct is verbose for terse

  6. #6
    Join Date
    Jun 2002
    Posts
    1,417
    Originally posted by cup
    [B]You could try this syntax but it will only work on the newer compilers and you must remember never to delete it.
    Code:
    class Fred
    {
        // whatever
    };
    main ()
    {
        char buffer[ /* whatever size you wish */];
        
        // Create derf on the stack in buffer
        Fred* derf = new (buffer) Fred;
        
        ...
    
        // deleting derf will cause a crash!!!
        return 0;
    }
    That does not work with VC6 compiler -- "error C2660: 'new' : function does not take 2 parameters"

  7. #7
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    That's strange because the following works on VC++ SP0
    Code:
    #include <iostream>
    using namespace std;
    class Fred
    {
    public:
        int subkey1;
        int subkey2; 
        Fred ()
        :   subkey1(0)
        ,   subkey2(0)
        {
        }
        
        void Dump ()
        {
            cout << "Fred " << subkey1 << " " << subkey2 << endl;
        }
            
    };
    
    main ()
    {
        char buffer[100];
        Fred* derf = new (buffer) Fred ();
        derf->subkey1 = 100;
        derf->Dump ();
        return 0;
    }
    Succinct is verbose for terse

  8. #8
    Join Date
    Jun 2002
    Posts
    1,417
    I copied your code and it compiles ok for me in VC6 too. I must have done something wrong.

  9. #9
    Join Date
    Jun 2002
    Posts
    1,417
    When you do that, you must insure that the alignment of the objects allocated that way are correct. The new (buffer) operator simply returns a pointer to buffer and makes no alignment checks or check to see if it has already been allocated to something else.

    There are other major problems with this approach too. You can not use the delete operator to deallocate Fred because it wasn't really allocated in the first place. That means the destructor is never called, which could create a lot of memory leaks.

    Code:
    main ()
    {
        char buffer[255];
        Fred* derf = new (buffer) Fred ();
        Fred* derf1 = new (buffer + sizeof(Fred)) Fred ();
    	if(derf == derf1)
    		cout << "they are the same" << endl;
    	else
    		cout << "they are different." << endl;
        derf->subkey1 = 100;
        derf->Dump ();
        return 0;
    }
    Last edited by stober; September 14th, 2002 at 12:10 PM.

  10. #10
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    Yup - you're right there. I normally use it for PROM based stuff or memory mapped files where the memory is at a fixed address and there is only one structure and no pointers.

    The only other way I know of is to use alloc (Solaris) or _alloca (MS) and cast it. Again, with this one, you musn't use free. It also boils down to a similar thing but this time, since it is an alloc, there is no new or delete. It gets worse
    Succinct is verbose for terse

  11. #11

    Actually....

    There is a corresponding placement delete to the placement new.

    And you can overload those types as well.

    For placement new and new[]

    void * __cdecl operator new(unsigned int nSize,void * pAddress) throw()
    {
    return pAddress;
    }

    void * __cdecl operator new[](unsigned int nSize, void * pAddress) throw()
    {
    return pAddress;
    }

    placement delete and delete[]

    void __cdecl operator delete(void * pBuf,void * pAddress) throw()
    {

    }

    void __cdecl operator delete[](void * pBuf, void * pAddress) throw()
    {

    }
    Last edited by JamesSchumacher; September 15th, 2002 at 03:12 PM.

  12. #12
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    Another alternative is to use C#. In C#, classes are instantiated on the heap and structs on the stack.
    Succinct is verbose for terse

  13. #13
    there is one old command:
    "_alloca"
    to allocate memory on stack, and you do not have to free it, it will be freed after you exit the function!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured