1

Say I have a struct like this:

struct vertexNodeInfo
{
    unsigned char level;
    int node;
    double leaf;
};

If I then had this:

vector<vertexNodeInfo> node;

How big (memory-wise, not .size) would the empty vector be, before any push_back? Would it be exactly the same size (again, in terms of memory) as vector<int> node;?

Clonkex
  • 3,373
  • 7
  • 38
  • 55
  • It needs enough to store a pointer to some data, the size or end of the data, and its capacity. Try `sizeof(std::vector)`. The size is independent of the number of elements it holds at any given time. – juanchopanza Nov 02 '13 at 08:39
  • @juanchopanza Actually I was hoping someone could just tell me. I'm writing a complicated DLL and I can't compile it yet. I need to know the answer before I can finish it and compile, so I can't easily test using `sizeof`. – Clonkex Nov 02 '13 at 08:42
  • @juanchopanza Second comment in case editing doesn't alert you of comments (forgot to put your name in when posting). – Clonkex Nov 02 '13 at 08:42
  • The size is unspecified, and it depends on the platform. All is required is that it holds enough data members to do what I mentioned in the first comment. – juanchopanza Nov 02 '13 at 08:45
  • @juanchopanza All I mean is, is the memory usage of an empty `vector` affected by the data inside its `struct`? – Clonkex Nov 02 '13 at 08:46
  • 3
    @Clonkex typically, no. Pedantically, yes, it could be, because pointers to different types can vary in size. – Luchian Grigore Nov 02 '13 at 08:48
  • @Clonkex - Depends on implementation. Anyway how on Earth does it effect the DLL and you ability to compile it? – Ed Heal Nov 02 '13 at 08:50
  • @EdHeal What? It doesn't. Well, it doesn't affect my ability to compile it. I need to know the answer so I can decide how to write my code so I can finish writing my code so I can compile. That's all :) – Clonkex Nov 02 '13 at 08:51
  • @Clonkex - In what way? – Ed Heal Nov 02 '13 at 08:54
  • 1
    I think what Ed is getting at is that you should not normally write code that depends on how much memory an empty vector occupies. In fact, since there's no guarantee about it in the standard you *can't* write portable code that depends on it. But even if there were a guarantee, it would require very unusual circumstances before anyone should need to rely on it. – Steve Jessop Nov 02 '13 at 10:43
  • @SteveJessop Actually it doesn't matter any more. I realised I only need a direct `new`/`delete` method. Now I'll ask a beginner question. Any time I use `new`, I get a pointer, right? (Obviously the var has to be declared as a pointer) What I mean is, it's not special because I used `new` on it, right? So when I pass the `new`'d var into a function, it's passing a pointer and not copying all the data of the var? – Clonkex Nov 02 '13 at 12:22
  • 1
    @Clonkex: What matters is the declaration of the function parameter, not where the object came from. If the function takes a pointer then you'll pass a pointer. If it takes a vector then you'll pass a vector... – Steve Jessop Nov 02 '13 at 13:14

2 Answers2

1

There's no requirement. Even more, it's a poorly formulated question.

The .size of the vector would be 0, because it has no elements.

The sizeof isn't affected the number of elements, and is typically large enough to contain one or two of pointers to the type (which can vary in size themselves, so there's no guarantee the sizeof(vector<vertexNodeInfo>) and sizeof(vector<int>) are equal), the size and capacity.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

As Luchian says, it is likely but not required that sizeof(vector<int>) == sizeof(vector<vertexNodeInfo>).

Furthermore, I don't believe the standard makes any requirement as to the capacity of a default-initialized vector:

Initial capacity of vector in C++

So it's permitted to have a capacity of 10, meaning that it has allocated enough memory for 10 elements up front. Then the size of this allocation would of course depend on the size of the elements. You don't exactly define what you mean by "how big, memory-wise", but if you mean to include dynamic allocations then this would be one.

I don't think that would be a very good implementation of vector, but it conforms to the standard.

Community
  • 1
  • 1
Steve Jessop
  • 273,490
  • 39
  • 460
  • 699