Thread: Weird memory problems (MS-VC++)

  1. #1
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114

    Weird memory problems (MS-VC++)

    I'm having some problems with dynamic memory. I experiment with operator "new" for making arrays, which works fine. but the problem is that when I use dynamic arrays in classes or functions, I'm getting memory heap errors...

    The thing's that lets say I have one dynamic int var in the main function, I put it length to 10 000 000, just for tests. But when I do the exact thing inside a class, it fails...

    Code:
    void main()
    {
        int *a;
        a = new int[10000000];
    }
    That works, but not this:

    Code:
    class abc
    {
        public:
            int *a;
            abc()
            {
                a = new int[10000000];
            }
            ~abc
            {
                delete a;
            }
    };
    
    void main()
    {
        abc i;
    }

    Is it possible that a class or function or structure (or other...) may limit the amount of memory possible to use in a program?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void main()
    main returns an int - see the FAQ

    > delete a;
    You're deleting the wrong thing.
    If you used [ ] in the new, then you need a [ ] in the delete, as in
    delete [ ] a;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You are doing it wrong! Memory is automatically put on the stack when declared in functions, except when you use the new operator. The new operator will return the memory you asked it for but you can only access it through a pointer.

    The reason is that stack memory is significantly smaller and built in a particular way (allocation for return type, allocation for parameter types, allocation for all the variables in the order they occur in the function), so the program knows where to find the memory most of the time and when its deallocated. With heap memory it's different: the memory will be in a different place with each run of the program, and it may be deallocated anywhere. The pointer allows you to access and control the memory directly.

    This is all assuming that new allocation actually works. If it fails, new will throw a std::bad_alloc exception, and you're left with a NULL pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difference in MSVC 6 & MS VC .Net
    By passionate_guy in forum C Programming
    Replies: 1
    Last Post: 01-23-2006, 06:39 AM
  2. problems with memory allocation
    By Waldo2k2 in forum C++ Programming
    Replies: 12
    Last Post: 07-16-2002, 01:08 PM
  3. Memory Allocation/Freeing Problems
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 05-29-2002, 08:50 AM
  4. Replies: 15
    Last Post: 04-19-2002, 02:23 PM
  5. Ascii conversion problems with dynamic memory
    By Butters in forum C++ Programming
    Replies: 2
    Last Post: 01-29-2002, 12:22 PM