Visual Studio 2010 runtime check failure # 3
It looks like that Microsoft is backfiring again in VS2010. Here's my code
#include "string.h"
typedef struct s_test
{
unsigned char a[20];
} t_test, *p_test;
void initialize(t_test t)
{
memset(t.a, 0, 20);
}
void main()
{
t_test t;
initialize(t);
}
and it throws
Run-Time Check Failure #3 - The variable 't' is being used without being initialized.
Well...since in other cases the runtime checker does help so I'm less likely to turn it off in the solution. But how the hell should I do with this? You might suggest to change the way the argumen开发者_如何学JAVAt is passed to pointers. However it would be an unpleasant experience regarding a consistent coding style. Now I feel like rawring to MS for this BRILLIANT stuff ;p
Well, the error report you are getting is a perfectly valid and appropriate error report. This is exactly what you are doing: you are using an uninitialized value. You are attempting to copy an uninitialized object - in which case the report from tun-time checker is, of course, expected.
Basically, what you are doing in your code is equivalent to this
int a, b = a;
This code will trigger the same error for the very same reason. So, why are you complaining about it?
That's because you're passing t by value - an unitialized value. Define your initialize function as follows, so that it can actually modify the passed in object and all is well:
void initialize(t_test *pt)
{
memset(pt->x, 0, 20); // x not a
}
Also, for the sake of all that's holy, change:
void main()
to
int main()
精彩评论