Thread: Allocating array concept

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    50

    Allocating array concept

    Hi ;
    Regarding to initializing array with size variable isn't allowed in c, what confusing me is that lets assume I have like this-
    int n=add(5,6); //add is a function type int that return 6+5 which it's11
    int array[n];
    so this isn't allowed for allocating array, anyone could help me why? thanks alot.

    I known is variable, but once the compiler read int array[n] , the variable n is already having value which is 11 ..so it's like int array[11] and it should be legal ..

    thanks alot

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,612
    Regarding to initializing array with size variable isn't allowed in c,
    What makes you think that using a variable sized array is not allowed in c? If you use the C99 version of the C standard VLA are legal, I don't recommend their use but they are legal.

  3. #3
    Registered User
    Join Date
    Apr 2020
    Location
    Greater Philadelphia
    Posts
    26
    >If you use the C99 version of the C standard VLA are legal, I don't recommend their use but they are legal.

    My education has now been updated. I too had assumed that they were not legal. Thank you!

    Now I wonder how this feature is implemented, especially after noticing a program that actually uses it: Print the longest common substring - GeeksforGeeks

    Perhaps the caller determines the lengths of two strings and passes them to the function because the variable-sized array must be allocated before the function does anything else. Does this two-dimensional array reside on the stack, or is it done with an implicit use of malloc() and free()?

    What would happen if more memory were requested for this matrix than is available?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,612
    Now I wonder how this feature is implemented, especially after noticing a program that actually uses it:
    Since the code presented in that link is C++, VLA are not allowed since C++ doesn't allow VLA so those snippets are badly formed and should fail to compile when using a strictly conforming compiler.

    VLA are only truly available in C that is compiling to the C99 language standard, prior to C99 they were not allowed, and after the C99 standard they are optionally (implementations choice) available.

    When using the C99 standard a Variable Length Array is allowed, the arrays are "automatic" variables probably on the stack, they act like "static" arrays as far as the use is concerned.

    If you try to create arrays too large the program will crash.

    Again I do not recommend the use of VLA, if you need runtime sized arrays I recommend you use malloc()/free() in C and std::vector or new/delete in C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 11-14-2011, 08:30 AM
  2. confusion in array concept...
    By xterminator in forum C Programming
    Replies: 5
    Last Post: 04-03-2011, 11:51 AM
  3. Displaying an array (concept not a how to)
    By mrcheesypants in forum C++ Programming
    Replies: 5
    Last Post: 11-29-2005, 06:50 PM
  4. help allocating 2d array w/ new
    By Mr_Jack in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2004, 08:10 AM
  5. allocating mem for an array
    By Strut in forum Linux Programming
    Replies: 2
    Last Post: 10-19-2002, 05:03 AM

Tags for this Thread