allocation and freeing of an array of ints

This is a discussion on allocation and freeing of an array of ints within the C++ Programming forums, part of the General Programming Boards category; first of all, how come you can do this in C++ and not in C int a[n]; secondly, why is ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    44

    allocation and freeing of an array of ints

    first of all, how come you can do this in C++ and not in C

    int a[n];

    secondly, why is the array "a" being printed after i freed it in the following piece of code:

    int *a = new int[10];
    for (int i = 0; i < 10; ++i)
    a[i] = i + 1;

    delete [] a;

    for (int i = 0; i < 10; ++i)
    cout << a[i] << " ";

    kind regards

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by boxden
    first of all, how come you can do this in C++ and not in C

    int a[n];
    Unless n is a constant positive integer, you cannot do that in C++, but under certain circumstances you can do that in C even if n is not a constant, with respect to the 1999 edition of the C standard (this is the variable length array feature).

    If you are able to use variable length arrays in C++, then you are relying on your compiler's language extension.

    Quote Originally Posted by boxden
    secondly, why is the array "a" being printed after i freed it in the following piece of code:
    Undefined behaviour. You should not be accessing that destroyed array.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    >first of all, how come you can do this in C++ and not in C
    >
    >int a[n];

    The feature you're using there is actually a C99 feature that's not available in C++ unless you use non-standard compiler extensions.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    44
    thank you for the reply guys, i was compiling it like this
    g++ *.cpp

    what flags do i need to set to have the same behavior as C's gcc -ansi -Wall -pedantic?

    also laserlight, could you tell me more about C's possibility of using int n[var]? I thought ansi C prohibited it?

    thanks in advance

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by boxden
    thank you for the reply guys, i was compiling it like this
    g++ *.cpp

    what flags do i need to set to have the same behavior as C's gcc -ansi -Wall -pedantic?
    Use -Wall and -pedantic too

    Quote Originally Posted by boxden
    also laserlight, could you tell me more about C's possibility of using int n[var]? I thought ansi C prohibited it?
    You might want to search the Web, e.g., The New C: Why Variable Length Arrays?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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. Back To The Basics: Freeing An Array on the Heap?
    By Deo in forum C++ Programming
    Replies: 12
    Last Post: 04-07-2007, 04:42 AM
  2. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 07:36 AM
  3. 2D arrays:dynamic allocation and freeing
    By bravetanveer in forum C Programming
    Replies: 5
    Last Post: 02-05-2005, 06:24 AM
  4. memory allocation and freeing
    By Jase in forum Linux Programming
    Replies: 1
    Last Post: 05-25-2003, 06:26 AM
  5. dynamic allocation question
    By vale in forum C++ Programming
    Replies: 1
    Last Post: 08-26-2001, 04:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21