SupaDupa
SupaDupa

Reputation: 181

Get size of 3D char* Array

i'm trying to make a multidimensional array, which holds weapons and their attachments: I have lots of arrays like this:

char *G18[19] = { "glock_mp", "glock_akimbo_mp", "glock_eotech_mp", "glock_fmj_mp", "glock_reflex_mp", "glock_silencer_mp", "glock_xmags_mp", "glock_akimbo_fmj_mp", "glock_akimbo_silencer_mp", "glock_akimbo_xmags_mp", "glock_eotech_fmj_mp",
        "glock_eotech_silencer_mp", "glock_eotech_xmags_mp", "glock_fmj_reflex_mp", "glock_fmj_silencer_mp", "glock_fmj_xmags_mp", "glock_reflex_silencer_mp", "glock_reflex_xmags_mp", "glock_silencer_xmags_mp" };

But all weapons don't belong to same category, f.e. an AK47 is an Assault Rifle and this G18 is a Machine Gun. So i created 2D Arrays which represnt a category, like so:

char **MACHINEGUNS[4] = { G18, TMP, RAFFICA, PP2000 };

so now i have the weapons sorted i created another array which should hold the categories, like so:

char ***WEAPONS[7] = { ASSAULTRIFLES, SUBMACHINEGUNS, LIGHTMACHINEGUNS, SNIPERS, PISTOLS, MACHINEGUNS, SHOTGUNS };

accessing the weapons like

char *weapon = WEAPONS[assaultrifle][ak47][0]; // assaultrifle & ak47 are enum mebers

works perfectly fine. The problem i'm facing is that i can't get the row and column sizes. F.e. if i want to know how many weapon classes their are i would do:

sizeof(WEAPONS)

which gives me 7. If i want to know how many assaultrifles there are i do:

sizeof(WEAPONS[assaultrifles])

But here's the problem: This gives me 4 although the assaultrifle's array size is 9:

char **ASSAULTRIFLES[9] = { AK47, M16A4, M4A1, FN2000, ACR, FAMAS, FAL, SCARH, TAR21 };

it returns 4 aswell if i do this:

sizeof(WEAPONS[assaultrifles][ak47])

even though the ak47's array size is 39. Any idea why it's not working and how i could achiev this? Thanks in advance and sorry for the long text!

Upvotes: 0

Views: 160

Answers (1)

javidcf
javidcf

Reputation: 59701

There is no such thing as "getting the size" of an array at runtime in C or C++. You can get the size of an array (declared with [<size>]) with sizeof because its size is known at compile time. When you get the sizeof a pointer you are literally getting how many bytes a pointer takes, which is why you get 4. If you want to know the size of a vector at runtime, the typical options are:

  • Use std::vector, and STL containers in general. There are a million tutorials and examples out there that you can check about that.
  • Instead of storing raw arrays (or pointers to arrays), store simple structs or classes containing the pointer to the array and the size. This is kinda like rolling your own, limited std::vector, and you have to be careful of freeing the memory correctly where necessary and so on, so I'm not sure it's a great choice.
  • Some other dirty C-like trick like adding NULL/nullptr at the end of every array and then iterate through the whole thing until you reach it every time you need to find the size. Inefficient and error-prone.

So, yeah, in short, use std::vector or other containers.

Upvotes: 2

Related Questions