I want to add a string after the struct in memory. How to check that i dynamically allocated right amount of bytes?
Example:
const wchar_t* add_str = L"test string"; struct test_{ wchar_t* name; size_t namelen; } test; void* ptest_void = malloc(sizeof(test) + wcslen(add_str)*sizeof(wchar_t)); // i cant dereference void*, hence, cant check sizeof(*ptest_void) // then i try to get sizeof of a ptr which was cast to (test_*): test_* ptest = (test_*)ptest_void; size_t ptest_sz = sizeof(*ptest); // ptest_sz has the size of _test struct, but without size of add_str... free(ptest_void);
sizeof(pointer)returns size of pointer, not the content it points to.malloc/freein C++. Usenew/new[]/delete/delete[]if you must, but in general try to avoid manual memory management entirely. Use containers first, smart pointers if you have to, raw manual memory management almost never.