Thread: strange compiler

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    strange compiler

    Code:
    #include <stdio.h>
    
    int main()
    {
     int bil;
    
    scanf("%d", &bil);
    
    int kotak[bil];
    
    return 0;
    }
    if you run it in devc++ it will work.

    can someone explain why this can be happening?

    my lecturer also doesn't know about this

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    C99, VLA ( variable length arrays ). gcc supports ( in some way ) VLA's for quite a while already.
    Kurt

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    If you use the -pedantic option with gcc, it will issue the following warnings (see gcc's documentation):

    foo.c: In function ‘main’:
    foo.c:9: warning: ISO C90 forbids variable-size array ‘kotak’
    foo.c:9: warning: ISO C90 forbids mixed declarations and code

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    C99 also allows you to write ugly code variable declarations throughout your program, instead just at the beginning.



    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Can someone explain me in a very easy explanation? I don't get it at all..

    If I'm able to use that then I don't have to use malloc again?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Like has been said already, it's a feature new in C99.
    But most everyone uses C89 if they want to write portable code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    141
    Quote Originally Posted by LeonLanford View Post
    Can someone explain me in a very easy explanation? I don't get it at all..

    If I'm able to use that then I don't have to use malloc again?
    No I'm afraid it's not a replacement for malloc. I'm pretty sure a variable sized array inside a function will be allocated on the stack (hence of limited size). But even worse if you should return a pointer to this array, the pointer would be invalid and writing to it would likely crash your program. It's kind of equivalent to the old alloca() function I think.

    However if you only need the array for your current function then it is pretty nice. The array will get 'freed' when the function returns (by virtue of your stack pointer being restored to the callers context.). So you need never call free in that case. Now trying to find a fully compliant C99 compiler on the major platforms though might be an issue. Thus C99 code could not be considered particularly portable.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by SevenThunders View Post
    It's kind of equivalent to the old alloca() function I think.
    It's more powerful than alloca(), because it understands multi-dimensional arrays. You could declare an array int x[var1][var2] and the compiler will not only allocate the memory but compute the array indexing so you can access it via x[i][j].

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    141
    Quote Originally Posted by brewbuck View Post
    It's more powerful than alloca(), because it understands multi-dimensional arrays. You could declare an array int x[var1][var2] and the compiler will not only allocate the memory but compute the array indexing so you can access it via x[i][j].

    Nice. I wish C99 had more support, especially on Windows, where Microsoft has no intention of ever supporting it. Perhaps if C++ updates their standard to support it. Although given the fact that there are some incompatibilities I don't know.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    What's the politics behind it taking so long for compilers to support C99? Didn't C++98 have reasonably complete support years ago? And is gcc progressing towards complete support in the near future? I know it can be run in (not fully implemented) C99 mode with -std=c99.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    141
    Quote Originally Posted by robatino View Post
    What's the politics behind it taking so long for compilers to support C99? Didn't C++98 have reasonably complete support years ago? And is gcc progressing towards complete support in the near future? I know it can be run in (not fully implemented) C99 mode with -std=c99.
    For microsoft they probably feel there is not enough of a market to make it worthwhile implementing it. Their flagship IDE supports C++ and then some of their .net languages like C#. The only real hope is for C++ to decide to support C99 as a subset of the language.

    Frankly there are very few C99 compliant compilers out there and none of them are free and maybe only one of them runs on windows. gcc claims to be close, however the two most important changes for scientific programming, variable length arrays and complex numbers are either not supported or broken for gcc
    http://gcc.gnu.org/c99status.html
    .

    C99's strength is really in the scientific computing area, which I guess compiler writers see no use for.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    VLA's are essentially useless candy IMO.

    The biggest problem is what happens if your VLA no longer fits on the stack (assuming that is where VLAs get created on a given machine). Even the best scenario results in the program being killed by the OS. On an embedded system, it probably just trashes someone else's memory. Nor is there a way of finding out in advance whether it will succeed or not, just close your eyes, pray and take a jump.

    Of course, a lot of naive programmers are going to take unchecked user input, and create their VLA directly from that. Most of them won't even bother to check for negative numbers, nevermind huge positive numbers.

    At least with malloc, there is a clearly defined way of being told there is no more room, and from there you can take appropriate action.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Quote Originally Posted by SevenThunders View Post
    Frankly there are very few C99 compliant compilers out there and none of them are free and maybe only one of them runs on windows.
    uh... you mean to say *fully* compliant, right? because:
    Quincy 99
    Code::Blocks
    Dev-C++
    Pelles C
    are all free, and all run on Windows.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you're not fully compliant, then you're not compliant. Don't they teach people anything in school any more? Didn't you ever learn that "in a true or false question, if any of it is false, then the whole thing is false"? Thus, is it compliant? No.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Quote Originally Posted by quzah View Post
    If you're not fully compliant, then you're not compliant. Don't they teach people anything in school any more? Didn't you ever learn that "in a true or false question, if any of it is false, then the whole thing is false"? Thus, is it compliant? No.
    Quzah.
    Compliance in so much as it pertains to the more notorious of the C99 standards, such as those that make it possible for the OP's code to compile. In this context, the qualifier "fully" makes it clearer as to whether it complies with every C99 standard, as it is not uncommon to refer to a compiler as C99 even though it supports only the majority of these standards. It's a shame the english language isn't completely boolean quantifiable huh?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  2. Have you ever written a compiler?
    By ammar in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 12-27-2004, 07:10 AM
  3. Dev C++ Compiler, Indentation?
    By Zeusbwr in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 06:13 AM
  4. lcc win32 compiler download problems
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-01-2004, 07:39 PM
  5. simple error or strange compiler?
    By threahdead in forum C Programming
    Replies: 6
    Last Post: 01-20-2003, 03:00 PM