Storing values to array

T

trickish

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;
}
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
 
D

Default User

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;
}
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];


Show all the code. We need to see at least what the signature is for
read(). I suspect that it takes an int *, which means the new memory
gets lost because pointers are passed by value. Common enough error.

Just scrap all that and use a vector of ints, pass that into read() via
a reference.


Brian
 
M

mlimber

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;
}
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-lite/containers.html#faq-34.1). It
simplifies the whole memory management process, and the syntax for
passing by reference is less obscure.

Cheers! --M
 
J

John Harrison

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;
}
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?


Because thisSuduko in Reader:read and thisSudoku in main are two
different variables. Allocating memory for thisSuduko in Reader::read
has no affect at all on thisSudoko in main which stays at size 1.

Look at this code

int main()
{
int x = 1;
f(x);
cout << x;
}

void f(int x)
{
x = 2;
}

what do you think that will print 1 or 2? 1 of course, the x in main is
different from this x in f.

It's no different for pointers.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top