Passed value not being received

L

Lilith

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.
 
I

Ian Collins

Lilith said:
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 :)
 
L

Lilith

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.
 
W

werasm

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
 
S

Salt_Peter

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.


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
*/
 
J

Jim Langston

werasm said:
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.
 
L

Lilith

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.
 
I

Ian Collins

Lilith said:
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.
 
L

Lilith

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.
 
B

Bo Persson

Lilith wrote:
:: On Wed, 19 Dec 2007 15:16:36 -0800, "Jim Langston"
::
::: werasm wrote:
::::: On Thu, 20 Dec 2007 11:27:44 +1300, Ian Collins
:::::
:::::
:::::
:::::: 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
 
P

peter koch

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.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top