Thread: array problem

  1. #1
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43

    array problem

    hello, i made a selection sort program using for loops not functions,the array can only store 10 elements, out of curiosity i entered more than 10 elements to be sorted an it works. can some one please explain this to me , how is that possible .

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    So you have an array and the compiler knows that this is memory that belongs to you. There is a lot more memory, which has not been allocated to you, but it's still accessible. What you are essentially doing is writing and then reading from that memory without having specifically asked for it. You *can* do that, but note that this isn't safe and should never be relied upon. The compiler can choose to re-order your variables and then your program will most likely crash if you try to enter more than 10 elements.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    C and C++ do allow this; you are not protected though. By doing what you are doing and to emphasis what QuantumPete stated you are stepping into a world of undefined behavior.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with looking for MIN,MAX in Array
    By ToNy_ in forum C Programming
    Replies: 5
    Last Post: 12-18-2011, 07:28 AM
  2. problem initializing a double array for large array
    By gkkmath in forum C Programming
    Replies: 4
    Last Post: 08-25-2010, 08:26 PM
  3. Problem converting from char array to int array.
    By TheUmer in forum C Programming
    Replies: 11
    Last Post: 03-26-2010, 11:48 AM
  4. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  5. Array problem
    By fatal_boot in forum C Programming
    Replies: 5
    Last Post: 08-28-2005, 03:29 AM