Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Storing values to array

Reply
Thread Tools

Storing values to array

 
 
trickish@gmail.com
Guest
Posts: n/a
 
      11-09-2005
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

 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      11-09-2005
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.
 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      11-09-2005
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

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      11-09-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Counting values in an array, storing in a hash then making an arrayof hashes? Jen Ruby 3 03-23-2011 12:11 PM
User Images: Storing in Files VS Storing in Database Jonathan Wood ASP .Net 1 06-02-2008 05:56 PM
Storing large number of values in 2D array rajus C Programming 12 10-20-2006 11:33 PM
storing pointer vs storing object toton C++ 11 10-13-2006 11:08 AM
storing values from file into an array.-newbie Go Perl Perl Misc 3 10-20-2003 11:59 PM



Advertisments