Thread: Memory allocation of 1D Arrays (C89 compiler)

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    1

    Unhappy Memory allocation of 1D Arrays (C89 compiler)

    Code:
    #include<stdio.h>
    void main()
    {
            int a[10];
            a[15]=100;
            printf("%d",a[15]);
    }
    this code snippet should not be compiled due to obvious reasons but it is getting compiled and it is also printing the value of a[15] as 100...Why so???? i searched and found about VLA (variable length array s in the C99 compiler ) but that is not the answer....would be very obliged if someone could give a proper answer.... I have tried different compilers like devC,turboC,ideone.org

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is the C++ forum. Since you wrote "C89" and your code is stylistically C, I shall move this post to the C forum.

    Quote Originally Posted by hindol.it
    this code snippet should not be compiled due to obvious reasons but it is getting compiled
    The obvious reason is that -- freestanding implementations and a particular interpretation of the C standard aside -- the return type of main should be int, but you declared it as void. Some compilers may accept it; others won't.

    Quote Originally Posted by hindol.it
    and it is also printing the value of a[15] as 100...Why so????
    Accessing an array out of bounds results in undefined behaviour. This means that if the compiler is able to determine it, the compiler can choose to refuse to compile... or it can compile the code but do something entirely different. In the most usual case for such out of bounds access though, this may lead to a crash due to accessing/overwriting memory that should not have been accessed/overwritten.

    Hence, printing a[15] as 100 is within the realm of possibilities.

    Quote Originally Posted by hindol.it
    i searched and found about VLA (variable length array s in the C99 compiler ) but that is not the answer....
    Indeed, VLAs have nothing to do with this since the array a is not a VLA.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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. memory allocation for string arrays
    By atlantis43 in forum C Programming
    Replies: 6
    Last Post: 05-28-2015, 08:54 AM
  2. dynamic memory allocation (arrays)
    By newHansen in forum C++ Programming
    Replies: 8
    Last Post: 02-25-2014, 01:28 PM
  3. Using Dynamic Memory allocation for arrays
    By mrafay in forum C Programming
    Replies: 19
    Last Post: 01-09-2011, 11:44 PM
  4. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  5. memory allocation
    By pari in forum C Programming
    Replies: 5
    Last Post: 12-10-2002, 10:51 AM

Tags for this Thread