Thread: Can someone remind me how to tell gcc not to 0 initialise everything in debug mode

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,735

    Can someone remind me how to tell gcc not to 0 initialise everything in debug mode

    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

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    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:
    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;
    }
    Output:
    $ 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

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,735
    Quote Originally Posted by john.c View Post
    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:
    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;
    }
    Output:
    $ gcc zeroed.c -g
    $ ./a.out
    main: non-zero detected
    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.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by awsdert View Post
    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 initialized
    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.

  5. #5
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,735
    Quote Originally Posted by rstanley View Post
    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.
    I don't think it's global variables that are the issue but I might as well try. Thanks for the link though, might prove more helpful than google in this case.

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by awsdert View Post
    I don't think it's global variables that are the issue but I might as well try. Thanks for the link though, might prove more helpful than google in this case.
    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!

    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.
    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.

    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.

  7. #7
    Registered User
    Join Date
    Oct 2019
    Posts
    82
    Quote Originally Posted by rstanley View Post

    As for realloc() you would need to memset() the added memory.
    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);

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by ghoul View Post
    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);
    Why do you think that was NOT what I was referring to??? I didn't think I had to give an explicit example to the OP!

  9. #9
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,735
    Quote Originally Posted by rstanley View Post
    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.

    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.
    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.

  10. #10
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by awsdert View Post
    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.
    When you say, "initialization", do you mean at compile time with a constant, or "assignment" later.
    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;
    }
    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.

  11. #11
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Try running with valgrind.

    This brings back up what I was typing in a different thread:

    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.
    In this case, that next person is might be you.

  12. #12
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,735
    Quote Originally Posted by hamster_nz View Post
    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.
    Well that's not possible, the enums have counts built into them, I use those counts when declaring the array size so the compiler is able to catch it before it can reach production stage

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    >There ARE legitimate reasons to avoid initialising all variables, namely speed, that initialisation takes an instruction
    Yet here you are, scrambling to figure out where you screwed up.

    The imaginary microsecond you saved has turned into very real days of wtf is wrong with my code.

    Your first order of business is to make the damn thing work by the most reliable means possible.
    One of those things is rigorous initialisation of variables.

    When it works, then you profile and optimise.

    > yes it means the occasional bug creeps in when I forget it's not initialised but it's not often that happens.
    ROFLMAO

    How many times do you plan falling down the same hole, wondering how to get yourself out of it, before you realise a better way might be to avoid falling down the hole in the first place?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,735
    Quote Originally Posted by Salem View Post
    >There ARE legitimate reasons to avoid initialising all variables, namely speed, that initialisation takes an instruction
    Yet here you are, scrambling to figure out where you screwed up.

    The imaginary microsecond you saved has turned into very real days of wtf is wrong with my code.

    Your first order of business is to make the damn thing work by the most reliable means possible.
    One of those things is rigorous initialisation of variables.

    When it works, then you profile and optimise.

    > yes it means the occasional bug creeps in when I forget it's not initialised but it's not often that happens.
    ROFLMAO

    How many times do you plan falling down the same hole, wondering how to get yourself out of it, before you realise a better way might be to avoid falling down the hole in the first place?
    Say what you will, I don't mind the time lost, I have an eternity in heaven to look forward to, this life is just a pastime until God declares it time to take me.

  15. #15
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Quote Originally Posted by awsdert View Post
    Say what you will, I don't mind the time lost, I have an eternity in heaven to look forward to, this life is just a pastime until God declares it time to take me.
    Since you bring god into it:
    There is an old joke, about a person who dies in a flood, after telling all their potential rescuers that "Gold will provide".

    In heaven they ask "God, why did you forsake me, and leave me to die"?

    And God says "What! I sent you a car, a truck, a boat and a helicopter, what more could I do?".
    Your project works in "debug mode" (by this I assume you mean with no optimizations), but not in "release mode" (optimizations turned on). If that is the case it is far more likely that the optimizer are making use of an assumption you made in your code.

    Does it compile cleanly with ALL compiler warnings tuned on?

    You can still debug with optimizations turned on - it can just be a little more confusing, as the code is all jumbled up.

    If you can point me to how I can recreate this bug with minimal effort on my part (e.g. not much more than "git clone ..." and then type "make mybug") I can take a look at it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-24-2010, 01:35 PM
  2. Using the Debug mode ((F10) V.S 2005
    By csonx_p in forum Windows Programming
    Replies: 21
    Last Post: 06-17-2008, 05:06 PM
  3. Debug Mode
    By Coding in forum C# Programming
    Replies: 5
    Last Post: 02-14-2008, 03:00 PM
  4. Replies: 4
    Last Post: 09-16-2006, 07:11 PM
  5. Debug Mode Vs. Release Mode
    By incognito in forum Tech Board
    Replies: 5
    Last Post: 12-18-2003, 04:06 PM

Tags for this Thread