Tried googling it but google didn't seem to understand what I was looking for, brought up everything but, I need to stop it happening so I can track down variables that need to be initialised
Tried googling it but google didn't seem to understand what I was looking for, brought up everything but, I need to stop it happening so I can track down variables that need to be initialised
I am not aware that gcc zero-initializes everything in "debug mode". I assume you are talking about local variables since globals and statics are always zero-initialized if not explicitly initialized.
You could try looking here:
Debugging Options (Using the GNU Compiler Collection (GCC))
EDIT: I just tried the following:
Output:Code:#include <stdio.h> void f() { int a, b, c, d; if (a || b || c || d) printf("f: non-zero detected\n"); else printf("f: all zeroes\n"); } int main() { int a, b, c, d; if (a || b || c || d) printf("main: non-zero detected\n"); else printf("main: all zeroes\n"); return 0; }
$ gcc zeroed.c -g
$ ./a.out
main: non-zero detected
Last edited by john.c; 03-31-2022 at 05:30 PM.
A little inaccuracy saves tons of explanation. - H.H. Munro
I appreciate the link but I already looked there, that's what I meant by everything but what I was looking for, and yeah that non-zero situation is definitly what I'm looking for in debug mode yet when I run the code in release vs debug I never get the bug showing in debug (which is obviously annoying, how am I to catch a segfault that only happens in release mode, there won't be enough information for me to identify what function/code to look through). True the uninitialised data I'm looking for is usually 0'd even in release mode but occasionally I get a crash and I can only attribute it to uninitialised data, if only I knew where to look I could fix it.
If you are referring to global variables, then "-fno-zero-initialized-in-bss" may be what you are looking for. See this page for more information.
Local variables are never initialized to zero by default.
Also, please view a complete list of gcc options.
Last edited by rstanley; 03-31-2022 at 06:18 PM.
Try running with valgrind.
This brings back up what I was typing in a different thread:
In this case, that next person is might be you.I pity the next person who comes along, and should the debugger stops at "ENUM( Type, E_BUFF_TYPE_INDICE, GL_ELEMENT_ARRAY_BUFFER );" is left scratching their head. Only to find out that "GfxBufTypeEnums[]" has been declared too small.
However, it more than likely that the debugger won't stop, and your code will just trash a random memory location somewhere, giving them an almost unsolvable bug that will haunt their nightmares.
After having a good strong cup of coffee this morning, I can't understand why you would want any and all variables NOT initialized to zero or some legitimate value! I can't see how variables picking up garbage values left in memory, would in any way help to debug any program!
Seems simple. Go through your code and locate any local variables not initialized, and initialize them to zero or some legitimate value, and all pointers to NULL. As for malloc(), use memset(), or switch to calloc(). As for realloc() you would need to memset() the added memory. If that does not fix the problem, then you have more serious problems.True the uninitialised data I'm looking for is usually 0'd even in release mode but occasionally I get a crash and I can only attribute it to uninitialised data, if only I knew where to look I could fix it.
Also, as soon as you free() and allocated memory, ALWAYS set the pointer to NULL, to avoid accidentally read or write the memory freed! I list all this not just to you but for the benefit of all readers of this thread.
Last edited by rstanley; 04-01-2022 at 06:33 AM.
While this is possible, it might turn out more complicated than other cases which are simple enough.
My guess is that one would have to keep track of the size of the currently allocated memory chunk then do something like:
Code:memset(ptr + previous_size, 0, current_size - previous_size);
There ARE legitimate reasons to avoid initialising all variables, namely speed, that initialisation takes an instruction, if your function can potentially exit before the variable is needed then that's wasted time which can add up, I make a point of avoiding initialising variables until I actually need them, yes it means the occasional bug creeps in when I forget it's not initialised but it's not often that happens.
Dude chill, the only place my code uses realloc is in buffers & there's dedicate handlers for adjusting the count of used elements, those handlers all defer to 1 handler that clears newly "allocated" elements before handing back to the caller so realloc is nowhere near the issue.
It's definitely a variable in some function somewhere.
I was referring to a post by ghoul not you.
When you say, "initialization", do you mean at compile time with a constant, or "assignment" later.
Depending on the context, the compiler might optimize the code by converting the "assignment", to an "initialization". Bottom line, actual 'Initialization" has no overhead at run-time. Look at the assembler code.Code:int main(void) { int a; // Not initialized, assigned later int b = 0; // Initialization to 0 at compile time ... a = 0; // Initial assignment, or re-assignment later return 0; }
I mean for reasons like this:
Notice how I don't need the array variable until after I checked I'm going to be using it, in C99 I could just declare it later but in C89 it must be at the top of the scope, since I like to keep my code as C89 compatabile as possible I have to decide what scopes I need to declare my variables in, I also don't like going to far in with the scope like say this:Code:void* grow_buffer( struct buffer *B, uint total ) { void *array; if ( B->total >= total ) return B->array; array = realloc( B->array, B->Tsize * total ); if ( !array ) return NULL; B->array = array; B->total = total; return array; }
If there's decent reason to use a sub scope then I avoid it & stick to the upper scopeCode:void* grow_buffer( struct buffer *B, uint total ) { if ( B->total < total ) { void *array = realloc( B->array, B->Tsize * total ); if ( !array ) return NULL; B->array = array; B->total = total; } return B->array; }