How to declare success


  • I am using Visual Studio 11 beta. I have C code with the following structure:

    __success(*rcode == 0)
    void xyz(__out int *rcode, 
             __in int func,
             __out int *something)
        if (func == 1) {
            *something = 10;
            *rcode = 0;
        } else {
            *rcode = -1;  /* Error */

    I set a return code (*rcode) that indicates the call did not succeed. In this case the *something parameter will not be set.

    The problem I have is that I get error C6001 Using uninitialized memory '*rcode'. The error points to the declaration of xyz. I cannot change the code to use a function code instead (so I could use __success(return == 0)).

    Can anyone suggest how this should be done?



    • Moved by Jack-ZhaiMicrosoft contingent staff Thursday, March 29, 2012 2:29 AM Move to the correct forum about the VC++ issue. (From:Visual Studio Code Analysis and Code Metrics)
    Tuesday, March 27, 2012 9:24 AM

All replies

  • Hi Bengt,

    Thank you for posting in the MSDN forum.

    Since it is hard for us to repro this whole project, if possible, could you share us the project? Please attach your Visual Studio project, you can upload it to the sky driver, and then share the download link in your post.  We will try to check it with our computer, so we can resolve this issue together as soon as possible.

    Best Regards,

    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, March 28, 2012 9:21 AM
  • Hi Jack,

    I have uploaded to the sky driver: 

    It is a really simple Visual Studio 11 project to reproduce the issue!

    Best Regards



    Wednesday, March 28, 2012 2:17 PM
  • This problem is most likely caused by the way your function calls xyz.   We need to see that function, or at least the calling statment, the declaration for all objects used in argument expressions, and the statments that assign values to those objects.
    Thursday, March 29, 2012 4:03 AM
  • Barry,

    I have a complete sample uploaded in the previous post! The error does not seem to have anything to do with the calls to the routine. I have many variations of the routine provided and I consistently get the same error. Anyway here are a couple of calls to xyz:

    int rcode; int whatever; xyz(&rcode, 1, &whatever);
    if (rcode == 0) {
    whatever += 10;

    I get the same error regardless if these calls are present or not. Please note that I originally posted the problem on Code Analysis forum since it the (Visual Studio 11) code analysis that gives an error.


    • Edited by Bengt G Thursday, March 29, 2012 6:41 AM
    Thursday, March 29, 2012 6:17 AM
  • I downloaded your code and was able to build and execute it on Visual Studio Ultimate 11 Beta without a hitch.

    I'm using Windows 7.

    Microsoft MVP (Visual C++)

    Polymorphism in C

    Thursday, March 29, 2012 8:46 AM
  • Superman,

    Did you perform Build/Run Code Analysis on Solution?



    Thursday, March 29, 2012 9:22 AM
  • Code Analysis gave me the error that you mentioned.

    I got rid of that by making the following change - __success(rcode == 0)

    Microsoft MVP (Visual C++)

    Polymorphism in C

    • Proposed as answer by Bengt G Thursday, March 29, 2012 6:46 PM
    • Unproposed as answer by Bengt G Friday, March 30, 2012 7:11 AM
    Thursday, March 29, 2012 9:35 AM
  • That is interesting! The warning goes away, but I am not getting any feedback on routines that use the called function improperly:

    xyz(&rcode, &whatever);

    In this case I should get a warning that I am using the "whatever" variable incorrectly. Maybe that is not implemented in the analysis yet...

    Thanks for your help!


    Thursday, March 29, 2012 6:46 PM
  • The error is on the call to xyz, which requires three arguments (only two provided) the seond of which must be an int (not a pointer).  Is there a prototype or definition in scope at the point where you call the function?  What is your warning level set to?
    Thursday, March 29, 2012 10:40 PM
  • I am sorry, I wrote the code directly in the forum and forgot a parameter - it should, of course be: xyz(&rcode, 3, &whatever). What I am after is for the code analyser to find potential places in the code where the output parameter may be used without first checking the return code so these can be corrected.

    BTW, I use warning level 4!


    Friday, March 30, 2012 6:49 AM
  • You cannot dereference rcode for the comparison until you know it's a valid pointer.  Try the following, which depends on the short-cut behaviour of the && operator:

    __success(rcode != 0 && *rcode == 0)

    Answering policy: see profile.

    Friday, March 30, 2012 11:06 AM
  • David,

    I get error C6001 Using uninitialized memory with your suggestion. In any case the parameter is __out (rather than __out_opt) which says it cannot be NULL.

    I agree with you that *rcode should be used since just rcode would be the address. In a _When_ clause *rcode is used (and works as intended). I am beginning to think we are dealing with a bug in the analyser.


    Friday, March 30, 2012 11:18 AM
  •  There's some documentation about annotations downloadable from PREƒast Annotations, but interestingly __success() is not one of them.


    void xyz(__out int *rcode, 
    __deref_out_range(!=, 0) int *rcode,
    // ..etc

    Answering policy: see profile.

    Friday, March 30, 2012 1:56 PM
  • Yes, I see you can declare what values are valid after the call. But as you say, this does not affect what is considered success or not...


    Saturday, April 07, 2012 4:31 PM
  • This will be corrected in the next release!


    • Proposed as answer by Bengt G Friday, April 27, 2012 6:13 AM
    Friday, April 27, 2012 6:13 AM