LB is on the money here if you're working in c++. However in C there are no vectors.
IF you are working only in C (and I stress if, because as LB states it's very easy to make a mistake) then you need to know a few things.
Memory has different aspects, but in this regard we will only talk about the stack and the heap. The stack is where the program reserves all the memory needed for it's local variables and stuff.
The size that needs to be reserved in the stack is calculated at compile time, and not executable time. This means you cannot have dynamic variables in the stack.
Don't worry the world isn't ending. If you're not capping out on memory, then you still have the heap!
The heap is basically all the unallocated data space, and there's no regulation to the allocation and deallocation of blocks from it. This means it is prime for dynamic allocation!
How do you access the heap?
1 2 3 4
|
int x* = new int//this is used to create an integer pointer to a heap memory block. Let's say
//You're running a typical system and an int is 4 bytes. You've just allocated 4 bytes of
//memory in the heap.
delete x; //this is used to delete the all the memory in a heap allocation.
|
You MUST delete any data you create in the heap, because it will not be deleted until you tell the computer to delete it. This means that even if you leave the scope of the heap variable, the data will still be there in the heap.
Now how would you allocate an array?
1 2 3 4
|
int x;
cin >> x;
int *y = new int [x];
delete y[];
|
Before testing any of this, read these :
http://www.cplusplus.com/forum/general/111693/
http://www.programmerinterview.com/index.php/data-structures/difference-between-stack-and-heap/
DO NOTE, I haven't worked with stack/heap in a while, while confident... I'm not entirely sure that the array delete in the 2nd code reference will work safely.