Link to home
Start Free TrialLog in
Avatar of Dude2008
Dude2008

asked on

C++ Cout

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>?
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just to be complete, you have a memory leak, but that doesn't impact the correctness of the code.
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

Did you try running that, pgnatyuk ?
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
>> 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 []
I did.
And stop it, please.
Screen-shot-2010-07-01-at-10.33..png
Avatar of Dude2008
Dude2008

ASKER

>> 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....
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.

>> 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.
@Infinity08: Do you have to get nervous because of a spelling mistake in this program?
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.
Ok. Forgotten.
@Dude2008 : I hope these last few posts haven't confused you too much. Is there anything that you're still unsure about ?
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?
>> 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 ?
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

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
done