Thread: Newbie "new" nuisance

  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,817
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 07: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