arrays

A

Amit Gulati

Why is this valid in C++ ??

int *i = new int[];

I can access upto 4 elements using the int pointer.

If the dimension is not specified, then how many elements
are created??

Thanks

Amit
 
L

Leor Zolman

Why is this valid in C++ ??

int *i = new int[];

It isn't. MSVC, and perhaps some other implementations, allow it for the
benefit of legacy hacks (or perhaps simply because they haven't taken the
trouble to /disallow/ it, in the case where common internal compiler
machinations process both function array arguments and array-new
constructs), but it is not valid C++.
-leor
 
I

Ivan Vecerina

Amit Gulati said:
Why is this valid in C++ ??

int *i = new int[];
I don't think it is valid in ISO C++.
Which compiler accepts this syntax?
Did you try ANSI-strict mode?
I can access upto 4 elements using the int pointer.

Which might result from the fact that your platform
allocates memory blocks of at least 16 bytes (int being 4 bytes).

If the dimension is not specified, then how many elements
are created??

The new expression above is most likely returning, when accepted
by a compiler, an empty array. So dereferencing the returned
pointer will lead to undefined behavior.


Regards,
Ivan
 
A

Amit Gulati

Leor said:
Why is this valid in C++ ??

int *i = new int[];


It isn't. MSVC, and perhaps some other implementations, allow it for the
benefit of legacy hacks (or perhaps simply because they haven't taken the
trouble to /disallow/ it, in the case where common internal compiler
machinations process both function array arguments and array-new
constructs), but it is not valid C++.
-leor

I can access upto 4 elements using the int pointer.

If the dimension is not specified, then how many elements
are created??

Thanks

Amit
Thanks again!!
 

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
474,169
Messages
2,570,916
Members
47,458
Latest member
Chris#

Latest Threads

Top