c++ understanding size_t behaviour for vector creation
Asked Answered
P

1

1

this is a folow up to this https://softwareengineering.stackexchange.com/questions/256241/c-coding-practice-class-vs-free-functions question I posted a few day ago. In short, the idea is to create a custom vector class for statistical data analysis.

I got a great response, that made me realise that I need to understand: why use size_t in a constructor of a container class and why use it anyway?

Here is a part of the proposed solution:

template<class T>
class vect
{
    std::vector<T> m;

public:
    vect(size_t n) :m(n) {}
    void addTo(T a){ m.push_back(a); }
    std::vector<T> get() const { return m;}
    ... more functions and overloaded operators
};

I understand that size_t is an (unsigned int) data type and should be used to indicate that it's value should represent the size of n object.

In order to understand the behaviour of size_t I did the following:

int main() {
    vect<int> m(0);
    vect<int> n(100);
    std::cout << sizeof(n) << std::endl;
    std::cout << sizeof(m) << std::endl;
    std::cout << sizeof(m.get()) << std::endl;
    for (int i = 0 ; i < 100; i++) {
        m.addTo(i);
    }

    std::cout << sizeof(m) << std::endl;
    std::cout << sizeof(m.get()) << std::endl;

    }

all of which return "24". (I expected a change in the size of the object after adding parameters to it.) However:

    for(int i = 0; i<100;i++)
        std::cout << m[i] << std::endl;

nicely prints out all values from 0 to 100. Thus I know that there are 100 integers stared in the vector, but then why is its size 24 and not 100?

Obviously I am new to c++ programming and to make things worse this is my first template class.

Thank you for your time and patience, I really appreciate it.

Physoclistous answered 24/9, 2014 at 7:14 Comment(0)
T
3

sizeof is concerned with the size of a type in memory. Your vect class is a type that takes up 24 bytes. The size of a type never changes once it's been compiled.

But how can your vector store so much information without changing it size? Because it contains pointers to other things that can take up much more space (indeed, they can take up as much space as you need - that's why they're called dynamic data structures). Presumably your vect instance contains a pointer to the standard vector class, which contains other pointers, maybe to an array or a dynamically allocated section of memory which holds the actual data.

You cannot query the size of those private, indirectly referenced bits of memory because you don't know their name, or their type. In fact, a major reason for creating such container classes is so that you don't have to know how much memory to allocate - you just stuff things into them, and they silently allocate as much memory as necessary.

Tetrahedral answered 24/9, 2014 at 7:33 Comment(3)
Thank you Mr. Foth. If I may follow up: so what is the benefit of including a size_t into to constructor when the size of the container that inherits its value is predefined? (vect(size_t n): m(n){})Physoclistous
"size_t is the type returned by the sizeof operator and is widely used in the standard library to represent sizes and counts." In the constructor, the size_t argument is used as a number of elements, not as a size of something in bytes.Tetrahedral
Thank you very much. I know my questions seem embarrassingly simple but I really tried to understand what's going on there. So again I really appreciate your answers.Physoclistous

© 2022 - 2024 — McMap. All rights reserved.