bash shell脚本中的C++, super 初学者关于调用函数和调试的问题

标签 c++ bash unix gdb

我的任务是调试教授编写的程序。我们必须在 putty 中完成所有这些工作,而我是 bash shell 脚本编写方面的新手(阅读:我基本上对函数和处理 C++ 程序一无所知)。

所以我编译了 untitled.cpp 文件,它使程序无标题。然后我用 ./untitled 执行程序,让它运行。目前很好。程序显示“种子值?”,我根据作业输入给定值。然后,由于程序结束,命令提示符返回到 untitled 目录。我不确定下一步该做什么,因为这是我所有的问题。

这是指令:

Run the program (single step) and enter a seed value of 3222011. After calling the shuffle() function, what is the value of array[8]?

免责声明:我不是要答案,只是要如何找到答案。在我刚才提到的那个之后还有 7 个问题。

我的问题:

  1. 如何找到数组元素值?或者任何数据成员的值(value),真的吗?
  2. 稍后会说“您可以通过键入 gdb 命令 print array 在 main() 函数中打印整个数组的内容。”这是行不通的。它只在当前上下文中返回“无符号”数组“。
  3. “步入”一个函数是什么意思,我该怎么做?

给定的程序是:


#include <cstdlib>
#include <iostream>

using std::cout;
using std::cin;
using std::flush;

const int ARRAYSIZE = 30;

void fill(int ar[], int size);
void shuffle(int ar[], int size);

int main(void)
{

  int array[ARRAYSIZE] = {0}; // Clear out array
  int seed;

  cout << "Seed value? " << flush;
  cin >> seed;
  srand(seed);

  fill(array, ARRAYSIZE);

  shuffle(array, ARRAYSIZE);

  return 0;
}

void fill(int b[], int size)
{
  int index;

  // Place random values at random locations in the array
  for(int i = 0; i < 10 * size; i++)
    {
      index = rand() % size;
      b[index] = rand();
    }
}

void shuffle(int ar [], int size)
{
  int* first, * second;

  for(int j = 0; j < 5 * size; j++)
    {
      // Pick a random pair of positions to swap
      first  = ar + rand() % size;
      second = ar + rand() % size;

      // Swap
      int temp = *first;
      *first = *second;
      *second = temp;
    }
}

最佳答案

我几乎要在这里给你答案了。但是你应该学习如何使用 gdb。

你的老师正在测试这些东西 -- 你是否知道如何运行 gdb -- 你是否知道如何附加断点 -- 在使用 gdb 调试程序时是否知道如何打印值

你用 --ggdb 选项编译你的程序,例如

g++ -ggdb 无标题.cpp

这就是将 gdb 附加到程序的方式

gdb a.out

完成后,您将进入 gdb 命令提示符。您的程序尚未启动。在启动程序之前,您需要像这样创建一个“断点”

27

这在 untitled.cpp 的第 27 行创建了断点

然后输入r运行程序

r

输入您的种子值并按 Enter 键。您会看到 gdb 将您带到断点处,即程序的第 27 行。现在您可以打印您想要查看的值。

步骤如下

snegi@snegi-p7-1267c:~/Workspace/scripts$ gdb a.out 
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/satyarth/Workspace/scripts/a.out...done.
(gdb) b 27
Breakpoint 1 at 0x40094c: file untitled.cpp, line 27.
(gdb) run
Starting program: /home/satyarth/Workspace/scripts/a.out 
Seed value? 3222011

Breakpoint 1, main () at untitled.cpp:27
27    return 0;
(gdb) p array[8]
$1 = 560529867

关于bash shell脚本中的C++, super 初学者关于调用函数和调试的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19647113/

相关文章:

c++ - 如何使用 Qt 类和函数构建 XML 文件并转义所有非 Unicode 字符?

c++ - 由解决方法 : c++ : boost_log : how does compiler choose st or mt 修复

c++ - 获取变量的中间范围值

bash - 如何使用 bash 将充满 IP block 的文件批量添加到 IPTables

bash - 安装 Flutter 后从 Mac OS 终端运行 Dart 程序

c++ - 从核心转储中获取创建时间

c++ - 如何 Ransac cpp

bash - 在 Bash 中格式化结果

linux - 使用 sed 替换字符串

python - os.pathconf() 键及其含义