c++ dynamic memory

This is a discussion on c++ dynamic memory within the C++ Programming forums, part of the General Programming Boards category; Hello, I am trying to write a program that uses dynamically allocated arrays in C++. I got the code working, ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    c++ dynamic memory

    Hello,

    I am trying to write a program that uses dynamically allocated arrays in C++. I got the code working, but I am not sure I understand why it works.

    Here is the code:

    Code:
    #include <iostream>
    #include <iomanip>
    
    
    using namespace std;
    
    void allocateh (double *  arr, size_t n, double seed)
    {
            for (size_t i=0;i<n;i++)
                    *arr++=seed/(i+1);
            return;
    }
    
    void allocates (double * & arr, size_t n, double seed)
    {
            double * ptarr = new double [n];
            if (ptarr != NULL)
            {
                    arr=ptarr;
                    for (size_t i=0;i<n;i++)
                    {
                            *arr++=seed/(i+1);
                    }
            }
            return;
    }
    
    void display (const double *  arr, size_t max, size_t rowformat, size_t width)
    {
            cout<<fixed<<setprecision(3);
            size_t icells=0;
            short correct = 9;
            size_t poscount=rowformat*width-(rowformat-1);
            for (size_t i=0;i!=poscount;i++)
                    cout<<"=";
            cout<<endl;
    
            while (max > rowformat)
            {
                    for (size_t i=0;i!=rowformat;i++)
                    {
                            cout<<"| Cell #"<<setw(width-correct)<<icells+1;
                            icells++;
                    }
                    cout<<"|"<<endl;
                    for (size_t i=0;i!=rowformat;i++)
                    {
                            cout<<"| Temp= "<<setw(width-correct)<<*arr++;
                    }
                    cout<<"|"<<endl;
                    max-=rowformat;
    
                    for (size_t i=0;i!=poscount;i++)
                            cout<<"-";
                    cout<<endl;
            }
            if (max)
            {
                    for (size_t i=0;i!=max;i++)
                    {
    
                            cout<<"| Cell #"<<setw(width-correct)<<icells+1;
                            icells++;
                    }
                    cout<<"|"<<endl;
                    for (size_t i=0;i!=max;i++)
                    {
                            cout<<"| Temp= "<<setw(width-correct)<<*arr++;
                    }
                    cout<<"|"<<endl;
            }
    
            for (size_t i=0;i!=poscount;i++)
                    cout<<"=";
            cout<<endl;
    
    
            return;
    }
    
    void reset_ptr(double * &  p, size_t n)
    {
            p-=n;
            return;
    }
    
    
    int main()
    {
            size_t max=30;
            size_t maxrow = 6;
            size_t width=20;
    
            double * pstack;
            allocates(pstack,max,10000.);
            reset_ptr(pstack,max);
            display(pstack,max,maxrow,width);
    
            double * pheap = new double;
            allocateh(pheap,max,100000.);
            //reset_ptr(pheap,max);
            display(pheap,max,maxrow,width);
    
            return 0;
    }
    The code shown above gives the output:

    Code:
    $ ./lab4-hot.out
    ===================================================================================================================
    | Cell #          1| Cell #          2| Cell #          3| Cell #          4| Cell #          5| Cell #          6|
    | Temp=   10000.000| Temp=    5000.000| Temp=    3333.333| Temp=    2500.000| Temp=    2000.000| Temp=    1666.667|
    -------------------------------------------------------------------------------------------------------------------
    | Cell #          7| Cell #          8| Cell #          9| Cell #         10| Cell #         11| Cell #         12|
    | Temp=    1428.571| Temp=    1250.000| Temp=    1111.111| Temp=    1000.000| Temp=     909.091| Temp=     833.333|
    -------------------------------------------------------------------------------------------------------------------
    | Cell #         13| Cell #         14| Cell #         15| Cell #         16| Cell #         17| Cell #         18|
    | Temp=     769.231| Temp=     714.286| Temp=     666.667| Temp=     625.000| Temp=     588.235| Temp=     555.556|
    -------------------------------------------------------------------------------------------------------------------
    | Cell #         19| Cell #         20| Cell #         21| Cell #         22| Cell #         23| Cell #         24|
    | Temp=     526.316| Temp=     500.000| Temp=     476.190| Temp=     454.545| Temp=     434.783| Temp=     416.667|
    -------------------------------------------------------------------------------------------------------------------
    | Cell #         25| Cell #         26| Cell #         27| Cell #         28| Cell #         29| Cell #         30|
    | Temp=     400.000| Temp=     384.615| Temp=     370.370| Temp=     357.143| Temp=     344.828| Temp=     333.333|
    ===================================================================================================================
    ===================================================================================================================
    | Cell #          1| Cell #          2| Cell #          3| Cell #          4| Cell #          5| Cell #          6|
    | Temp=  100000.000| Temp=   50000.000| Temp=   33333.333| Temp=   25000.000| Temp=   20000.000| Temp=   16666.667|
    -------------------------------------------------------------------------------------------------------------------
    | Cell #          7| Cell #          8| Cell #          9| Cell #         10| Cell #         11| Cell #         12|
    | Temp=   14285.714| Temp=   12500.000| Temp=   11111.111| Temp=   10000.000| Temp=    9090.909| Temp=    8333.333|
    -------------------------------------------------------------------------------------------------------------------
    | Cell #         13| Cell #         14| Cell #         15| Cell #         16| Cell #         17| Cell #         18|
    | Temp=    7692.308| Temp=    7142.857| Temp=    6666.667| Temp=    6250.000| Temp=    5882.353| Temp=    5555.556|
    -------------------------------------------------------------------------------------------------------------------
    | Cell #         19| Cell #         20| Cell #         21| Cell #         22| Cell #         23| Cell #         24|
    | Temp=    5263.158| Temp=    5000.000| Temp=    4761.905| Temp=    4545.455| Temp=    4347.826| Temp=    4166.667|
    -------------------------------------------------------------------------------------------------------------------
    | Cell #         25| Cell #         26| Cell #         27| Cell #         28| Cell #         29| Cell #         30|
    | Temp=    4000.000| Temp=    3846.154| Temp=    3703.704| Temp=    3571.429| Temp=    3448.276| Temp=    3333.333|
    ===================================================================================================================
    My questions are in regards to the way the pointers pstack and pheap behave: I need to call the function reset_ptr if I use pstack, but I don't need it when I use pheap (if I uncomment the line reset_ptr(pheap,max) then I get the first two positions skipped).

    The pointer pstack is defined on the stack and points to a double; the function allocates takes the address of that pointer and makes it point to a newly allocated array of doubles on the heap. Once the array is being populated in allocates, then the pointer must be reset back to the first element.

    The pointer pheap is defined on the stack but made to point to a double on the heap. The function allocateh populates the array with doubles. Now why don't I need to reset this pointer back to the first element? If I do it will skip the first two elements.

    Thank you in advance.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I got the code working, but I am not sure I understand why it works.
    The program has undefined behavior. It may appear to work for you, but it won't necessarily work in all cases.

    The pointer pstack is defined on the stack and points to a double;
    No, it doesn't. It's just uninitialized (points to random garbage).

    the function allocates takes the address of that pointer and makes it point to a newly allocated array of doubles on the heap. Once the array is being populated in allocates, then the pointer must be reset back to the first element.
    Of course, there is no real reason why you should modify the pointer to point to the end of the array in the first place.

    Really just forget about the idea behind reset_ptr. Don't lose a pointer to the memory you allocated. If you need to iterate, use a copy of the original pointer.

    The pointer pheap is defined on the stack but made to point to a double on the heap.
    One double. All operations involving this pointer are accessing out of bounds (and the program eventually crashes with VC++).

    The function allocateh populates the array with doubles. Now why don't I need to reset this pointer back to the first element? If I do it will skip the first two elements.
    That's because allocateh is modifying a copy of the pointer, which doesn't affect pheap in main. That is the difference between passing something by value / by reference.

    A general misunderstanding seems to be about the use of stack and heap. The pointers themselves are on the stack, and the memory is dynamically allocated in either case (too little in the second case).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [BEGINNER] dynamic memory dumb question
    By arthurhess in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2009, 02:53 PM
  2. Dynamic Linking & Memory usage
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 06-02-2007, 10:57 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 04:59 AM
  4. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 02:41 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 10:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21