Find the answer to your Linux question:
Results 1 to 6 of 6
Hi All, GCC is not generating the object for part of the C++ source when an uninitialized local variable is used within an "if" block or used in another "if" ...
  1. #1
    Just Joined!
    Join Date
    Feb 2010
    Posts
    3

    GCC not generating object!

    Hi All,

    GCC is not generating the object for part of the C++ source when an uninitialized local variable is used within an "if" block or used in another "if" statement however when the variable is initialized at the begining, GCC start generating the object correctly. This is happening only when the code is compiled with optimization level 3 (-O3).

    main()

    {

    char apc;

    WorkAreaCIUMC* workAreaCIUMC ;

    /*Object code for the following "if" statement and subsequent "if" block is NOT generated by GCC due to local variable 'apc' uninitialized. */

    if((workAreaCIUMC->requestType == 'A')||
    (workAreaCIUMC->requestType == 'B'))
    {
    apc = 'Y';
    }

    /*Object code for the following source is generated by GCC */
    long long tempAmt = 0LL;
    long long calAmt = 0LL;
    const char chargeCCSGOPEN[] = "PGLME";
    const char ssp[] = "TSCPEVCSP";

    ---
    -----------
    -----------


    /*Object code for the following "if" statement and subsequent "if" block is NOT generated by GCC due to local variable 'apc' uninitialized. */

    if((strstr(ssp,workAreaCIUMC->transactionSource))&&
    (calAmt > 100 ) &&
    (workAreaCIUMC->glReturnCode == 0) &&
    (!(memcmp(workAreaCIUMC->billingCurrencyCode,"USD",
    sizeof(workAreaCIUMC->billingCurrencyCode))))&&
    (apc != 'Y'))
    {

    - -- ------------------
    - -----------------------------
    -----------------------------------------

    }

    }

    I can fix this by initializing the 'apc' variable at the begining of the program but the problem here is we are migrating more than 2000 C++ source files to GCC and its difficult to find out where else this problem exists. I am looking for a C++ compiler option which generates the object code irrespetive of whether the local variable is initialized or not when compiled with O3.

    Any pointers will be greatly appreciated. Thank you!

    -Samir

  2. #2
    Linux Enthusiast KenJackson's Avatar
    Join Date
    Jun 2006
    Location
    Maryland, USA
    Posts
    506
    I have several questions about your question.

    You are saying GCC and talking about compiling C++. Are you using the g++ command (not gcc)?

    In your code excerpt the variable WorkAreaCIUMC isn't initialized either. So workAreaCIUMC->requestType in the first if statement is using a wild pointer with unknowable results. Is it really initialized, but now shown?

    If the variable apc is declared on the stack and not initialized as shown here, how can you have any confidence the code will execute correctly?


    I think there are automated code checkers that analyze code for errors like uninitialized variables. I've never used one, but you could search sourceforge or your distro's repository.

    Actually, try turning on GCC's -Wall switch. I think that will give a warning for uninitialized variables.

  3. #3
    Just Joined!
    Join Date
    Feb 2010
    Posts
    3
    Following are my answers,

    1. I am using maketpf compiler tool for z/TPF. I believe it is invoking g++ internally.

    2. The code example I presented in the first post is just an extract of the source file. WorkAreaCIUMC is a pointer to a structure and mapped onto a data block.

    3. Thats correct, it may not execute correctly however this is currently running on production server based on z/OS.


    Compiler option which flags uninitialized variables would help in future developments however am looking for a compiler option that would not ignore the code using the uninitialized variables and generate the object for it as there are over 2000 C++ source file which maycontain similar kind of offended code and has to be migrated to the linux environment.

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Well, this is a problem. The correct means to remediate this is to fix the situations where this occurs. Your compiler should generate a warning about uninitialized variables unless you have the warning level set too low. That makes it fairly easy to fix. However, this is indicative of serious flaws in your code base. If they aren't initializing these variables, what about others, such as pointer variables? Personally, I think you have a LOT of work ahead of you.

    As Mr. Jackson said, there are automated code checking tools available to find this sort of stuff, but in my experience (considerable) the commercial ones are VERY costly. We invested well over $100,000 USD for one these tools when doing Y2K analysis back in 1998, and that was for something like 10 seats (or less). I don't think they are any less expensive today. Open source tools are probably available now, but how good they are I cannot say. Here is a wikipedia page for that which might point you in the right direction: List of tools for static code analysis - Wikipedia, the free encyclopedia

    My professional opinion, after 30 years of cross-platform development experience? Just because a "innocuous" fault runs correctly on 9 of 10 platforms, does not mean it will on the 10th. As I said, this type of fault is likely indicative of much more serious ones laying in wait.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Feb 2010
    Posts
    3
    Thanks for your inputs guys! Looks like its a case of faulty code which was not caught on old compiler (IBM z/OS) and it happily generated the corresponding object for it. GCC is newer and more strict in nature and did not forgive the bad code!

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Better safe than sorry! goes the old expression. As the engineer responsible for the formulation of standard development/build processes and practices at what was, when I left, one of the 100 biggest software companies, we always required that full warnings be enabled on our builds, and that no code was to be released until all warnings were resolved, unless the warnings were deemed either innocuous or unresolvable by a committee of senior engineers. Some examples of this sort of situation could be caused by 3rd party library headers that we were using for some functionality. In such cases, we would report these to the software provider(s) with the understanding that we would be unable to renew our contracts with them if not resolved within some reasonable time period. In case you are wondering, the software we developed and sold controls operations in 80% of the semiconductor fabs world-wide.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...