new int?

T

thomas

#include<iostream>
using namespace std;

int main(){
int *x = new int[10];
cout<<sizeof(x)/sizeof(int)<<endl;
for(int i=0;i<20;i++) cout<<x<<" ";
cout<<endl;
}

********output:******
1
3670712 3670712 0 0 0 0 0 0 0 0 393219 524787 4301368 1 0 0 196610
524748 369016
8 3690232

while I expected:
10
??? error?
 
M

Martin York

#include<iostream>
using namespace std;

int main(){
int *x = new int[10];
cout<<sizeof(x)/sizeof(int)<<endl;
for(int i=0;i<20;i++) cout<<x<<" ";
cout<<endl;

}

********output:******
1
3670712 3670712 0 0 0 0 0 0 0 0 393219 524787 4301368 1 0 0 196610
524748 369016
8 3690232

while I expected:
10
??? error?


sizeof(x) returns the sizeof(int*):
the number of bytes used by a pointer to int.
sizeof(int)
the number of bytes used by a int.

It looks like the size of the pointer is the same size of an int on
your system. What you wanted was the size of what x pointed at.
Unfortunately thats not possible via a pointer. What you may have
wanted to do was:

int main()
{
int x[10];
cout<<sizeof(x)/sizeof(int)<<endl;
}
 
M

Martin York

#include<iostream>
using namespace std;

int main(){
int *x = new int[10];
cout<<sizeof(x)/sizeof(int)<<endl;
for(int i=0;i<20;i++) cout<<x<<" ";
cout<<endl;

}

********output:******
1
3670712 3670712 0 0 0 0 0 0 0 0 393219 524787 4301368 1 0 0 196610
524748 369016
8 3690232

while I expected:
10
??? error?


Also note:
1) <emory allocated by new int[10] is un-initialized. ie it contains
random data. Hence the list of random numbers that you printed out.
2) Accessing beyond the end of the array is undefined behavior so
even though it managed to print out random numbers it is just as
likely to crash the program.
3) You should de-allocate the memory with delete [] x;
 
K

Keith Halligan

[snip]

while I expected:
10
??? error?

I assume you mean in the sizeof(x)/sizeof(int) line?

The reason you don't get 10 here and you get 1 is that sizeof operator
will not work on dynamically allocated storage such as pointers. It
will just return the size of the pointer in bytes so in this case
it'll return 4 (if on a 32-bit system). sizeof only works properly on
stack variables, so if you had an array of 10 ints

eg. int x[10];

then

cout << sizeof(x) << endl;

would return 10

You'll just have to store the size of the dynamically allocated array,
there's no way to uniquely find the length of the array. If using
strings, you can use strlen if the array is null terminated. But
otherwise no, just store the length.
 
J

Juha Nieminen

Martin said:
3) You should de-allocate the memory with delete [] x;

Even better, you should avoid the 'new' altogether (if possible) and
use "std::vector<int> x(10);" instead.
 
J

Jim Langston

thomas said:
#include<iostream>
using namespace std;

int main(){
int *x = new int[10];
cout<<sizeof(x)/sizeof(int)<<endl;

What is x? x is an int*. What is the size of a pointer? On your system it
appears to be the size of an int (guessing 4). So this is outputting
cout << 4 / 4 << endl;

There is no way to know how much data a pointer is pointing to. You have to
store that value somewhere or get the information some other way.
for(int i=0;i<20;i++) cout<<x<<" ";
cout<<endl;
}

********output:******
1
3670712 3670712 0 0 0 0 0 0 0 0 393219 524787 4301368 1 0 0 196610
524748 369016
8 3690232

while I expected:
10
??? error?
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top