View Single Post
Old 04-18-2005, 09:44 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
int * MyArray = getArray();
Now i'm wondering: If ever Array changes, MyArray will change either?
Yes. MyArray is just a pointer pointing to the same starting address of Array.
Below is an example:
Code:
#include <iostream>
#include <cstdlib> //size_t


//-
int TheArray[5];
//-

void init_array(int*, int);
const int* get_array();
void print_array(const int*);
//--------------


int main()
{
  init_array(TheArray, 0);
  
  const int* OtherArray;  
  OtherArray = get_array();
  print_array(OtherArray);
  
  init_array(TheArray, 5);
  print_array(OtherArray);
  
  std::cin.get();
  return 0;
}

//-----------

void init_array(int* itsArray, int initval)
{
  for(std::size_t j = 0; j < 5; ++j)
  {
    itsArray[j] =  initval+j;
  }  
}

//-----------

const int* get_array()
{
  return TheArray;
}

//-----------

void print_array(const int* itsArray)
{
  for(std::size_t j = 0; j < 5; ++j)
  {
    std::cout<<itsArray[j]<<std::endl;
  }  
}
Let's explain how come.

This is how it all looks in memory.
1) First we create and initialize the original array:
The square brackets is the DATA and the top bracket is index 0.
Code:
int TheArray[5];

TheArray----->[0]
              [1]
              [2]
              [3]
              [4]
On the next step, pointer OtherArray is created:
Code:
const int* OtherArray;

OtherArrayArray----->...
Then, OtherArray points to the same data (or object) as TheArray:
Code:
OtherArray = get_array();

TheArray----->[0]<-----OtherAray (const)
              [1]
              [2]
              [3]
              [4]
Then the first array TheArray has its contents changed somehow:
Code:
init_array(TheArray, 5);

TheArray----->[5]<-----OtherAray (const)
              [6]
              [7]
              [8]
              [9]
But, OtherArray is pointing to the very same data! So accessing data through OtherArray will result in the very same data as TheArray points to.

However, changing the contents through OtherArray...
Code:
init_array(OtherArray, 5);
... is illegal, since OtherArray is declared const. But that is optional. I only did it for demonstrational purposes.

Quote:
then how else can i force a function to return a dynamic array whose values will stay always the same?
- If it is illegal to modify the contents through OtherArray then declare it const as we did it.
- If you have to modify the values through OtherArray, but don't want to touch the original contents of TheArray, then make a shallow copy: copy each element to the new array:
Code:
//Calculate "size" first.
  int* MyArray = new int[size];
  for(std::size_t j=0; j < size; ++j)
  {
    MyArray[j] = Array[j];
  }
  //Modify "MyArray" here. Original "Array" will be untouched.
__________________

Last edited by Valmont; 04-19-2005 at 03:36 AM.
Valmont is offline   Reply With Quote