Paul Wicks
Paul Wicks

Reputation: 65530

How to declare the size of an array at runtime in C?

I basically want to the C of equivalent of this (well, just the part with the array, I don't need the class and string parsing and all that):

public class Example
{
    static int[] foo;
    public static void main(String[] args)
    {
        int size = Integer.parseInt(args[0]);
        foo = new int[size]; // This part
    }
}

Pardon my C ignorance. I've been corrupted by java ;)

Upvotes: 13

Views: 21457

Answers (6)

Sonal Shah
Sonal Shah

Reputation: 11

You need int *a=calloc(n,sizeof(int)); when declaring the array in the function at runtime while implementing sorting algorithms. This will work in Turbo C++. Here I apply it in merge sort.

Upvotes: 1

Jan
Jan

Reputation: 293

Unfortunately, many of the answers to this question, including the accepted one, are correct but not equivalent to the OP's code snippet. Remember that operator new[] calls the default constructor for every array element. For POD types like int that do not have a constructor, they are default-initialized (read: zero-initialized, see §8.5 ¶5-7 of The C++ Standard).

I just exchanged malloc (allocate uninitialized memory) for calloc (allocate zeroed memory), so the equivalent to the given C++ snippet would be

#include <stdlib.h>  /* atoi, calloc, free */

int main(int argc, char *argv[]) {
    size_t size = atoi(argv[1]);
    int *foo;

    /* allocate zeroed(!) memory for our array */
    foo = calloc(sizeof(*foo), size);
    if (foo) {
        /* do something with foo */

        free(foo); /* release the memory */
    }

    return 0;
}

Sorry for reviving this old question but it just didn't feel right to leave without a comment (which I do not have the required rep for) ;-)

Upvotes: 4

mirz
mirz

Reputation: 21

If you need to initialize the data, you can use calloc:

int* arr = calloc (nb_elems, sizeof(int));
/* Do something with your array, then don't forget to release the memory */
free (arr);

This way, the allocated memory will be initialized with zeroes, which can be useful. Note that you can use any data type instead of int.

Upvotes: 2

Chuck
Chuck

Reputation: 237010

int count = getHowManyINeed();
int *foo = malloc(count * sizeof(int));

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 753475

In C, you can do that with this, if you ignore the error checking:

#include <stdlib.h>
static int *foo;

int main(int argc, char **argv)
{
     int size = atoi(argv[1]);
     foo = malloc(size * sizeof(*foo));
     ...
}

If you don't want a global variable and you are using C99, you could do:

int main(int argc, char **argv)
{
    int size = atoi(argv[1]);
    int foo[size];
    ...
}

This uses a VLA - variable length array.

Upvotes: 5

dirkgently
dirkgently

Reputation: 111120

/* We include the following to get the prototypes for:
 * malloc -- allocates memory on the freestore
 * free   -- releases memory allocated via above
 * atoi   -- convert a C-style string to an integer
 * strtoul -- is strongly suggested though as a replacement
*/
#include <stdlib.h>
static int *foo;
int main(int argc, char *argv[]) {
    size_t size = atoi(argv[ 1 ]); /*argv[ 0 ] is the executable's name */
    foo = malloc(size * sizeof *foo); /* create an array of size `size` */
    if (foo) {  /* allocation succeeded */
      /* do something with foo */
      free(foo); /* release the memory */
    }
    return 0;
}

Caveat:Off the cuff stuff, without any error checking.

Upvotes: 11

Related Questions