nothings / stb

stb single-file public domain libraries for C/C++
https://twitter.com/nothings
Other
26.84k stars 7.72k forks source link

stb_image_write: silence C++ warnings for = {0} #1311

Open N-R-K opened 2 years ago

N-R-K commented 2 years ago

Closes: https://github.com/nothings/stb/issues/1099


Grep finds a couple more instances of { 0 } being used:

$ grep -Hnr --exclude-dir=tests --exclude-dir=.git --exclude-dir=deprecated -E "\{ ?0 ?}"
stb_vorbis.c:5137:   { 0 },
stb_vorbis.c:5248:      static int channel_selector[3][2] = { {0}, {PLAYBACK_MONO}, {PLAYBACK_LEFT, PLAYBACK_RIGHT} };
stb_voxel_render.h:2044:   stbvox_mesh_face face_data = { 0 };
stb_voxel_render.h:2683:   stbvox_mesh_vertex p1[4] = { 0 };
stb_ds.h:481:// this is a simple string arena allocator, initialize with e.g. 'stbds_string_arena my_arena={0}'.
stb_ds.h:764:  stbds_array_header temp={0}; // force debugging
stb_ds.h:1662:  stbds_string_arena                        sa      = { 0 };
stb_image.h:5026:   stbi_uc has_trans=0, tc[3]={0};
stb_image.h:5815:   unsigned char raw_data[4] = {0};
stb_herringbone_wang_tile.h:877:   stbhw_config c = { 0 };
stb_herringbone_wang_tile.h:878:   stbhw__process p = { 0 };
stb_connected_components.h:415:   unsigned char num_adj[STBCC__MAX_CLUMPS_PER_CLUSTER] = { 0 };
stb_connected_components.h:713:   unsigned char connected[STBCC__MAX_EDGE_CLUMPS_PER_CLUSTER][STBCC__MAX_EDGE_CLUMPS_PER_CLUSTER/8] = { { 0 } };
stb_connected_components.h:777:   unsigned char disconnected[STBCC__MAX_EDGE_CLUMPS_PER_CLUSTER][STBCC__MAX_EDGE_CLUMPS_PER_CLUSTER/8] = { { 0 } };

Fixing them should be the same as this PR, should I go ahead and update those too?

nothings commented 2 years ago

We didn't really resolve this in #1099. I'm not a big fan of using memset() because it's ugly, but I'm not really a big fan of ZERO_INIT because it introduces the #ifdef. I mean, I understand it's harmless, it's just ugly in its own way.

If we did take this PR, it needs to be STBIW_ZERO_INIT and etc. for each library, so they don't interfere if the user has "ZERO_INIT" in their code with some other meaning. (And simply #undefing at the end isn't sufficient, since they could #define it before including us.)

N-R-K commented 2 years ago

I'm not a big fan of using memset() because it's ugly, but I'm not really a big fan of ZERO_INIT because it introduces the #ifdef. I mean, I understand it's harmless, it's just ugly in its own way.

I agree actually.

If we did take this PR, it needs to be STBIW_ZERO_INIT and etc. for each library, so they don't interfere if the user has "ZERO_INIT" in their code with some other meaning.

That's true. I'll wait until you make up your mind on it before updating the PR then.

NB: I'm fine if this is rejected, only opened it because you agreed to use memset() which I think is more ugly than the macro.

N-R-K commented 2 years ago

Updated to using memset(), compiles fine on both gcc and clang without any warnings with -Wall -Wextra -Wmissing-field-initializers.

Sokolmish commented 2 years ago

There are also several places where such initializer is used:

N-R-K commented 2 years ago

There are also several places where such initializer is used:

* stb_ds.h in line [764](https://github.com/nothings/stb/blob/af1a5bc352164740c1cc1354942b1c6b72eacb8a/stb_ds.h#L764) (also, I don't know for what purpose this variable is used here).

* stb_image.h in lines [5026](https://github.com/nothings/stb/blob/af1a5bc352164740c1cc1354942b1c6b72eacb8a/stb_image.h#L5026) and [5815](https://github.com/nothings/stb/blob/af1a5bc352164740c1cc1354942b1c6b72eacb8a/stb_image.h#L5815).

Thanks, I updated the list in https://github.com/nothings/stb/pull/1311#issue-1167509162 along with the grep expression to catch {0} (no spaces between braces and zero) as well.