what's wrong with this program
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: what's wrong with this program

  1. #1
    Join Date
    Jun 2014
    Posts
    6

    what's wrong with this program

    The file1.c defines an array 'myarray' as,

    int myarray[10];

    The file2.c uses 'myarray' as,

    extern int *myarray;
    void foo()
    {
    myarray[0] = 10;
    }

    What kind of problem this program may have?

    Thanks !

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,763

    Re: what's wrong with this program

    it'll work as long as you don't do something stupid. such as accessing elements out of the actual defined bounds. In that case "anything could happen".


    if you defined it extern properly, the compiler could potentially spot an out of bounds access with a warning (assuming the index is calculable at compile time)

  3. #3
    Join Date
    Jun 2014
    Posts
    6

    Re: what's wrong with this program

    "accessing elements out of the actual defined bounds" would be a problem in general everywhere.

    When you say, "if you defined it extern properly" what do you mean here ?

  4. #4
    Join Date
    Jul 2013
    Posts
    209

    Re: what's wrong with this program

    Quote Originally Posted by mithlesh View Post
    What kind of problem this program may have?
    Plenty. It's generally considered bad programming practice to have global data (state).

    http://c2.com/cgi/wiki?GlobalVariablesAreBad

  5. #5
    Join Date
    Apr 1999
    Posts
    27,423

    Re: what's wrong with this program

    Quote Originally Posted by mithlesh View Post
    The file1.c defines an array 'myarray' as,

    int myarray[10];
    What is
    Code:
    sizeof(myarray)
    in that line of code? It is sizeof(int) * 10, which is probably 40 bytes (assuming int's are 4 bytes). Then this:

    The file2.c uses 'myarray' as,

    extern int *myarray;
    What is
    Code:
    sizeof(myarray)
    in that code? it is sizeof(int*), which is probably 4 or 8 bytes. See the problem? So which one is right? You have two different values for sizeof(). If you now access myarray believing it can hold 10 ints, what if it can't hold 10 ints, but instead is only 4 (or 8) bytes in size? You now have a memory overwrite.

    If you are to use extern, the proper way to use extern is to make sure you use the same type. An array is not a pointer:

    Code:
    int myarray[10];
    is to be matched with:
    Code:
    extern int myarray[10];
    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 2nd, 2014 at 04:08 AM.

  6. #6
    Join Date
    Jun 2014
    Posts
    6

    Re: what's wrong with this program

    Thanks a lot !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center