gpt4 book ai didi

C 程序 : how can result of 1st few lines of code change based on what happens much later in program?

转载 作者:太空宇宙 更新时间:2023-11-04 01:38:36 25 4
gpt4 key购买 nike

我正在 Linux 机器上使用 gcc -std=C89 开关编写一些 C 代码。此 C 代码使用 OCILIB 库调用的 OCI 驱动程序与 Oracle 数据库通信。从数据库下载必要的数据后,C 程序调用一个 C 函数 (my_function) 来执行大量复杂的数学运算。程序流程如下所示:

int main (void) {
OCI_Connection *cn;
OCI_Statement *st;
OCI_Resultset *rs;
...
/* FIRST CALL TO DB */
OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT);
cn = OCI_ConnectionCreate(...);
st = OCI_StatementCreate(cn);
OCI_Prepare(st, ...);
OCI_Bindxxx(st, ...);
OCI_Execute(st);

printf(...); /* verify data retrieved from database is correct */

/* SECOND CALL TO DB */
OCI_Prepare(st, ...); /* different prepare stmt than above */
OCI_Bindxxx(st, ...);
OCI_Execute(st, ...);

printf(...); /* verify data retrieved from database is correct */

/* THIRD CALL TO DB */
OCI_SetFetchSize(st, 200);
OCI_Prepare(st, ...);
OCI_Bindxxx(st, ...);
OCI_Execute(st);
rs = OCI_GetResultset(st);
...
printf(...); /* verify data retrieved from database is correct */

OCI_Cleanup();
return EXIT_SUCCESS;

my_function(...);
}

如果我按所示运行程序,printf 语句都显示正确的数据已从数据库下载到 C 程序中。但是,my_function 还没有执行。

如果我将 return EXIT_SUCCESS 代码行从 my_function() 之前移动到 my_function() 之后,重新编译代码并运行它,printf 语句显示第一次调用数据库的数据在 C 程序中正确保存,但第二次调用的数据不正确,并且 printf 第三次通话的声明似乎没有做任何事情。

编译和运行时没有错误或警告报告。

我在 C(或 OCILIB)方面经验不足,但对于那些有经验的人,是否有合理的解释说明 return EXIT_SUCCESS 在代码中的位置如何与位于很久之前的代码进行交互它,造成这个?

在我简单的想法中,我认为代码一次执行一行,所以如果代码工作到第 123 行(例如),对第 456 行代码的更改应该不会影响结果直到第 123 行(例如,比较第 456 行的更改前后)。也许我错过了什么?

最佳答案

另一种可能性是您的代码依赖于未初始化变量的值,并且通过在调用 myfunction() 之前添加 return 您正在改变编译器的方式在内存中布置变量。

例如,优化编译器可能会注意到对 myfunction() 的调用由于 return 而无法访问,因此它可能能够避免为myfunction() 调用可能需要的临时变量。

确保您的编译器设置为警告使用未初始化的变量。

关于C 程序 : how can result of 1st few lines of code change based on what happens much later in program?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10038355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com