Newbie question - Dynamic memory and pointer oddity

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
 
L

Lance Diduck

Can anyone explain what is going on here? Am I doing something wrong?

Thanks

John Hicken
Not doing anything wrong. cout is an ostream, and ostream assume that
any char * is really a null terminated string, and should treat as
such.
If it did not, then thing like
cout<<"Print Me";
would simply print out whatever address the compiler happened to stuff
the literal string in.

The only thing that is really "wrong" is that new char[100]; does not
automatically pad the unused spaces with 0. It just happened to in your
situation: in others cases you may have seen tons of garbage output
after the "ab"
 
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

I think I've figured it out. c is treated as a character array, and
thus it gets printed as a string, with a null byte terminating it. With
the other array, it just gets treated as a pointer, with the address
printed out.

John Hicken
 
J

john.hicken

Lance said:
Can anyone explain what is going on here? Am I doing something wrong?

Thanks

John Hicken
Not doing anything wrong. cout is an ostream, and ostream assume that
any char * is really a null terminated string, and should treat as
such.
If it did not, then thing like
cout<<"Print Me";
would simply print out whatever address the compiler happened to stuff
the literal string in.

The only thing that is really "wrong" is that new char[100]; does not
automatically pad the unused spaces with 0. It just happened to in your
situation: in others cases you may have seen tons of garbage output
after the "ab"

Thanks for the reply. I was coming to that sort of conclusion, as you
can see by my other response, but I wasn't thinking in terms of
ostreams. I think it's starting to make more sense now.

John Hicken
 
M

Mike Wahler

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

I think I've figured it out. c is treated as a character array, and
thus it gets printed as a string, with a null byte terminating it.

Yes, but note that, as Lance pointed out, your string was zero-terminated
only by accident in your code. For reliable, defined behavior, you should
have written:
c[0] = 'a';
c[1] = 'b';
c[2] = 0;
With
the other array, it just gets treated as a pointer, with the address
printed out.

In all cases, what you're passing to 'cout' are pointers. It's just that
the '<<' function overload whose parameter is (char *) outputs the
zero-terminated string starting at that address, while the others output
the address itself.

If you want to see the address stored in 'c', you can convert it to
another pointer type, i.e. void* :

std::cout << static_cast<void*>(c) << '\n';

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top