You seem to have some basic misunderstanding of arrays in C++. They have have a fixed size, known at compile-time. Zero-sized arrays are illegal. (Some compilers allow a zero-sized array when not invoked in standard mode, however they have unclear semantics, and certainly can't have any value stored in them).
Also you use sizeof wrongly. sizeof x tells you how many bytes are required to store x. (NOT anything that x may be pointing to, if x is a pointer, and not anything that x is a handle for, or a container class of). For example std::string is a container which has a pointer pointing to dynamically-allocated string contents. sizeof name only gets you the container size, it does not get you how much space has been dynamically allocated.
In your code, memsize can be determined at compile-time; it would never be different on different calls to the function.
Further, using a function CreateDIFDict is poor style as it forces dynamic allocation. Instead, provide a constructor. Then the user can choose whether they want dynamic allocation, and they can take advantage of move and copy semantics.
It's not clear whether or not you want DIFDict to take a copy of the strings and DIFColors that you are initializing it with. If you do not want a copy:
struct DIFDict
{
std::string name;
std::string *defs;
struct DIFColor **colors;
uint8_t defsize;
DIFDict(std::string name, std::string *defs, struct DIFColor **colors, uint8_t defsize) :
name(name), defs(defs), colors(colors), defsize(defsize) {}
};
Make very sure that you destroy all DIFDicts before you destroy the things that the pointers are pointing to. (Raw pointers do not have an ownership semantic in C++).
If you do want a copy:
struct DIFDict
{
std::string name;
std::vector<std::string> defs; // or some other container
// ??? colors
DIFDict(std::string name, std::string const *p_defs, uint8_t defsize)
: name(name), defs(p_defs, p_defs + defsize) {}
};
I'm not sure what colors is supposed to point to, if you give more information about the expected behaviour of colors I'll update this example.
malloc()doesn't call default constructors. Usenewinstead.[]anywhere in the struct definition; and C only permits it as the final member of a struct. (notwithstanding the fact thatstd::stringdoes not exist in C)