Thread: Generic Vector

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    2

    Generic Vector

    Hello my friends, I have a question.
    I started to write a library for data structures like hash maps, vectors and stacks. To make sure all kind of values can be passed every data argument is just a void*. That's fine if I want to store pointers, but not if I want to store numbers or actual structs, not only pointers to them. So I thought just use macros but the problem is, that I would have to write two versions for each macro, because I cannot pass e.g "&vector" but have to pass "vector" because the adress-of operator won't get evaluated in a macro but instead it is used as its name. And I don't want to create a temporary variable everytime I want to use these macros just for this. Then there is this other approach where you never pass the actual data into the function and it returns an index in the array that you then change. This -> Generic Vector Type in C * GitHub is an example for this. The problem is first, you have to cast the array everytime you want to index it because the type is void and second this only works for data structures where the data is laid out in a plain array. What do you do if you want for example a vector that could store not only pointers but also structs or numbers?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,493
    Quote Originally Posted by Genosse Mendes
    To make sure all kind of values can be passed every data argument is just a void*. That's fine if I want to store pointers, but not if I want to store numbers or actual structs, not only pointers to them.
    You could store generic regions of memory instead (i.e., objects, except for the lack of type information beyond the size), by recording the size of one object of the given type. This way, you still work with a dynamic array of void*, but instead of the objects being external to the vector, they are owned by the vector. It would still be up to the user of the vector to cast the void* to the correct type, but you would be able to store numbers or objects of struct type and "assign" to them (via memcpy or say, a callback function).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2020
    Posts
    2
    Thank you very much for your answer. I do not fully understand what this should look like. Could you maybe write a little bit of pseudo-code?

Popular pages Recent additions subscribe to a feed

Tags for this Thread