And in C, you can declare an array such that the size is determined during execution?
void allocate(int size) { int sample[size]; }
And in C, you can declare an array such that the size is determined during execution?
void allocate(int size) { int sample[size]; }
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); }
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:
list_init()
method) allocates default memory for the DEFAULT_CAPACITY elements.typedef int ListItem;
string typedef int ListItem;
void list_add(List *list, ListItem item) и ListItem* list_get(List *list, int index)
.void list_clear(List *list)
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. - avpList
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 - AleXSource: https://ru.stackoverflow.com/questions/1027/
All Articles