if i want an array of 10000 ints and i just nned to use it like array only (not the functions which vector have seprately from array), then i make an array int a[10000] and vector<int>a(10000); then memory allocation time will be different for them ? if yes, then please tell why ? thanks in advance.

Edited 3 Years Ago by nitin1

The vector will be slightly larger for the class's other data members. But the array of integers will be the same size. If you don't need the methods of a vector them use a normal array.

write this program to find out how large the vector class is

#include <vector>
#include <iostream>

int main()
{
   std::cout << "sizeof(vector) = " << sizeof(std::vectgor)) << '\n'
}

The difference in total memory used between a vector or an array is going to be negligible (probably only one pointer and two integers).

The use of a vector is going to take a bit more time to allocate the memory because it must ask the heap to allocate a chunk of memory. But that's about it.

If you use a static array, it will be a faster because it is allocated on the stack. However, the stack has only a limited amount of memory available (usually 2 or 8 Mb), so, it is not really a good idea to take too much of the stack memory for large arrays. Every time you call a function, the local variables are allocated on the stack, taking up more and more of the stack memory, and so, that is why a recursive algorithm can end up running out of stack memory (called a "stack overflow").

In summary, the overhead of allocating the memory dynamically (using std::vector) is slightly more than using a static array, but it is usually negligible compared to whatever else you actually do with the elements in the array. And because you should avoid using the stack too much, it is usually better to use a std::vector for an array of any significant size.

very nicely explained! like if i want to allocate an int array of 10^7, then difference will be shown ? I have seen that sometimes when i change my vector to int, they run "sufficiently" fast as compared to when i was using vectors. have i observed the right thing ?
1. total memory difference used between both is negligible.
2. time difference between both is negligible.

are they both right ? thanks.

If the time taken to allocate a your vector/array is the main time cost in your program, then you're doing well!

In reality, I would almost always use std::vector You could end up saving space. For example, if you make an array of 10000 ints then as soon as the program starts, you're going to be using 40000 bytes on the stack, even if you only have a few items in the array. However, if you use a std::vector< int >, then you'll only have the small number of variables in the vector on the stack (usually a pointer and a couple of ints, or a trio of pointers) and the amount of memory that is actually needed on the heap. If you only have 10 things in the array, then you'll only be using 40 bytes on the heap (plus whatever capacity padding your std::vector implementation has).

If as you say that you have observed that:

  1. total memory difference used between both is negligible.
  2. time difference between both is negligible.

Then I'd just use std::vector because of the added flexibility. In C++11, there's also std::array (see here), just to throw that into the mix too :o)

that means having memory from heap is better than having memory from stack ?
but i have felt that malloc function usually takes longer time so as to alocate memory. (malloc also do dynamic allocation). then ?

malloc/calloc etc. indeed allocates dynamically items to the heap, such as the new operator in C++. As it was pointed out before, if you're using a large static array, at runtime, from the start, a big part of your stack will be filled, but if you use memory in a dynamical way, perhaps it would work better in general. Yes, memory allocation does take a little bit of time to perform, since it has to allocate that memory contrary to the static one, where all of the memory is allocated at the program start-up, but if you really need to use large amounts of information, and you want them to be store in a container, i. e. an array, than you should use the dynamical memory allocation. Since the "heap" is learger than the usual stack, it's better when dealing with large amounts of information to save them on the heap.
Another thing you should consider is the fact that if you do allocate statically the array, on the stack, you'll have less space (if having large arrays) for the other members of the program, since elements of the functions and the good run of the program relies on that stack to perform their operatios.

that means having memory from heap is better than having memory from stack ?

No, they're different but niether is "better". Each is the right thing to do in different situations.

but i have felt that malloc function usually takes longer time so as to alocate memory.

It does take slightly longer, but is that time significant in the whole run-time of your program? I would predict not.

This article has been dead for over six months. Start a new discussion instead.