Thread: Size of array concept in C/C++

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    34

    Size of array concept in C/C++

    Hi guys,
    As I understand that the if the length of the array isn't constant (variable) then in c/c++ I can't declare it and I must create it dynamically.
    This point I understand, but what about if I create an array in one function and I pass the length of the array as argument .. will it be allowed in c/c++?
    I'll explain by an example:
    Code:
    void doSomething(int length)
    {
      int array[length]={0}; // here length isn't run time cosntant, so creation of the array must be dynamically no? 
    }
    int main()
    {
    int length=9;
    doSomething(length);
    return 0;
    }
    I passed the length as argument to function doSomething , and there I create an array, but the variable length is considered variable because it's not a run time constant variable .. so it's not allowed to create the array in this way and I need to create it dynamically . Am I right?
    Another question, what about if the length is of type const int length. still I can't create the array and need to dynamically create it?

    I need to understand the concept when I need to create dynamically the array if the length is variable - i.e isn't a run time /compile time constant.


    THANKS!
    Last edited by JohnnyOmari; 12-23-2020 at 01:54 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Variable-length array - Wikipedia
    Variable length arrays are a strictly C99 feature; they're an optional feature in C11. They've never been valid in C++ (and nor likely to ever be).

    > will it be allowed in c/c++?
    There is no such thing as c/c++.
    Modern C and modern C++ share a common ancestor, and that's about it.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    34
    Quote Originally Posted by Salem View Post
    Variable-length array - Wikipedia
    Variable length arrays are a strictly C99 feature; they're an optional feature in C11. They've never been valid in C++ (and nor likely to ever be).

    > will it be allowed in c/c++?
    There is no such thing as c/c++.
    Modern C and modern C++ share a common ancestor, and that's about it.
    What do you mean?
    so creating array with variable length(not dynamically) depends on which compiler I use? I want to understand if it's allowed in c or not .
    @lets forget from c++.

    thanks

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    It's allowed if you use C99 mode in your compiler.

    gcc -std=c99 prog.c

    Otherwise, you're relying on the grace of your compiler as to whether it works or not.

    > int array[length]
    The other problem is that if length is very large, you may end up blowing the stack and crashing.

    int *array = malloc( length * sizeof(*array) );
    If the malloc fails, you then have the opportunity to check whether array is NULL or not, and take action accordingly.

    With a VLA, you may not have that luxury; the program will simply be terminated by the OS.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocating array concept
    By Brian_Teir in forum C Programming
    Replies: 3
    Last Post: 04-27-2020, 09:26 AM
  2. Replies: 19
    Last Post: 11-14-2011, 08:30 AM
  3. confusion in array concept...
    By xterminator in forum C Programming
    Replies: 5
    Last Post: 04-03-2011, 11:51 AM
  4. Replies: 12
    Last Post: 03-11-2010, 02:28 PM
  5. Displaying an array (concept not a how to)
    By mrcheesypants in forum C++ Programming
    Replies: 5
    Last Post: 11-29-2005, 06:50 PM

Tags for this Thread