Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This question requires a bit of theory that i don't have.
First off, an array is basically the pointer to the first element in the array definition, and depending on the index specified, it can reference all of the data sequentially after it. Correct?
C++
int array[5];
Which is why we can just declare a pointer, and then allocate an array of values to it afterwards like so:
C++
int * array;
array = new int[5];
So now, why is it that when I pass an int pointer like to a function, allocate the space there and then after the function has run try to access those values, I get an access violation error?

Here's an example:
C++
#include <iostream>

void function( int * Array)
{
     Array = new int[5];
     for(int i = 0; i < 5; i++)
         Array[i] = 0;           // works fine here
}

int main(int argc, char * argv[])
{
     int * Array = NULL;
     function(Array);
     Array[3] = 2;               // access violation here
     system("PAUSE");
     return 0;

} 
Posted
Updated 2-Oct-11 8:15am
v4
Comments
Philippe Mori 1-Oct-11 19:30pm    
A much easier way is to uses STL vector instead.
André Kraak 2-Oct-11 14:16pm    
Edited question:
Added pre tags
Formatted text/code
Spelling/Grammar

That's because a pointer e.g. int* is essentially an integer (a location in memory) when you pass this pointer to your function you pass the integer of the pointer and not the address of the pointer.
So inorder to change the value of a pointer in your function you'll have to pass the address of the pointer and not the value it would look like this:

C++
#include <iostream>
void function(int ** Array)
{
     *Array = new int[5];
     for(int i = 0; i < 5; i++)
         Array[i] = 0;           
}


int main(int argc, char * argv[])
{
     int * Array = NULL;
     function(&Array);
     Array[3] = 2;               
     system("PAUSE");
     return 0;
}


I find it hard to explain this, so instead of sitting here and rewrite this 5 time, I'll just let you ask any questions you have regarding my explanation or the updated source code I posted, and I'll try to go into more detail.


Just found this article Pointer to Pointer and Reference to Pointer[^] I haven't read it myself but it seems to go over double pointers in more detail. In any case it doesn't hurt to have multiple sources of information :)
 
Share this answer
 
v4
Comments
Philippe Mori 1-Oct-11 19:29pm    
Well, an ampersand is missing in your call to function. It should be function(&Array);
Simon Bang Terkildsen 1-Oct-11 19:30pm    
Thank you, that's what I get for not entering the code in VS first :D
FatalCatharsis 1-Oct-11 19:31pm    
ah, i see what your saying, at first your wording confused me a little, but i understand what you mean after reading the article. just like passing a variable to a function, passing a pointer, just copies the pointer. so to actually manipulate the pointer in a function like you would manipulate the variable in a function, you have to pass it by reference. thanks for the help :P
Simon Bang Terkildsen 1-Oct-11 19:33pm    
My pleasure, I actually think you just explained it better than I did :)
Lets write your code with a little bit different style:
#include <iostream>

void function( int* Array)
{
     Array = new int[5];
     for(int i = 0; i < 5; i++)
         Array[i] = 0;           // works fine here
}
 

int main(int argc, char* argv[])
{
     int* Array = NULL;
     function(Array);
     Array[3] = 2;               // access violation here
     system("PAUSE");
     return 0;
 
}

(just put the * close to int, so that it is clear that int* is a type name)
and now compare it to this one:
#include <iostream>

void function( int val)
{
   val = 5;
}
 

int main(int argc, char * argv[])
{
     int val =0;
     function(val);
     std::cout << val << std::endl;
     system("PAUSE");
     return 0;
 
}


Are you surprised in seeing "0" as a result, instead of 5?

Now, your function, int* array is a value of type "pointer to int", that -on function call- points to the same location as the main Array (NULL in this case) but, inside function you changed the pointer to point to something else (new int[5], in your case), than you assign values to the pointed array.
But inside your function there is nothing that makes main to now about the change you did: Array, in main is still pointing to NULL, and you try to write int NULL+2.

To solve the problem you should allocate in main, than call function, and inside the function, use the pointer to assign the values to the elements.
Or, pass a reference to the pointer (so that if you change it, the "original" is also changed:

#include <iostream>

void function( int*& Array) //note the &
{
     Array = new int[5];
     for(int i = 0; i < 5; i++)
         Array[i] = 0;           
}
 

int main(int argc, char * argv[])
{
     int * Array = NULL;
     function(Array);
     Array[3] = 2;               
     system("PAUSE");
     delete[]Array;      //required, or will result in a memory leak.        
     return 0;
 
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900