Reputation: 3382
I am using Code Blocks:
Release 13.12 rev 9501 (2013-12-25 18:25:45) gcc 4.8.2 Linux/unicode - 64 bit
On the following system:
Linux Mint 17.3 Cinnamon (version 2.8.7) 64-bit
Intel Core i7-4790
NVIDIA GeForce GTX 750 Ti
When I set selected target to "Debug" and run my code, it works perfectly. I have run it many times without any problem. But when I select the "Release" target and run the program from the command prompt quite often there is a segmentation fault.
My C++ program is about a thousand lines, and I don't know what part of the program is causing the segmentation fault. So I don't know which code is relevant at this stage (I will post some code here when I have more information). It does make use of pointers and a dynamic data structure. I am guessing that something is not being initialised correctly, but I don't know how to go about debugging the "Release" version. I have not changed any of the compiler or linker settings from the default values. What could be different in the "Release" version to cause this problem?
(Update) Following Nathan's advice I was able to isolate the segmentation fault. There is a for loop, but under certain circumstances the upper limit (end index) of this loop was not being initialised, along the lines of:
void fnProc(bool var_val, int inp_val, int test_val, int st_idx, int lim_idx, int xarr[])
{
int idx, end_idx;
if (test_val > inp_val)
end_idx = someFn(inp_val, lim_idx, xarr);
if (!var_val)
for (idx = st_idx; idx <= end_idx; idx++)
xarr[idx] = 0;
}
I was able to fix the problem, by initialising the "end_idx" variable to zero ("st_idx" is always greater than one):
void fnProc(bool var_val, int inp_val, int test_val, int st_idx, int lim_idx, int xarr[])
{
int idx, end_idx;
if (test_val > inp_val)
end_idx = someFn(inp_val, lim_idx, xarr);
else
end_idx = 0;
if (!var_val)
for (idx = st_idx; idx <= end_idx; idx++)
xarr[idx] = 0;
}
Is it possible to get Code Blocks / GCC compiler to issue a warning when a variable has not been initialised like this? I see there is a GCC option: -Wuninitialized, but I don't see this in Code Blocks.
Upvotes: 3
Views: 6083
Reputation: 61452
Is it possible to get Code Blocks / GCC compiler to issue a warning when a variable has not been initialised like this? I see there is a GCC option: -Wuninitialized, but I don't see this in Code Blocks
You can add any compiler options that are not available from the Compiler settings -> Compiler flags menu by navigating from the workspace tree-view Your Project -> Build options -> {Debug|Release} -> Compiler settings -> Other compiler options and listing them in the text-box.
However, it is unnecessary to add -Wuninitialized
in this way because it
is enabled by -Wall
and is provoked by your code with the usual release build
optimisation -O2
(or any higher than -O0
):-
foo.cpp
extern int someFn(int, int, int[]);
void fnProc(bool var, int inp_val, int test_val, int st_idx, int lim_idx, int xarr[])
{
int idx, end_idx;
if (test_val > inp_val)
end_idx = someFn(inp_val, lim_idx, xarr);
if (!var)
for (idx = st_idx; idx <= end_idx; idx++)
xarr[idx] = 0;
}
With g++ 5.2:
$ g++ -O2 -Wall -c foo.cpp
foo.cpp: In function ‘void fnProc(bool, int, int, int, int, int*)’:
foo.cpp:5:14: warning: ‘end_idx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
int idx, end_idx;
^
and this holds good back at least as far back as GCC 4.4.7
-Wall
is enabled from the Code::Blocks Compiler settings -> Compiler flags menu
by ticking item Warnings -> Enable all common compiler warnings.
In Code::Blocks 16.01 (which I currently have) even this setting is unnecessary
because -Wall
is enabled for both Debug and Release configurations by default,
thus the warning duly appears in a default Code::Blocks console project release build
of foo.cpp
:-
-------------- Build file: Release in deleteme (compiler: GNU GCC Compiler 5.2)---------------
g++-5 -Wall -fexceptions -O2 -c /home/imk/develop/deleteme/foo.cpp -o obj/Release/foo.o
/home/imk/develop/deleteme/foo.cpp: In function ‘void fnProc(bool, int, int, int, int, int*)’:
/home/imk/develop/deleteme/foo.cpp:5:14: warning: ‘end_idx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
int idx, end_idx;
^
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 1 warning(s) (0 minute(s), 0 second(s))
As far as I recall, -Wall
has always been a default GCC option in
Code::Blocks for the 6 years or so that I've used it, but maybe I am mistaken.
Upvotes: 4