In article <ljjRf.13636$Tf3.4543@dukeread09>,
"cdg" <> wrote:
> I was just asking, what is the best way to return an array (not vector)
> from a function to a function that is in the program before the array is
> declared.
You can't return an array from a function, you return a pointer to an
element in an array (usually the first element.)
> Is this a situaton that requires the "new" statement for dynamically
> allocated memory.
> Or would declaring a global array before "main" be a better approach.
Humm... I expect that for someone new to the language the global would
be less error prone, but if the size of the program grows much bigger
than a page, it wouldn't be better IMHO.
> I am not interested in vectors with this. I am just trying to understand
> the problem.
The problem is, you can't return an array. Arrays don't really exist in
C++, there are vectors, and there are blocks of memory which you can
dereference with [] so that they look like arrays.
Of the two, vector is more like an array because it supports value
semantics (for eg. you can return one.)
Blocks of memory (like you used in your sample program,) can't be
returned, they just sit in RAM waiting for code to modify them. You can
let functions know where the block of memory is by passing a pointer
into it, but you can't move the block once it is created.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
|