Storing values to array

Discussion in 'C++' started by trickish@gmail.com, Nov 9, 2005.

  1. Guest

    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
     
    , Nov 9, 2005
    #1
    1. Advertising

  2. Default User Guest

    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;
    > }
    > 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.
     
    Default User, Nov 9, 2005
    #2
    1. Advertising

  3. mlimber Guest

    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;
    > }
    > 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
     
    mlimber, Nov 9, 2005
    #3
  4. 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;
    > }
    > 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
     
    John Harrison, Nov 9, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rajus
    Replies:
    12
    Views:
    594
    Al Balmer
    Oct 21, 2006
  2. toton
    Replies:
    11
    Views:
    742
    toton
    Oct 13, 2006
  3. Jonathan Wood
    Replies:
    1
    Views:
    531
    Jonathan Wood
    Jun 2, 2008
  4. Jen
    Replies:
    3
    Views:
    237
    Robert Klemme
    Mar 23, 2011
  5. Go Perl
    Replies:
    3
    Views:
    113
    Tad McClellan
    Oct 21, 2003
Loading...

Share This Page