Re: Passed value not being received
Lilith wrote:
:: On Wed, 19 Dec 2007 15:16:36 -0800, "Jim Langston"
:: <tazmaster@rocketmail.com> wrote:
::
::: werasm wrote:
:::: On Dec 19, 11:42 pm, Lilith <lil...@dcccd.edu> wrote:
::::: On Thu, 20 Dec 2007 11:27:44 +1300, Ian Collins
::::: <ian-n...@hotmail.com> 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