By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,514 Members | 1,801 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,514 IT Pros & Developers. It's quick & easy.

Storing values to array

P: n/a
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

Nov 9 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
tr******@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];


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

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Nov 9 '05 #2

P: n/a
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

Nov 9 '05 #3

P: n/a
tr******@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?


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
Nov 9 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.