If you are getting the crash specifically at the
size_t len = sizeof(dastruct) / sizeof(struct my_struct);
line, it might be caused by sizeof(struct my_struct)
evaluating to 0. (I.e the problem is actually the division by zero). This might happen in some compilers when type struct my_struct
is incomplete, i.e. it is not defined. Using incomplete type in sizeof
is illegal in C, but some compilers allow it for some reason, evaluating it to 0. (Although I'd expect the compiler to catch this division by 0 at compile time.)
Your code is obviously fake and doesn't illustrate the problem. If the above theory is correct, most likely in your real code you either mistyped the name of the type struct my_struct
in sizeof
or forgot to include the definition of the type struct my_struct
.
(Highly unlikely, but anyway...)