curiosity with vector in c -
i wrote little little program in c, , expected error vectors.
#include <stdlib.h> #include <stdio.h> main(){ int vet[0]; vet[0] = 1; vet[1] = 2; vet[2] = 2; vet[3] = 2; vet[4] = 2; vet[5] = 2; vet[6] = 2; vet[7] = 2; int x; for(x=0;x<19;x++) printf("%d\n", vet[x]); system("pause"); } before writing code thought add element vector, size, should call malloc function, discovered without malloc function code works fine. question is, why should use malloc function if in case it's useless? , why should write dimension of vector when initialize ?
because, writing memory never allocated, causing undefined behaviour. works, doesn't, makes dog explode.
Comments
Post a Comment