Elements in dynamically allocated integer array

Hey,

so I have simple problem with dynamically allocated array of integers when counting 'em. Result is '1' always which is obvious because sizeof x is 4 and when divided by 4 = 1.
So is there way to get sizeof whole array?

1
2
3
4
5
6
7
8
9
#include<iostream>
using namespace std;

void main(void)
{
int *x=new int [];
*x=1;*(x+1)=2;*(x+2)=3;
int elm = (sizeof x)/(sizeof *(x+1));
}


And I know that this works when array is created like "int array[]" -- but since i'm learning want to try this way.
This is incorrect statement

int *x=new int [];

I do not know why the compiler allows you to write such construction. You should explicitly specify the number of elements of the array you are allocating.

Also this statement have no any sense because sizeof( int *) / sizeof( int ) is always usually equal to 1

int elm = (sizeof x)/(sizeof *(x+1));

Function main shall be defined as

int main()
Last edited on
I know that this works when array is created like "int array[]"

It doesn't work. You have to provide a number.

int *x=new int [someValue];

Similarly, void main(void) isn't C++

What are you using to compile this code? The compilers I have to hand all refuse to deal with it.
Last edited on
Oh, I see now -- at least this worked with strings so confused me little bit.

And Ya, I know that int main() is the way to define main function but just experimenting all kind of stuff for fun..and using visual studio 2010
Topic archived. No new replies allowed.