运行 发布版本时出现分段错误,但代码块中没有调试版本
Getting a segmentation fault when running Release version, but not Debug version in Code Blocks
我正在使用代码块:
Release 13.12 rev 9501 (2013-12-25 18:25:45) gcc 4.8.2 Linux/unicode - 64 bit
在以下系统上:
Linux Mint 17.3 Cinnamon (version 2.8.7) 64-bit
Intel Core i7-4790
NVIDIA GeForce GTX 750 Ti
当我将 selected 目标设置为 "Debug" 并且 运行 我的代码时,它运行良好。我有 运行 多次,没有任何问题。但是当我从命令提示符 select "Release" 目标和 运行 程序时,经常会出现分段错误。
我的C++程序大约有一千行,我不知道程序的哪一部分导致了段错误。所以我不知道在这个阶段哪个代码是相关的(当我有更多信息时,我会 post 一些代码在这里)。它确实使用指针和动态数据结构。我猜有些东西没有被正确初始化,但我不知道如何去调试 "Release" 版本。我没有更改任何默认值的编译器或链接器设置。 "Release" 版本中有什么不同会导致此问题?
(更新)按照 Nathan 的建议,我能够隔离分段错误。有一个 for 循环,但在某些情况下,此循环的上限(结束索引)未被初始化,如下所示:
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;
}
我通过将 "end_idx" 变量初始化为零("st_idx" 总是大于一)解决了这个问题:
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;
}
是否可以让代码块/GCC 编译器在变量未像这样初始化时发出警告?我看到有一个 GCC 选项:-Wuninitialized,但我没有在代码块中看到它。
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
您可以添加 编译器设置 -> 编译器标志 中不可用的任何编译器选项
通过从工作区树视图导航菜单 您的项目 -> 构建选项 ->
{调试|发布} -> 编译器设置 -> 其他编译器选项
并将它们列在文本框中。
但是这样加-Wuninitialized
是没有必要的,因为
由 -Wall
启用,并由您的代码和通常的发布版本激发
优化 -O2
(或任何高于 -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;
}
使用 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;
^
这至少可以追溯到 GCC 4.4.7
-Wall
从 Code::Blocks 编译器设置 -> 编译器标志 菜单启用
通过勾选项目 Warnings -> Enable all common compiler warnings.
在Code::Blocks 16.01(我现在有)中甚至不需要这个设置
因为 -Wall
默认情况下为 Debug 和 Release 配置启用,
因此警告正式出现在默认的 Code::Blocks 控制台项目发布版本中
的 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))
据我所知,-Wall
一直是 GCC 的默认选项
Code::Blocks 我用了 6 年左右,但也许我错了。
我正在使用代码块:
Release 13.12 rev 9501 (2013-12-25 18:25:45) gcc 4.8.2 Linux/unicode - 64 bit
在以下系统上:
Linux Mint 17.3 Cinnamon (version 2.8.7) 64-bit
Intel Core i7-4790
NVIDIA GeForce GTX 750 Ti
当我将 selected 目标设置为 "Debug" 并且 运行 我的代码时,它运行良好。我有 运行 多次,没有任何问题。但是当我从命令提示符 select "Release" 目标和 运行 程序时,经常会出现分段错误。
我的C++程序大约有一千行,我不知道程序的哪一部分导致了段错误。所以我不知道在这个阶段哪个代码是相关的(当我有更多信息时,我会 post 一些代码在这里)。它确实使用指针和动态数据结构。我猜有些东西没有被正确初始化,但我不知道如何去调试 "Release" 版本。我没有更改任何默认值的编译器或链接器设置。 "Release" 版本中有什么不同会导致此问题?
(更新)按照 Nathan 的建议,我能够隔离分段错误。有一个 for 循环,但在某些情况下,此循环的上限(结束索引)未被初始化,如下所示:
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;
}
我通过将 "end_idx" 变量初始化为零("st_idx" 总是大于一)解决了这个问题:
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;
}
是否可以让代码块/GCC 编译器在变量未像这样初始化时发出警告?我看到有一个 GCC 选项:-Wuninitialized,但我没有在代码块中看到它。
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
您可以添加 编译器设置 -> 编译器标志 中不可用的任何编译器选项 通过从工作区树视图导航菜单 您的项目 -> 构建选项 -> {调试|发布} -> 编译器设置 -> 其他编译器选项 并将它们列在文本框中。
但是这样加-Wuninitialized
是没有必要的,因为
由 -Wall
启用,并由您的代码和通常的发布版本激发
优化 -O2
(或任何高于 -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;
}
使用 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;
^
这至少可以追溯到 GCC 4.4.7
-Wall
从 Code::Blocks 编译器设置 -> 编译器标志 菜单启用
通过勾选项目 Warnings -> Enable all common compiler warnings.
在Code::Blocks 16.01(我现在有)中甚至不需要这个设置
因为 -Wall
默认情况下为 Debug 和 Release 配置启用,
因此警告正式出现在默认的 Code::Blocks 控制台项目发布版本中
的 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))
据我所知,-Wall
一直是 GCC 的默认选项
Code::Blocks 我用了 6 年左右,但也许我错了。