Array question

This is a discussion on Array question within the C Programming forums, part of the General Programming Boards category; The following program compiles (using MS Visual Studio 2010) with no errors or warnings and runs without any error, why? ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    13

    Array question

    The following program compiles (using MS Visual Studio 2010) with no errors or warnings and runs without any error, why? Thanks in advance for explanation.


    #include <stdio.h>

    int f[3] = { 1,2,3 };
    int g[4] = { 12,45,55,65 };

    int main(int argc, char *argv[])
    {
    printf("%d\n", f[5]);
    return 0;
    }


    The output is: 55

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    f and g are next to each other in memory and when you output f[5] it simply looks for an address that is 5*sizeof(int) bytes from the start of f and prints out the value there (which happens to be the 55 from array g). You should avoid doing this as it is dangerous.

    And use code tags [code][/code].
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    The output is actually garbage from memory, in the general case. C has no array-index out of bounds checking built in, so you can use an index off the end of an array without the compiler complaining, but the memory you are accessing when you do it could hold anything.

    In your case, the program managed to put the two arrays next to each other in memory, so that f[5]==g[2]. This is more likely to happen for small arrays, but it is not guaranteed.

    In the future, please use code tags around your code

    EDIT: beaten ^_^
    C is fun

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    22
    Quote Originally Posted by rassul View Post
    The following program compiles (using MS Visual Studio 2010) with no errors or warnings and runs without any error, why? Thanks in advance for explanation.


    Code:
    #include <stdio.h>
    
    int   f[3] = { 1,2,3 };
    int   g[4] = { 12,45,55,65 };
    
    int main(int argc, char *argv[])
    {	
    	printf("%d\n", f[5]);
    	return 0; 
    }
    The output is: 55
    C has no bound checking... So this is valid as f and g are allocated memory continuously with f below g...

    So f[5] is valid...

    Hope this helps...

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    Quote Originally Posted by C_Enthusiast
    So this is valid as f and g are allocated memory continuously with f below g...

    So f[5] is valid...
    I would caution against this statement as it is not clear what is meant by "valid" in this context. f[5] is syntactically correct and should compile, but it is logically incorrect and results in undefined behaviour. How f and g are positioned in memory is implementation defined, so you should not rely on this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by C_Enthusiast View Post
    So f[5] is valid...
    No, it's certainly not valid, just lucky.

    It could just as easily output 12, 45, -858993460, or anything else, even just simply crash.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    22
    Quote Originally Posted by laserlight View Post
    I would caution against this statement as it is not clear what is meant by "valid" in this context. f[5] is syntactically correct and should compile, but it is logically incorrect and results in undefined behaviour. How f and g are positioned in memory is implementation defined, so you should not rely on this.
    Ok I got my mistake as f and g are global arrays...
    But I want to know that is my statement correct if they would have been defined in main function as then memory will be assigned to them in stack ???

    Please update me..

    Regards
    C_Enthusiast

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    Quote Originally Posted by C_Enthusiast
    Ok I got my mistake as f and g are global arrays...
    But I want to know that is my statement correct if they would have been defined in main function as then memory will be assigned to them in stack ???
    Which statement are you referring to?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    In this case you have a defined array of 3 elements. By definition only elements 0, 1 and 2 are valid all the rest are out of bounds. As the others have pointed out this leads to undefined behavior since you have no guarantee what that spot in memory holds...

    Also, consider the example given above... where f[5] overlaps with g[2]... what does f[5] = 0; do to the other variable...

    One of the most important lessons in life says: "Just because you _can_ do something, does not mean you _should_".

  10. #10
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    455
    Either you put it on the stack or not, you still access an out-of-range. This can be one of the hardest bug to catch.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  11. #11
    Registered User
    Join Date
    Jun 2010
    Posts
    22
    Quote Originally Posted by laserlight View Post
    Which statement are you referring to?
    I am referring to this statement
    f and g are allocated memory continuously with f below g...

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    Quote Originally Posted by C_Enthusiast
    I am referring to this statement
    rassul's observation implies that that appears to be the case. As to whether your statement will be correct in some other case: it depends on the implementation.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question : Passing Array Arguments as Pointers
    By NEMEAN in forum C Programming
    Replies: 1
    Last Post: 12-04-2010, 02:33 PM
  2. Array Question
    By Alecroy in forum C++ Programming
    Replies: 4
    Last Post: 09-26-2010, 03:22 PM
  3. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 06:07 PM
  4. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 05:27 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21