Newbie "new" nuisance

This is a discussion on Newbie "new" nuisance within the C++ Programming forums, part of the General Programming Boards category; I'm currently going through a C++ primer and one of the exercises was to create storage of various types using ...

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Newbie "new" nuisance

    I'm currently going through a C++ primer and one of the exercises was to create storage of various types using "new," to print the addresses and then delete them. Here is my code:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int* i = new int;
    	long* l = new long;
    	char* c = new char;
    	float* f = new float[100];
    
    	cout << "Address of i: " << i << endl;
    	cout << "Address of l: " << l << endl;
    	cout << "Address of c: " << c << endl;
    	cout << "Address of f: " << f << endl;
    
    	delete i;
    	delete l;
    	delete c;
    	delete f;
    
    	getchar();
    	return 0;
    }
    It prints the address fine except for the pointer to char. This is what I get:

    Code:
    Address of i: 007D4B50
    Address of l: 007D4B90
    Address of c: ═²²²²½½½½½½½½■ε■
    Address of f: 007D4C10
    How come the char* is printing as garbage?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,681
    When printing a char pointer, cout tries to print a string (null terminated array of characters starting at the address given). This is different behavior than in the other cases. Try this:
    Code:
    cout << "Address of c: " <<  reinterpret_cast<void*>(c) << endl;
    Code:
    float* f = new float[100];
    
    ...
    
    delete f;
    That last bit needs to be delete [] f since you've allocated an array of elements and not just a single one.
    Last edited by hk_mp5kpdw; 05-01-2009 at 09:12 PM.
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Aha! That all makes sense and thanks for reminding me about the delete []f.

    However - do I have to use a "reinterpret_cast"? It works OK with a simple (void*) cast too.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    19,550
    Quote Originally Posted by Sharke
    However - do I have to use a "reinterpret_cast"? It works OK with a simple (void*) cast too.
    You should use reinterpret_cast as it is easier to spot, more ugly, and the C++ style casts document your intentions better.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  2. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  3. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 06:49 PM
  4. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

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