We had a school project, any information system using C. To save a list of student records of dynamic size, I went to the structure of linked lists. This morning my friend let me see his system. I was surprised by his list of entries:
#include <stdio.h> /* and the rest of the includes */ /* global unsized array */ int array[]; int main() { int n; for (n=0; n < 5; n ++) { array[n] = n; } for (n=0; n < 5; n ++) { printf("array[%d] = %d\n", n, array[n]); } return 0; }
As with code, it declares an unserialized array that is global (in the bss segment) for the entire program. He was able to add new entries to the array, overwriting the following blocks of memory with a non-zero value so that he could move around the array:
for (n=0; array[n]; n++) { }
He used (I also tested it) with Turbo C v1. I tried this on linux and it also works.
As I have never seen this method before, I assume that there is a problem with it. So yes, I want to know why this is a bad idea and why they prefer it on a linked list.
source share