Thread: Help with a problem regarding dynamic memory and arrays

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    14

    Help with a problem regarding dynamic memory and arrays

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       int * myInt = new int;
       long *myLong = new long;
       char* myChar = new char[100];
       float* myFloat = new float[100];
       
       cout << "&myInt's new = " << myInt << ".\n";
       cout << "&myLong's new = " << myLong << ".\n";
       for(int i = 0; i < 100; i++)
       {
          cout << "&myChar's new element [" << i << "] = " << &(myChar[i]) << ".\n";
          
       }
    [B]   for(int i = 0; i < 100; i++)
       {
          cout << "&myFloat's new element [" << i << "] = " << myFloat << ".\n";
          myFloat++;
       }
       delete myInt;
       delete myLong;
       delete []myChar;
       delete []myFloat;
       
       return 0;                   
    }
    Upon running this program (which is trying to print the addresses of various dynamically allocated variables and arrays), all of the addresses are provided save those belonging to the char array's elements.
    Instead of their arrays, lines such as
    FP_NO_HOST_CHECK=NO and
    ComSpec=C:\WINDOWS\system32\cmd.exe are printed in various degrees of truncation. Do these have anything to do with previous contents of the heap?
    More importantly, how can I get the source code to print out the addresses properly?

  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
    > Do these have anything to do with previous contents of the heap?
    Yes.

    > More importantly, how can I get the source code to print out the addresses properly?
    Unfortunately, it treats a char* as a special case when you try and output a pointer. It assumes it should dereference it and print the string where it points.
    You need to cast the pointer to some other type (say void*) to cause the actual pointer value to be printed.

    > myFloat++;
    You do realise that the value passed to delete is no longer the value returned by new.
    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
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    The &myChar[i] is of type char *, which is commonly a c string, and will be printed out as if it was a null terminated string. You can cast it to a void pointer (void *). In addition, since you modify myFloat, delete[]'ing it is bad. Make another temporary pointer equal to myFloat.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    You don't need to increment myfloat.

    Code:
    cout << "&myFloat's new element [" << i << "] = " << &myFloat[i] << ".\n";
    EDIT: Also another way, avoiding the cast, to calculate the addresses of char* is:
    Code:
    &myChar + i * sizeof(char)
    Last edited by Mario F.; 07-26-2006 at 12:55 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I was going to recommend that the op do something nice like this instead.
    I don't write nearly as much to the console but I guess that's okay.

    Code:
    #include <cstring>
    #include <iostream>
    
    using namespace std;
    
    int main( ) {
        int *my_int = new int(0);
        long int *my_long = new long int(0);
        float *float_arr = new float[100];
        char *my_cstr = new char[100];
    
        for (int i = 0; i < 100; i++)
            float_arr[i] = i + 1.0;
    
    
        // we have to copy a string into the array correctly using strncpy
        strncpy(my_cstr, "hello world", sizeof my_cstr);
    
        cout << "my_int    @ " << &my_int << "\n";
        cout << "my_long   @ " << &my_long << "\n";
        cout << "float_arr @ " << &float_arr << "\n";
        cout << "my_cstr   @ " << &my_cstr << "\n";
    
        delete my_int;
        delete my_long;
        delete [] my_cstr;
        delete [] float_arr;
    }
    Last edited by whiteflags; 07-26-2006 at 07:34 PM.

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    14
    Thanks very much for the help everyone. I had no idea that char* behaved this way, but it does describe the seemingly odd output I had been getting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. Dynamic Memory and Arrays
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2006, 04:09 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. dynamic memory and arrays
    By jomns in forum C++ Programming
    Replies: 4
    Last Post: 01-04-2004, 02:18 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM