none
Problem with array not defined as static

    Question

  • I am using VC++ 2015 Express on a console application.  This is C code, not C++, so not classes are in use.

    I have an array or pointers to structs defined at the top of the file.

    static MYSTRUCT*       myarray[16]; 

    I now want to break the code the code into 3 files so I need to remove the static.  When I remove the static, still with the original code, the code no longer works.

    MYSTRUCT*       myarray[16]; 

    If I set a watch on myarray, I can see it corrupt when it is not being used.  I have tried a similar test with a large simple array or chars and the same thing happens.  It has to be declared static.

    Is this a bug or operator error?

    Scott


    • Edited by exoson Friday, April 21, 2017 3:48 PM additional info
    Friday, April 21, 2017 3:45 PM

All replies

  • Hard to say without knowing how you use it.

    I tested out with a fairly short application and what I put into the array is what comes out of the array when I print the values. I purposely avoid touching that memory while keeping the application working on something, and no matter how long I waste time, even constantly accessing memory from elsewhere, the array contents are fine.

    So please give more information, including a small sample if possible.


    This is a signature Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Friday, April 21, 2017 4:28 PM
  • Thanks for the push.

    While trying to get adequate information on the problem we solved it.  Turned out to be terrible coding by the original author.  The array named as static was used in another file without the static, but with the same name and for the same purpose.  Argh!

    I am quite surprised that the original coder didn't know better and equally surprised that VC++ didn't complain about the hidden name.

    Saturday, April 22, 2017 3:38 PM
  • Hi exoson,

    thanks for posting here.

    >>I am quite surprised that the original coder didn't know better and equally surprised that VC++ didn't complain about the hidden name.

    As far as I know, we could hide names with file scope by explicitly declaring the same name in block scope. However, file-scope names could be accessed using the scope-resolution operator (::).

    For more information about scope, please refer to this document below.

    https://msdn.microsoft.com/en-us/library/b7kfh662.aspx

    Hope this could be help of you.

    Best Regards,
    Sera Yu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    21 hours 45 minutes ago
    Moderator
  • The array named as static was used in another file without the static, but with the same name and for the same purpose.   

    I am quite surprised that the original coder didn't know better and equally surprised that VC++ didn't complain about the hidden name.

    VC++ will complain if a variable is defined more than one time.

    Without the source code of the variable definitions, I cannot say, why VC has not complained. But there must be an answer.

    Since you have found the bug in the code, you can close the posting now.

    Regards, Guido

    58 minutes ago