memory allocation for local variables

J

James Kanze

Allocating array[] with *new is standard,
I need more explanation plz.you mean like this?
size_t sz;
int array[]= * new int [sz];
this looks odd to me !!!!!!!!!!so ,wont one have to delete the
array explicitly?

Of course. And that wouldn't be the syntax.

In context, the * is really just a typo---what I was thinking
about was:

int* pArray = new int[ size ] ;

Something like
int& rFirst = *new int[ size ] ;
// ...
delete [] &rFirst ;
is also legal, but not very useful, since the only way to access
beyond the first element is to take the address of rFirst, and
then do pointer arithmetic on it.

Something like:

int (&array)[ size ]
= reinterpret_cast< int(&)[ size ] >( *new int[ size ] ) ;
// ...
delete [] &array ;

is also legal, if size is a constant, but frankly, I wouldn't
recommend it.

When all is said and done, of course, if you are allocating
arrays dynamically, and their lifetime corresponds to some sort
of scope, there is absolutely no reason not to use std::vector.
If the lifetime is truely dynamic, then you can usually use
std::vector as well, but there will be cases where it will
introduce an additional indirection.
I used to think that new was used for pointer to arrays.

You mean that new of an array returns a pointer to the first
element, and not a pointer to the array. That's true (except
that the first element is guaranteed to have the same address as
the array, so my reinterpret_cast above works).
 
T

terminator

Allocating array[] with *new is standard,
I need more explanation plz.you mean like this?
size_t sz;
int array[]= * new int [sz];
this looks odd to me !!!!!!!!!!so ,wont one have to delete the
array explicitly?

Of course.  And that wouldn't be the syntax.

In context, the * is really just a typo---what I was thinking
about was:

    int* pArray = new int[ size ] ;

Something like
    int& rFirst = *new int[ size ] ;
    //  ...
    delete [] &rFirst ;
is also legal, but not very useful, since the only way to access
beyond the first element is to take the address of rFirst, and
then do pointer arithmetic on it.

that is what i am used to accept.
Something like:

    int (&array)[ size ]
        = reinterpret_cast< int(&)[ size ] >( *new int[ size ] ) ;
    // ...
    delete [] &array ;

is also legal, if size is a constant, but frankly, I wouldn't
recommend it.

nor do many.
When all is said and done, of course, if you are allocating
arrays dynamically, and their lifetime corresponds to some sort
of scope, there is absolutely no reason not to use std::vector.
If the lifetime is truely dynamic, then you can usually use
std::vector as well, but there will be cases where it will
introduce an additional indirection.


You mean that new of an array returns a pointer to the first
element, and not a pointer to the array.  That's true (except
that the first element is guaranteed to have the same address as
the array, so my reinterpret_cast above works).
yes.

thanks,
FM.
 
R

Richard Herring

John Brawley said:
John said:
I don't know if this is the same idea, but I do exactly this
with an array[]. My program can't grab the necessary memory
until the user enters a number so the program can provide it
for what comes later. I was told the only two ways to do
this were an array[] and a vector[], and I had to create the
array[] with *new.
It's an extension. It's not standard, and so it is not
portable.

I think there's some confusion here. Variable length arrays in
C++ are a g++ extension---as far as I know, no other C++ compiler
implements them. Allocating array[] with *new is standard,
however.
Nobody seems to have posted the obvious (FAQ) response to clarify the
alleged confusion. Namely:

"Please post an example of minimal, complete, compileable code that
illustrates what you are trying to do."

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
 

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,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top