i'm new to serious programming and have been stumped by this weird behavior all morning
in a nutshell: i pass a pointer in main to a public class function. it sets the pointer to point at a private dynamical class array and returns the array's length. inside the function, everything is fine -- i can access the elements of the array using the pointer. but once i'm back in main, i try to access the values using the pointer, but i see garbage.
i will try and attach the relevant bits of code.
-------------------------------------------------------------------
from "synapses.h"
Code:
class synapses {
private:
int actcount[SIZE]; //contains the current length of each dynamical array actsyn[i]
int *actsyn[SIZE]; //each of these points to a dynamical array initialized with "new"
//SIZE is a global int
public:
int getpost(char p, int pre, int *post);
//More declarations....
};
int synapses::getpost(char p, int pre, int *post){
int res = -1;
switch(p){
case 'a':
post = actsyn[pre]; //set pointer to point at dynamical array initalized with "new"
res=actcount[pre]; //length of dynamical array
break;
case 's':
post = supsyn[pre];
res=supcount[pre];
break;
default: cout<<"synapses.getpost called with invalid char arg"<<endl;
exit(1);
}
for (int i=0; i<res; i++){
cout<<post[i]<<" "; //**This statement prints the expected values
}
return res;
}
from "main"
Code:
int *wack;
int foo = (*connectivity).getpost('a',goo,wack); //goo is an int
//connectivity is a pointer to a synapses object
for(int k = 0; k<foo; k++){
cout<<wack[k]<<" "; //**This is the statement that prints garbage
}