c - How can i save space in array which is not being used? -


if create array of size 10 , 2 elements stored in array remaining spaces gets wasted. how can solve problem? (by data structure)

you use simple linked list instead of array, or if need use array, should use realloc(), shrink array use 2 cells, instead of 10. , this:

#include <stdio.h> #include <stdlib.h>  int main(void) {     int* ptr = malloc(10 * sizeof(int));     ptr[0] = 4;     ptr[1] = 13;     ptr =  realloc(ptr, 2 * sizeof(int));     printf("%d %d\n", ptr[0], ptr[1]);     return 0; } 

output:

4 13


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -