trick...@gmail.com wrote:
> In my main I try to get values assigned to "int *thisSoduko" through
> the static method Reader::read.
>
> int main(char *args[])
> {
> ...
> int *thisSoduko = new int[1];
> if(Reader::read( fileName, delimiter, thisSoduko, size))
> {
> std::cout << size;
> for(int i = 0; i < size; i++)
> {
> std::cout << "," << thisSoduko[i];
> }
> std::cout << std::endl;
> }
> return 1;
> }
>
> in the Reader::read I let the pointer point to a new array with the
> desired length:
>
> thisSoduko = new int[size];
>
> and now I store the size number of appropriate integers.
>
> In the end of Reader::read the correct values are stored in
> "thisSoduko". This is tested with an equal for-loop as the one seen
> here in main. My problem is that the array does not contain the correct
> values here.
>
> Where does the pointer / assignment / bring over of values go wrong?
>
> Thanks
>
> TASD
We can't tell with what you posted, but I'm guessing that you're not
passing the pointer to Reader::read as a reference. So, basically the
compiler copies the current address to a local pointer, then you change
that local copy with your new allocation, and at the end of the
function, discard the change. Consequently, your pointer back in main
still points to the old memory that you allocated, and the memory
allocated in Reader::read() is leaked. If you had used a reference to a
pointer, you would have leaked the memory allocated in main because you
never deallocated it. In any case, you have *no* deallocations
whatsoever, so you're bound for trouble.
Try using a std::vector instead
(
http://www.parashift.com/c++-faq-lit...html#faq-34.1). It
simplifies the whole memory management process, and the syntax for
passing by reference is less obscure.
Cheers! --M