J
john.hicken
I'm new to C++, and I'm currently reading (and doing the exercises
from), Thinking in C++. Anyway, I've come across an oddity on one of
the exercises. Here is my code:
#include <iostream>
#include <cstdlib>
int main()
{
int* a = new int(20);
cout << "Address of new int: " << a << endl;
delete a;
long* b = new long(10034340);
cout << "Address of new double: " << b << endl;
delete b;
char* c = new char [100];
c[0] = 'a';
c[1] = 'b';
cout << "Address of new char array: " << c << endl;
delete [] c;
int* d = new int [100];
d[0] = 2;
d[1] = 3;
cout << "Address of new int array: " << d << endl;
delete [] d;
std::system("PAUSE");
return 0;
}
and here is the output:
Address of new int: 0x26704f0
Address of new double: 0x2670520
Address of new char array: ab
Address of new int array: 0x26705a0
Press any key to continue . . .
The oddity is the "ab" output. What I expected was for a new array of
100 chars to be allocated, and the address of the new array to be
passed to c. Thus, I would expect outputting c to cout would print the
address, as it seems to for the other cases. Instead it seems to print
the contents of the array. This doesn't seem to apply for the int
array.
I've tried de-referencing a, b, c and d, and I get the contents of the
new int and long, and the value of the first element (c[0] = 'a', and
d[0] = 2), in the case of the arrays, which is what I would expect, as
I gather arrays are in effect just pointers to the first element.
Can anyone explain what is going on here? Am I doing something wrong?
Thanks
John Hicken
from), Thinking in C++. Anyway, I've come across an oddity on one of
the exercises. Here is my code:
#include <iostream>
#include <cstdlib>
int main()
{
int* a = new int(20);
cout << "Address of new int: " << a << endl;
delete a;
long* b = new long(10034340);
cout << "Address of new double: " << b << endl;
delete b;
char* c = new char [100];
c[0] = 'a';
c[1] = 'b';
cout << "Address of new char array: " << c << endl;
delete [] c;
int* d = new int [100];
d[0] = 2;
d[1] = 3;
cout << "Address of new int array: " << d << endl;
delete [] d;
std::system("PAUSE");
return 0;
}
and here is the output:
Address of new int: 0x26704f0
Address of new double: 0x2670520
Address of new char array: ab
Address of new int array: 0x26705a0
Press any key to continue . . .
The oddity is the "ab" output. What I expected was for a new array of
100 chars to be allocated, and the address of the new array to be
passed to c. Thus, I would expect outputting c to cout would print the
address, as it seems to for the other cases. Instead it seems to print
the contents of the array. This doesn't seem to apply for the int
array.
I've tried de-referencing a, b, c and d, and I get the contents of the
new int and long, and the value of the first element (c[0] = 'a', and
d[0] = 2), in the case of the arrays, which is what I would expect, as
I gather arrays are in effect just pointers to the first element.
Can anyone explain what is going on here? Am I doing something wrong?
Thanks
John Hicken