Thread: Array Declaration

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

    Array Declaration

    Hello. I am new to C and have a simple question.

    If I had something like this:

    Code:
    int x;
    int array[x];
    x = 5;
    Would the computer know that the array should hold 5 integers, even though I calculated x afterwards?

    Thanks in advance!

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    In a word, no.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Definitely not. Nor would this work:

    Code:
    int x;
    x = 5;
    int array[x];
    Declaring an array like that is a static array, therefore the compiler looks at the size specified at compile time and allocates storage. Since the value of x is not determined until run time the compiler does not know how many bytes of memory to allocate to the array.

    Code:
    int x;
    x = 5;
    int * array;
    array = new int[x];
    delete [] array;
    Declaring an array like this is a dynamic array, therefore at run time the size of the array is determined and since it's at run time the value of x is determined therefore allocating 'x' * 4 bytes of memory is acceptable.

    Just a side note, the memory must be released for a dynamic array using delete.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by pobri19 View Post
    Just a side note, the memory must be released for a dynamic array using delete.
    free()

  5. #5
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Quote Originally Posted by pobri19 View Post
    Nor would this work:
    In C99, it does.

    And you want malloc/free instead of new delete (this is the C section of the forum).
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  6. #6
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Haha my bad
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    So would this work? And is this the correct syntax for malloc/free?

    Code:
    int x;
    int * array;
    x = 5;
    array = malloc(int[x]);
    // ...code
    free(array);
    Thanks everyone.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Close. malloc wants the number of bytes to allocate. If you want 5 integers, then you need the size of an integer. Commonly done like so:
    Code:
    int *foo;
    int x = 5;
    foo = malloc( x * sizeof( int )  );
    Or, if you prefer:
    Code:
    foo = malloc( x * sizeof( *foo ) );

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

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    Thanks! It works, but on the following line:

    Code:
    free(array);
    Dashcode is giving me the following warning:

    "Implicit declaration of function 'free'"

    What does this mean?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include<stdlib.h>

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

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    Awesome. Thanks everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. not constant satic array declaration
    By fcommisso in forum C Programming
    Replies: 6
    Last Post: 12-14-2009, 03:01 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM