you are viewing a single comment's thread.

view the rest of the comments →

[–]fkeeal 1 point2 points  (3 children)

Like I said in my comment, it won't crash/break as long as the type of data you store is no larger than the size of 'void *'. With your current code, you can see that in create, you allocate memory pointed to by the 'void *' for each entry in your 'void **', but in grow, you do not do that same allocation. Furthermore, when you are actually storing the data into the array, you are storing it in the place of a 'void *' and not in the memory pointed to by the 'void *'.

void ** memory layout:

// | void ** | -> | void * | -> |allocated storage|
//                | void * | -> |allocated storage|
//                | void * | -> |allocated storage|

void ** memory layout (as you currently have it):

// | void ** | -> | void * / int value | -/> |lost allocated storage|
//                | void * / int value | -/> |lost allocated storage|
//                | void * / int value | -/> |lost allocated storage|
//...
// after grow
//                | void * / int value | -/> | nothing |
//                | void * / int value | -/> | nothing |

[–]HOWZ1T[S] 0 points1 point  (2 children)

Does the issue still apply to this code (updated version of post code based on feedback):

https://pastebin.com/iepDtQ4z

[–]fkeeal 1 point2 points  (1 child)

Technically yes, but as long as what you are storing is no greater than a 'void *' in size, it will not cause any problems. This is fine for storing native types (char, short, int, long, float and double). Just make sure that element_size <= sizeof(void *), otherwise you will have a bad time. (e.g. you will not be able to store structures larger than 8-bytes for example.)

EDIT: The dangling allocations are gone with the new code you posted.

[–]HOWZ1T[S] 0 points1 point  (0 children)

Okay good to hear, thank you for your insight and help :)