Reputation: 185
I have created a buffer class to use with networking, and I use a side-effect to get the buffer pointer, as well as the size. I have created a simple test that displays the same behaviour as the class's getptr() function.
char SomeBuffer[100];
void* getbuf(int& size) {
size = 100;
return SomeBuffer;
}
int testrecv(void* ptr, int size) {
int i = 0;//BREAKPOINT HERE
return 0;
}
int main(int argc, char**argv) {
int size;
testrecv(getbuf(size), size);
}
when I view the variables from within the testrecv() function, size is some random value that was left on the stack. Shouldn't the size in testrecv() be 100, due to the side-effect in getbuf()?
Upvotes: 1
Views: 123
Reputation: 264571
The problem is that you are assuming an oder of evaluation:
testrecv(getbuf(size), size);
// What seems to be happening is
1) size is evaluated and set up for the function call.
2) getbuf() is called. This sets the local copy of size
but the value that is being passed to the function call has already been
evaluated. So this value will not be passed (but the random value that
was in the variable at the time the `size` parameter was evaluated).
3) Function testrecv() called.
Don't rely on side affects.
int size;
void* buf = getbuf(size); // Now size will be set
testrecv(buf, size);
see: https://stackoverflow.com/a/367663/14065
Upvotes: 2
Reputation: 153955
The order of evaluation of function arguments is unspecified. It seem, the system you are using first evaluates size
followed by getbuf(size)
. As a result, the argument doesn't have the expected value. The easiest fix is probably to return both pointer and the size:
std::pair<void*, int> getbuf() { return std::make_pair(someBuffer, 100); }
int testrcv(std::pair<void*, int> buffer) { ... }
(or you could use a std::vector<T>
of a suitable type...)
Upvotes: 2
Reputation: 81379
The order of evaluation of function arguments is implementation-defined. That means that you cannot rely on getbuf
being called before the size
argument is passed to testrecv
.
What its happening here for your particular compiler, is that arguments to testrecv
are evaluated from last to first. size
is evaluated first, and at that time it has an unspecified value (random value). Only then getbuf
is evaluated, modifying your size
variable to the value you expect, but its too late for the function argument.
Upvotes: 4