Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-18-2005, 07:05 AM   #1 (permalink)
lostromos
Registered User
 
Join Date: Apr 2005
Posts: 5
lostromos is on a distinguished road
Function returning pointer

Hi everybody,

I think i have a complete mess in my head and i would be grateful if smb could clarify things a little.

All i want from a function is to give me back a dynamic array. But i cannot easy manipulate what this function returns. So, lets say :
int * getArray(void) { return Array;}
Somewhere in my prog, i have the following:
int * MyArray = getArray();
Now i'm wondering: If ever Array changes, MyArray will change either?

If it's the case (if with a "get" function that returns a dynamic array i dont get a constant result but a variable one, depending on "Array") then how else can i force a function to return a dynamic array whose values will stay always the same?

I hope i was clear with my question, although it's not clear at all in my head.

Thanks for your patience,

George
lostromos is offline   Reply With Quote
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
Old 04-19-2005, 01:25 AM   #3 (permalink)
lostromos
Registered User
 
Join Date: Apr 2005
Posts: 5
lostromos is on a distinguished road
Valmont, your answer was clear and you have been of great help.

I understand that in my case the following should be done:

Code:
int * VarArray = getArray();   /* VarArray points to the contents of what getArray returns */
const int * MyArray = new int [size]; /* I have to create a new array where i'll stock the VarArray's data. However, since there are no places reserved (as in the case of VarArray) i have to reserve "size" places in memory. I'm also not sure if "const" is ok, but i can check this */
for(int i=0; i<size; i++)
  MyArray[i] = VarArray[i];  /* MyArray will remain like this for ever after */
.............................
delete [] MyArray;
return "Thanks one more Valmont"
George
lostromos is offline   Reply With Quote
Old 04-19-2005, 03:34 AM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Well done.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array to pointer to pointer frier Standard C, C++ 8 03-25-2005 07:57 PM
Java help?? d00ds?? zergmuncher Java 19 11-15-2003 03:35 AM
pointer to function with class? Kportertx Standard C, C++ 5 04-11-2003 06:12 AM
pass filename to function Blaqb0x Standard C, C++ 1 09-02-2002 11:12 PM


All times are GMT -8. The time now is 07:30 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting