And in C, you can declare an array such that the size is determined during execution?

void allocate(int size) { int sample[size]; } 

    3 answers 3

    Yes, such an array can be defined in C99. In particular, GCC allows it.

       void allocate(int size) { int* sample = malloc(sizeof(int) * size); .. freemem(sample); } 

      Malloc

      • mmm thank you, but I meant exactly the array on the stack - Nicolas Chabanovsky
      • alloca (sizeof (int) * size); - avp

      I can offer you to create an array using the OOP approach. Namely, implement methods for allocating memory, adding an element, obtaining an element by index and freeing memory. Here is an example:

      Header file: List.h

       #include <stdio.h> #define DEFAULT_CAPACITY 100 typedef int ListItem; extern ListItem *list_not_found; typedef struct { int length; int capacity; ListItem *items; } List; List* list_init(); void list_add(List *list, ListItem item); ListItem* list_get(List *list, int index); void list_clear(List *list); 

      Implementing dynamic sheet in file: List.m

       #include "List_s.h" #include <stdlib.h> ListItem *list_not_found; List* list_init() { static int static_list_nil; if (!list_not_found) list_not_found = &static_list_nil; List *newList = malloc(sizeof(List)); *newList = (List){.capacity = DEFAULT_CAPACITY, .length = 0, .items = malloc(DEFAULT_CAPACITY*sizeof(ListItem))}; return newList; } void list_add(List *list, ListItem item) { if (list->length >= list->capacity) { list->capacity *= 2; list->items = realloc(list->items, sizeof(ListItem)*list->capacity); } list->items[list->length++] = item; } ListItem* list_get(List *list, int index) { if (index >= 0 && index < list->length) return &list->items[index]; return list_not_found; } void list_clear(List *list) { free(list->items); } 

      Example of use:

       #include "List_s.h" int main(int argc, const char * argv[]) { List *newList = list_init(); for (int i=0; i<5; i++) list_add(newList, i); for (int i=0; i<10; i++) { ListItem *item = list_get(newList, i); if (item != list_not_found) printf("List[%d]=%d\n", i, *item); else printf("List[%d] - not found\n", i); } list_clear(newList); return 0; } 

      Total:

      • In this implementation, the initialization list (the list_init() method) allocates default memory for the DEFAULT_CAPACITY elements.
      • As an element, in this example the int type is used. The type of stored items can also be a structure; for this, it is necessary to specify the type of structure in the typedef int ListItem; string typedef int ListItem;
      • To add and get list items use the methods: void list_add(List *list, ListItem item) и ListItem* list_get(List *list, int index) .
      • After completing the list, you must free the memory void list_clear(List *list)
      • Don't you find it a little strange to call the data structure, which is essentially a vector List? And the name of the file is somehow "close" to the fairly well-known /linux/include/linux/list.h . / Finally, the question was definitely about allocating memory on the stack, not on the heap. - avp
      • I used the name of the List structure as an example only to show an analogy with OOP ( List class in java). In this example, I wanted to demonstrate how it is convenient to create dynamic lists of data, without thinking about allocating, freeing memory, but using methods for simple work with the list. In this implementation, memory is allocated on the heap, because, unfortunately, randomly chosen restrictions are imposed on the stack size, so the stack is much smaller than general-purpose memory. And data sets are sometimes very large, which leads to the use of malloc . @avp - AleX
      • one
        The above code still implements the classic vector, not the list. A list in Java is simply an interface that does not indicate exactly how to store elements, but only indicates what the class that implements it should be able to do. There is also a class ArrayList, which additionally confuses. (As for me - is it a very bad name - an array of lists or an array list?) - KoVadim