Skip to content

Instantly share code, notes, and snippets.

@Phildo
Created February 2, 2022 18:57
Show Gist options
  • Save Phildo/2e62b66e0c0412ed6513e7b85a736faf to your computer and use it in GitHub Desktop.
Save Phildo/2e62b66e0c0412ed6513e7b85a736faf to your computer and use it in GitHub Desktop.
pass by value bug in msvc
//this repro mangles the value of v.y in the pass_and_log function
//prints 0.20000 0.00000
//should print 0.20000 0.10000
//compiler: Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30139 for x64 (VS 2019 latest as of 2/1/22)
//compile command: cl /O2 /Ob1 repro.cpp
#include <cstdio>
struct vec2 { float x; float y; };
void pass_and_log(vec2 v) { printf("%f %f\n", v.x, v.y); } //prints y = 0.0f
void bar(vec2 v, int *i) { *i = 2; }
void foo(int *i)
{
vec2 v = {0.1f,0.1f};
bar(v, i);
v.x = 0.2f;
for(size_t j = 0; j < 2; j++)
pass_and_log(v);
}
int main()
{
int i;
foo(&i);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment