C++ Cout

Dude2008
Dude2008 used Ask the Experts™
on
int main(int argc, char *argv[])
{

int * ivar = new int(12);
ivar = new int(18);

cout << *ivar;

delete ivar;

return 0;
}


what is worng with the code>?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009
Commented:
Apart from the fact that you forgot this at the top :

        #include <iostream>

and forgot to mention the std:: namespace :

        std::cout << *ivar << std::endl;

(note that I also added a newline at the end), there's nothing wrong.
Top Expert 2009

Commented:
Just to be complete, you have a memory leak, but that doesn't impact the correctness of the code.

Commented:
Everything is wrong.


#include <iostream>

int main (int argc, char * const argv[]) 
{
	int * ivar = new int(12);
	for (int i = 0; i < 12; ++i)
		ivar[i] = i;
	
	for (int k = 0; k < 12; ++k)
		std::cout << "[" << k << "] = " << ivar[k] << "\n";

	delete [] ivar;
    return 0;
}

Open in new window

Top Expert 2009

Commented:
Did you try running that, pgnatyuk ?

Commented:
Do you want to talk here about it?

I cut and paste from the Xcode project.

Screen-shot-2010-07-01-at-10.30..png
Top Expert 2009

Commented:
>> I cut and paste from the Xcode project.

No you didn't. There's an important difference, namely on this line :

>>       int * ivar = new int(12);

Notice the () instead of the []

Commented:
I did.
And stop it, please.
Screen-shot-2010-07-01-at-10.33..png

Author

Commented:
>> I cut and paste from the Xcode project.

No you didn't. There's an important difference, namely on this line :

>>       int * ivar = new int(12);

Notice the () instead of the []

mean what....

Commented:
The correct (and traditional)way to allocate memory for an array of int is:
int* array = new int[12];

I took your code and modified it. So I got this new int(12). :( But it's compiled well in Xcode 3.2.2.

Top Expert 2009

Commented:
>> I did.
>> And stop it, please.

What do you mean ? The code you posted is wrong.

new int(12) does NOT allocate a dynamic array of 12 ints. It allocates memory for ONE int, and initializes it to 12.


>> mean what....

Sorry Dude2008. My last few posts where in response to pgnatyuk's posts.
My first two posts were there to answer your question.

Commented:
@Infinity08: Do you have to get nervous because of a spelling mistake in this program?
Top Expert 2009

Commented:
I'm not getting nervous. I'm simply pointing out a mistake. But you seem to take it personal, even though it was not intended to be personal.

Commented:
Ok. Forgotten.
Top Expert 2009

Commented:
@Dude2008 : I hope these last few posts haven't confused you too much. Is there anything that you're still unsure about ?

Author

Commented:
So Yes, I am confused..

apart from not including hearder file and std namespace...it compiles....

In terms on using []...becuase an array is declared?
Top Expert 2009

Commented:
>> In terms on using []...becuase an array is declared?

Were you trying to create an array ? Because nothing in your original code indicates that you were.

What was the point of your question ? Obviously you thought that something was wrong with it. Why did you think that ?

Commented:
I think Infinity had the right answer up top, but just to clarify for you:
Your code creates a pointer and two integers.  The integers are created dynamically which means that they do not get deleted when they go out of the function scope.  Since you assign the second dynamically created (with new) integer to the same pointer as the first one without explicitly deleting the first one, that first one is lost in a memory leak.
The second integer is printed using cout, which is included in the standard namespace and iostream library shown up top.  It is printed by dereferencing the pointer to get the value of the memory location (which is why it has an asterisk *).
The second integer is then destroyed using the delete command, thus freeing the memory.  This is good, but you should have done it with your first integer by printing the same delete line prior to assigning the second integer with new.
This code all works on normal integers, not arrays.  Arrays are created using [] instead of () as said by people earlier.  Arrays which are dynamically created must also be deleted using the delete[] operator rather than normal delete.
The code below is commented to help:
Good luck! :)  

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    //Create a pointer to an integer called ivar and then dynamically
    //allocate one integer space with a value of 12 and store it in the pointer.
    int * ivar = new int(12);

    //Allocate a new integer dynamically with a value of 18.  The pointer now
    //points to that instead.  The first integer (value of 12) is now lost in
    //a memory leak as Infinity08 mentioned.
    ivar = new int(18);

    //Dereference the pointer, giving a value of 18.
    cout << *ivar;

    //Delete the memory pointed to by the pointer (delete the dynamic integer of value 18).
    delete ivar;

    //End main.
    return 0;
}

Open in new window

Commented:
PS: When I say this:
    //Create a pointer to an integer called ivar and then dynamically
    //allocate one integer space with a value of 12 and store it in the pointer.

"store it" means store the memory address of that integer in the pointer, not store the integer itself in the pointer.  I appologize for the ambiguity :)
-w00te

Author

Commented:
done

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial