Thread: Decariing arrays and dynamic memory

  1. #1
    Registered User myradio's Avatar
    Join Date
    Jul 2015
    Posts
    2

    Question Decariing arrays and dynamic memory

    Hi There,

    There was a time in which I used C daily, but that was like 10 years ago. Now Im back, and there is something that probably didn't change in this 10 years, but I cant really remember how it works. I searched in the forum but didn't find conclusive answer.

    Question is:

    When I first learnt programming C in borland, I'd been told that arrays can only be defined with constants:

    Code:
    int a[10];
    or of course:

    Code:
    #define N 10
    int a[N];
    otherwise, you you had no other choise but using dynamic memory:

    Code:
    a = malloc(sizeof(int)*10);
    now I see that is totally possible to do:

    Code:
    int n=10;
    int a[n];
    My question is: whats the difference -if any- between these two ways? Are both allocating memory in the same place?

    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by myradio
    My question is: whats the difference -if any- between these two ways? Are both allocating memory in the same place?
    You have encountered use of the variable length array feature that was standardised in the 1999 edition of the C standard, though it was made optional in the 2011 edition of the C standard.

    The difference is that the size of the variable length array is not fixed at compile time, but unlike a dynamic array using malloc/realloc/free, you cannot resize once it has been created at runtime, but then you don't have to explicitly free the storage allocated for it either.

    As for where the storage for a variable length array will be allocated: I believe that is implementation defined.
    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

  3. #3
    Registered User myradio's Avatar
    Join Date
    Jul 2015
    Posts
    2
    Thank you so much laserlight!

    That was really clarifying


    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic memory allocation (arrays)
    By newHansen in forum C++ Programming
    Replies: 8
    Last Post: 02-25-2014, 01:28 PM
  2. Using Dynamic Memory allocation for arrays
    By mrafay in forum C Programming
    Replies: 19
    Last Post: 01-09-2011, 11:44 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Dynamic Memory and Arrays
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2006, 04:09 AM
  5. dynamic memory and arrays
    By jomns in forum C++ Programming
    Replies: 4
    Last Post: 01-04-2004, 02:18 PM

Tags for this Thread