Passed value not being received

Discussion in 'C++' started by Lilith, Dec 19, 2007.

  1. Lilith

    Lilith Guest

    I have a simple class method that receives as its second parameter an
    unsigned int. But, regardless of what I do it never receives the
    value that I pass to it. Instead it always comes up 4198769. I've
    passed the number as a literal, as a variable and as a constant and
    the method still recives the larger, unviable value.

    I've tried changing the value I pass to it and I've tried casting a
    constant and it still comes out wrong. I can't imagine a compler (VS
    2005) being broken enough to do this.

    Any thoughts on this? 4198769 doesn't seem to be relevant to
    anything.

    --
    Thanks,
    Lilith
     
    Lilith, Dec 19, 2007
    #1
    1. Advertising

  2. Lilith

    Ian Collins Guest

    Lilith wrote:
    > I have a simple class method that receives as its second parameter an
    > unsigned int. But, regardless of what I do it never receives the
    > value that I pass to it. Instead it always comes up 4198769. I've
    > passed the number as a literal, as a variable and as a constant and
    > the method still recives the larger, unviable value.
    >
    > I've tried changing the value I pass to it and I've tried casting a
    > constant and it still comes out wrong. I can't imagine a compler (VS
    > 2005) being broken enough to do this.
    >
    > Any thoughts on this? 4198769 doesn't seem to be relevant to
    > anything.
    >

    Post a minimal example that demonstrates the problem. You will probably
    fix the problem in the process of constructing the example :)

    --
    Ian Collins.
     
    Ian Collins, Dec 19, 2007
    #2
    1. Advertising

  3. Lilith

    Lilith Guest

    On Thu, 20 Dec 2007 11:27:44 +1300, Ian Collins <>
    wrote:

    >Lilith wrote:
    >> I have a simple class method that receives as its second parameter an
    >> unsigned int. But, regardless of what I do it never receives the
    >> value that I pass to it. Instead it always comes up 4198769. I've
    >> passed the number as a literal, as a variable and as a constant and
    >> the method still recives the larger, unviable value.
    >>
    >> I've tried changing the value I pass to it and I've tried casting a
    >> constant and it still comes out wrong. I can't imagine a compler (VS
    >> 2005) being broken enough to do this.
    >>
    >> Any thoughts on this? 4198769 doesn't seem to be relevant to
    >> anything.
    >>

    >Post a minimal example that demonstrates the problem. You will probably
    >fix the problem in the process of constructing the example :)


    Prototype within the class:
    void DrawBlock (point p, unsigned int size, sf::Color c);

    Method:
    void Roads::DrawBlock (point p, unsigned int size, sf::Color c)
    // size always shows up as 4198769
    {
    int left = p.x - size/2;
    int, top = p.y - size/2;

    for (int i = top; i < top + size; i++) {
    for (int j = left; j < left + size; j++) {
    canvas->SetPixel(j, i, c);
    }
    }

    }

    Call to the method:
    ..
    ..
    const unsigned int siz = 5;
    ..
    ..
    fin.x = 80;
    fin.y = 20;
    ..
    ..

    Roadway.DrawBlock (fin, siz, Red);

    It's the size that keeps showing up with the same value regardless of
    what I pass it in the call.

    --
    Lilith
     
    Lilith, Dec 19, 2007
    #3
  4. Lilith

    werasm Guest

    On Dec 19, 11:42 pm, Lilith <> wrote:
    > On Thu, 20 Dec 2007 11:27:44 +1300, Ian Collins <>
    > wrote:
    >
    >
    >
    > >Lilith wrote:
    > >> I have a simple class method that receives as its second parameter an
    > >> unsigned int. But, regardless of what I do it never receives the
    > >> value that I pass to it. Instead it always comes up 4198769. I've
    > >> passed the number as a literal, as a variable and as a constant and
    > >> the method still recives the larger, unviable value.

    >
    > >> I've tried changing the value I pass to it and I've tried casting a
    > >> constant and it still comes out wrong. I can't imagine a compler (VS
    > >> 2005) being broken enough to do this.

    >
    > >> Any thoughts on this? 4198769 doesn't seem to be relevant to
    > >> anything.

    >
    > >Post a minimal example that demonstrates the problem. You will probably
    > >fix the problem in the process of constructing the example :)

    >
    > Prototype within the class:
    > void DrawBlock (point p, unsigned int size, sf::Color c);
    >
    > Method:
    > void Roads::DrawBlock (point p, unsigned int size, sf::Color c)
    > // size always shows up as 4198769
    > {
    > int left = p.x - size/2;
    > int, top = p.y - size/2;
    >
    > for (int i = top; i < top + size; i++) {
    > for (int j = left; j < left + size; j++) {
    > canvas->SetPixel(j, i, c);
    > }
    > }
    >
    > }
    >
    > Call to the method:
    > .
    > .
    > const unsigned int siz = 5;
    > .
    > .
    > fin.x = 80;
    > fin.y = 20;
    > .
    > .
    >
    > Roadway.DrawBlock (fin, siz, Red);


    I don't see an obvious problem. You are passing by value.
    There are no implicit conversions (not that it would
    matter). Perhaps try this minimal example:

    #include <iostream>

    void DrawBlock( unsigned size )
    {
    std::cout << "size" << std::endl;
    }

    int main()
    {
    const unsigned sz( 5 );
    DrawBlock( sz );
    }

    Then see what is printed. If the value is still as mentioned,
    I suggest you change the name of the argument "size" to something
    else, as it may be that it is macro expanded (the only other thing
    I can think of).

    Regards,

    Werner
     
    werasm, Dec 19, 2007
    #4
  5. Lilith

    Salt_Peter Guest

    On Dec 19, 5:42 pm, Lilith <> wrote:
    > On Thu, 20 Dec 2007 11:27:44 +1300, Ian Collins <>
    > wrote:
    >
    >
    >
    > >Lilith wrote:
    > >> I have a simple class method that receives as its second parameter an
    > >> unsigned int. But, regardless of what I do it never receives the
    > >> value that I pass to it. Instead it always comes up 4198769. I've
    > >> passed the number as a literal, as a variable and as a constant and
    > >> the method still recives the larger, unviable value.

    >
    > >> I've tried changing the value I pass to it and I've tried casting a
    > >> constant and it still comes out wrong. I can't imagine a compler (VS
    > >> 2005) being broken enough to do this.

    >
    > >> Any thoughts on this? 4198769 doesn't seem to be relevant to
    > >> anything.

    >
    > >Post a minimal example that demonstrates the problem. You will probably
    > >fix the problem in the process of constructing the example :)

    >
    > Prototype within the class:
    > void DrawBlock (point p, unsigned int size, sf::Color c);
    >
    > Method:
    > void Roads::DrawBlock (point p, unsigned int size, sf::Color c)
    > // size always shows up as 4198769
    > {
    > int left = p.x - size/2;
    > int, top = p.y - size/2;
    >
    > for (int i = top; i < top + size; i++) {
    > for (int j = left; j < left + size; j++) {
    > canvas->SetPixel(j, i, c);
    > }
    > }
    >
    > }
    >
    > Call to the method:
    > .
    > .
    > const unsigned int siz = 5;
    > .
    > .
    > fin.x = 80;
    > fin.y = 20;
    > .
    > .
    >
    > Roadway.DrawBlock (fin, siz, Red);
    >
    > It's the size that keeps showing up with the same value regardless of
    > what I pass it in the call.
    >
    > --
    > Lilith



    We can only guess at where the problem is.
    Post compileable code something alongs these lines:

    #include <iostream>

    struct point
    {
    int x;
    int y;
    point(int x_, int y_) : x(x_), y(y_) { }
    };

    class Roads
    {
    public:
    Roads() { }
    void DrawBlock(const point& p, const unsigned int size)
    {
    std::cout << "p.x = " << p.x << std::endl;
    std::cout << "p.y = " << p.y << std::endl;
    std::cout << "size = " << size << std::endl;
    }
    };

    int main()
    {
    point p(11,22);
    Roads roadway;
    roadway.DrawBlock(p, 5);
    }

    /*
    p.x = 11
    p.y = 22
    size = 5
    */
     
    Salt_Peter, Dec 19, 2007
    #5
  6. Lilith

    Jim Langston Guest

    werasm wrote:
    > On Dec 19, 11:42 pm, Lilith <> wrote:
    >> On Thu, 20 Dec 2007 11:27:44 +1300, Ian Collins
    >> <> wrote:
    >>
    >>
    >>
    >>> Lilith wrote:
    >>>> I have a simple class method that receives as its second parameter
    >>>> an unsigned int. But, regardless of what I do it never receives
    >>>> the value that I pass to it. Instead it always comes up 4198769.
    >>>> I've passed the number as a literal, as a variable and as a
    >>>> constant and the method still recives the larger, unviable value.

    >>
    >>>> I've tried changing the value I pass to it and I've tried casting a
    >>>> constant and it still comes out wrong. I can't imagine a compler
    >>>> (VS 2005) being broken enough to do this.

    >>
    >>>> Any thoughts on this? 4198769 doesn't seem to be relevant to
    >>>> anything.

    >>
    >>> Post a minimal example that demonstrates the problem. You will
    >>> probably fix the problem in the process of constructing the example
    >>> :)

    >>
    >> Prototype within the class:
    >> void DrawBlock (point p, unsigned int size, sf::Color c);
    >>
    >> Method:
    >> void Roads::DrawBlock (point p, unsigned int size, sf::Color c)
    >> // size always shows up as 4198769
    >> {
    >> int left = p.x - size/2;
    >> int, top = p.y - size/2;
    >>
    >> for (int i = top; i < top + size; i++) {
    >> for (int j = left; j < left + size; j++) {
    >> canvas->SetPixel(j, i, c);
    >> }
    >> }
    >>
    >> }
    >>
    >> Call to the method:
    >> .
    >> .
    >> const unsigned int siz = 5;
    >> .
    >> .
    >> fin.x = 80;
    >> fin.y = 20;
    >>
    >> Roadway.DrawBlock (fin, siz, Red);

    >
    > I don't see an obvious problem. You are passing by value.
    > There are no implicit conversions (not that it would
    > matter). Perhaps try this minimal example:
    >
    > #include <iostream>
    >
    > void DrawBlock( unsigned size )
    > {
    > std::cout << "size" << std::endl;
    > }
    >
    > int main()
    > {
    > const unsigned sz( 5 );
    > DrawBlock( sz );
    > }
    >
    > Then see what is printed. If the value is still as mentioned,
    > I suggest you change the name of the argument "size" to something
    > else, as it may be that it is macro expanded (the only other thing
    > I can think of).


    If the value remains after you change the variable size, it may be a stack
    overflow somewhere which can cause strange values as something is writing to
    memory it shouln't. You should also put this in a debugger, look at the
    value of siz before the call and trace into the call. Are you using arrays
    or pointers in your program? If so, check where they are writing that they
    aren't writing off the end of the buffer or before the buffer.

    --
    Jim Langston
     
    Jim Langston, Dec 19, 2007
    #6
  7. Lilith

    Lilith Guest

    On Wed, 19 Dec 2007 15:16:36 -0800, "Jim Langston"
    <> wrote:

    >werasm wrote:
    >> On Dec 19, 11:42 pm, Lilith <> wrote:
    >>> On Thu, 20 Dec 2007 11:27:44 +1300, Ian Collins
    >>> <> wrote:
    >>>
    >>>
    >>>
    >>>> Lilith wrote:
    >>>>> I have a simple class method that receives as its second parameter
    >>>>> an unsigned int. But, regardless of what I do it never receives
    >>>>> the value that I pass to it. Instead it always comes up 4198769.
    >>>>> I've passed the number as a literal, as a variable and as a
    >>>>> constant and the method still recives the larger, unviable value.
    >>>
    >>>>> I've tried changing the value I pass to it and I've tried casting a
    >>>>> constant and it still comes out wrong. I can't imagine a compler
    >>>>> (VS 2005) being broken enough to do this.
    >>>
    >>>>> Any thoughts on this? 4198769 doesn't seem to be relevant to
    >>>>> anything.
    >>>
    >>>> Post a minimal example that demonstrates the problem. You will
    >>>> probably fix the problem in the process of constructing the example
    >>>> :)
    >>>
    >>> Prototype within the class:
    >>> void DrawBlock (point p, unsigned int size, sf::Color c);
    >>>
    >>> Method:
    >>> void Roads::DrawBlock (point p, unsigned int size, sf::Color c)
    >>> // size always shows up as 4198769
    >>> {
    >>> int left = p.x - size/2;
    >>> int, top = p.y - size/2;
    >>>
    >>> for (int i = top; i < top + size; i++) {
    >>> for (int j = left; j < left + size; j++) {
    >>> canvas->SetPixel(j, i, c);
    >>> }
    >>> }
    >>>
    >>> }
    >>>
    >>> Call to the method:
    >>> .
    >>> .
    >>> const unsigned int siz = 5;
    >>> .
    >>> .
    >>> fin.x = 80;
    >>> fin.y = 20;
    >>>
    >>> Roadway.DrawBlock (fin, siz, Red);

    >>
    >> I don't see an obvious problem. You are passing by value.
    >> There are no implicit conversions (not that it would
    >> matter). Perhaps try this minimal example:
    >>
    >> #include <iostream>
    >>
    >> void DrawBlock( unsigned size )
    >> {
    >> std::cout << "size" << std::endl;
    >> }
    >>
    >> int main()
    >> {
    >> const unsigned sz( 5 );
    >> DrawBlock( sz );
    >> }
    >>
    >> Then see what is printed. If the value is still as mentioned,
    >> I suggest you change the name of the argument "size" to something
    >> else, as it may be that it is macro expanded (the only other thing
    >> I can think of).

    >
    >If the value remains after you change the variable size, it may be a stack
    >overflow somewhere which can cause strange values as something is writing to
    >memory it shouln't. You should also put this in a debugger, look at the
    >value of siz before the call and trace into the call. Are you using arrays
    >or pointers in your program? If so, check where they are writing that they
    >aren't writing off the end of the buffer or before the buffer.


    The Roads class has a pointer to the image that will later be
    displayed.

    I have traced this through a debugger and I'm passing the number five
    to the method but when the method starts up, it's got 4198769 in the
    second parameter.

    I'm going to try this on another installation of the same compiler and
    see if it comes up differently.

    --
    Lilith
     
    Lilith, Dec 20, 2007
    #7
  8. Lilith

    Ian Collins Guest

    Lilith wrote:
    > The Roads class has a pointer to the image that will later be
    > displayed.
    >
    > I have traced this through a debugger and I'm passing the number five
    > to the method but when the method starts up, it's got 4198769 in the
    > second parameter.
    >
    > I'm going to try this on another installation of the same compiler and
    > see if it comes up differently.
    >

    Strip your code down to the smallest compilable sample that shows the
    problem.

    --
    Ian Collins.
     
    Ian Collins, Dec 20, 2007
    #8
  9. Lilith

    Lilith Guest

    On Thu, 20 Dec 2007 15:34:23 +1300, Ian Collins <>
    wrote:

    >Lilith wrote:
    >> The Roads class has a pointer to the image that will later be
    >> displayed.
    >>
    >> I have traced this through a debugger and I'm passing the number five
    >> to the method but when the method starts up, it's got 4198769 in the
    >> second parameter.
    >>
    >> I'm going to try this on another installation of the same compiler and
    >> see if it comes up differently.
    >>

    >Strip your code down to the smallest compilable sample that shows the
    >problem.


    I just ran this on my compiler at home and the problem doesn't exist.
    I'm going to try it one more time back at the office and perhaps try a
    re-install of the service pack to see if something just didn't go awry
    with the last update.

    --
    Lilith
     
    Lilith, Dec 20, 2007
    #9
  10. Lilith

    Bo Persson Guest

    Lilith wrote:
    :: On Wed, 19 Dec 2007 15:16:36 -0800, "Jim Langston"
    :: <> wrote:
    ::
    ::: werasm wrote:
    :::: On Dec 19, 11:42 pm, Lilith <> wrote:
    ::::: On Thu, 20 Dec 2007 11:27:44 +1300, Ian Collins
    ::::: <> wrote:
    :::::
    :::::
    :::::
    :::::: Lilith wrote:
    ::::::: I have a simple class method that receives as its second
    ::::::: parameter an unsigned int. But, regardless of what I do it
    ::::::: never receives the value that I pass to it. Instead it
    ::::::: always comes up 4198769. I've passed the number as a literal,
    ::::::: as a variable and as a constant and the method still recives
    ::::::: the larger, unviable value.
    :::::
    ::::::: I've tried changing the value I pass to it and I've tried
    ::::::: casting a constant and it still comes out wrong. I can't
    ::::::: imagine a compler (VS 2005) being broken enough to do this.
    :::::
    ::::::: Any thoughts on this? 4198769 doesn't seem to be relevant to
    ::::::: anything.
    :::::
    :::::: Post a minimal example that demonstrates the problem. You will
    :::::: probably fix the problem in the process of constructing the
    :::::: example :)
    :::::
    ::::: Prototype within the class:
    ::::: void DrawBlock (point p, unsigned int size, sf::Color
    ::::: c);
    :::::
    ::::: Method:
    ::::: void Roads::DrawBlock (point p, unsigned int size, sf::Color c)
    ::::: // size always shows up as 4198769
    ::::: {
    ::::: int left = p.x - size/2;
    ::::: int, top = p.y - size/2;
    :::::
    ::::: for (int i = top; i < top + size; i++) {
    ::::: for (int j = left; j < left + size; j++) {
    ::::: canvas->SetPixel(j, i, c);
    ::::: }
    ::::: }
    :::::
    ::::: }
    :::::
    ::::: Call to the method:
    ::::: .
    ::::: .
    ::::: const unsigned int siz = 5;
    ::::: .
    ::::: .
    ::::: fin.x = 80;
    ::::: fin.y = 20;
    :::::
    ::::: Roadway.DrawBlock (fin, siz, Red);
    ::::
    :::: I don't see an obvious problem. You are passing by value.
    :::: There are no implicit conversions (not that it would
    :::: matter). Perhaps try this minimal example:
    ::::
    :::: #include <iostream>
    ::::
    :::: void DrawBlock( unsigned size )
    :::: {
    :::: std::cout << "size" << std::endl;
    :::: }
    ::::
    :::: int main()
    :::: {
    :::: const unsigned sz( 5 );
    :::: DrawBlock( sz );
    :::: }
    ::::
    :::: Then see what is printed. If the value is still as mentioned,
    :::: I suggest you change the name of the argument "size" to something
    :::: else, as it may be that it is macro expanded (the only other
    :::: thing I can think of).
    :::
    ::: If the value remains after you change the variable size, it may
    ::: be a stack overflow somewhere which can cause strange values as
    ::: something is writing to memory it shouln't. You should also put
    ::: this in a debugger, look at the value of siz before the call and
    ::: trace into the call. Are you using arrays or pointers in your
    ::: program? If so, check where they are writing that they aren't
    ::: writing off the end of the buffer or before the buffer.
    ::
    :: The Roads class has a pointer to the image that will later be
    :: displayed.
    ::
    :: I have traced this through a debugger and I'm passing the number
    :: five to the method but when the method starts up, it's got 4198769
    :: in the second parameter.
    ::

    One possible reason could be that you debug optimized code (release
    mode?) where the parameter is passed in a register, and the debugger
    doesn't realize that.


    Bo Persson
     
    Bo Persson, Dec 20, 2007
    #10
  11. Lilith

    peter koch Guest

    On 19 Dec., 23:23, Lilith <> wrote:
    > I have a simple class method that receives as its second parameter an
    > unsigned int. But, regardless of what I do it never receives the
    > value that I pass to it. Instead it always comes up 4198769. I've
    > passed the number as a literal, as a variable and as a constant and
    > the method still recives the larger, unviable value.
    >
    > I've tried changing the value I pass to it and I've tried casting a
    > constant and it still comes out wrong. I can't imagine a compler (VS
    > 2005) being broken enough to do this.
    >
    > Any thoughts on this? 4198769 doesn't seem to be relevant to
    > anything.
    >
    > --
    > Thanks,
    > Lilith


    Bo Perssons suggestion is quite good: check to see if you run in
    release mode. If that's not the problem, maybe it is because the
    "project" is out of sync. Try to build all your sources again and see
    if this solves the problem. Another solution could be that you violate
    the ODR, having two different definitions of point.

    /Peter
     
    peter koch, Dec 20, 2007
    #11
    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.

Share This Page