tags:

views:

991

answers:

5

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 ;)

+9  A: 
/* 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.

dirkgently
You should mention that "if (foo) free(foo);" is the "garbage collection".
Joel Potter
Nevermind, looks like you did. +1 ;)
Joel Potter
There is no garbage collection in C. I'd be lying ;-) But I did put in some comments.
dirkgently
I don't think foo should be declared static, the meaning is different with java. Here we just want a global variable.
Ben
What's wrong with static? It's global (file scope), with internal linkage and IMHO much easier to understand for the OP than if I had written a smarter version (there'd be no globals). Also, this is an off-the-cuff version with a lot to left to be desired.
dirkgently
@dirkgently Your answer is very ok. I was just saying that declaring foo as an int * would have been enough. In java static is needed to declare a variable that will be instanciated only once, not once per new object.
Ben
I would do if(!foo) return 1; instead - that way it looks more like an error check, and you don't have to enclose your entire code block in brackets. But it's all personal taste.
Chris Lutz
As I have stated repeatedly, the code isn't my best. The atoi() to start with. And I don't much like multiple returns. YMMV.
dirkgently
+5  A: 

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.

Jonathan Leffler
A: 
int count = getHowManyINeed();
int *foo = malloc(count * sizeof(int));
Chuck
A: 

Note that the memory pointed by foo (which is what we call an array in C) will be filled with uninitialized values.

Ben
You should comment on the answer instead of posting a non-answer... the order of posts on SO is not guarenteed
singpolyma
+1  A: 

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.

mirz