试图读取数组时出现SIGSEGV
SIGSEGV whilst attempting to read array
我对此做了一些研究,在发现没有多少有用的东西之后,然后用尽了我所有的选择来尝试解决这个问题,我现在决定向Stack Overflow社区寻求建议。我就是弄不明白为什么我在尝试使用form = new_form(field);
时得到SIGSEGV。
所讨论的函数是这个:
void GkForms::formNavLabel(std::shared_ptr<WINDOW> display, FIELD *field[], std::vector<char *> fieldNames, const size_t &fieldCount)
{
try {
// Clear the WINDOW
assert(display != NULL);
wclear(display.get());
// Initialization options
cbreak();
noecho();
keypad(display.get(), TRUE);
int formWinRows = 0;
int formWinCols = 0;
int subWinRows = 0;
int subWinCols = 0;
int ch = 0;
static FORM *form;
for (size_t i = 0; i < fieldCount; ++i) {
if (fieldNames.at(i) == nullptr) {
throw std::runtime_error(gettext("There has been an internal error with creating the form."));
}
}
// Initialize the fields
for (size_t i = 0; i < fieldCount; ++i) {
field[i] = makeLabelActive(i, 0, fieldNames.at(i));
assert(field[i] != NULL);
}
// Set field options
for (size_t i = 0; i < fieldCount; ++i) {
set_field_back(field[i], COLOR_PAIR(15));
set_field_fore(field[i], COLOR_PAIR(16));
field_opts_on(field[i], O_VISIBLE);
field_opts_on(field[i], O_ACTIVE);
field_opts_off(field[i], O_EDIT);
}
// Create the form and post it
form = new_form(field);
实现这个函数使用的代码在这里:
std::vector<xmlConfig::Servers> xmlData = data_serverMenu(xmlCfgFile);
unsigned short lineCount = 0;
unsigned short linesPerPage = (subScrollYSize - (borderSize * 2));
std::vector<char *> output;
short pages = (linesPerPage / xmlData.size());
short curPage = 1;
int ch;
for (size_t i = 0; i < xmlData.size(); ++i) {
if (i < linesPerPage) {
i = (i * curPage);
std::stringstream ss;
ss << " [ " << xmlData.at(i).serverProtocol.c_str() << " ] " << xmlData.at(i).serverName.c_str() << " ";
output.push_back(const_cast<char *>(ss.str().c_str()));
++lineCount; // Do not get rid of this!
}
}
FIELD *fields[(lineCount + 1)];
for (unsigned short i = 0; i < (lineCount + 1); ++i) {
fields[i] = new FIELD();
if (i == (lineCount + 1)) {
fields[i] = 0;
}
}
std::unique_ptr<GkForms> gkForms (new GkForms());
gkForms->formNavLabel(display, fields, output, lineCount);
代码是相当混乱的时刻,因为它是在开发/实验的中间,但你可能会注意到,我正在实现一个NCurses应用程序,其中c++接口与C代码。如有任何帮助,我将不胜感激,谢谢。
注:我还决定包含这个辅助函数,以防需要它来诊断问题。
/**
* @brief GkForms::makeLabelActive creates form 'labels' that can be selected and interacted with
* @author Phobos Aryn'dythyrn D'thorga
* @param frow Position on the y-axis (NOTE: it is reversed, a possible bug?)
* @param fcol Position on the x-axis (NOTE: it is reversed, a possible bug?)
* @param label What you wish for the label to display as text
* @return Returns a complete FIELD object, ready to be used by the NCurses
* forms library
*/
FIELD *GkForms::makeLabelActive(int frow, int fcol, char *label)
{
FIELD *f = new_field(1, (int) strlen(label), frow, fcol, 0, 0);
if (f) {
set_field_buffer(f, 0, label);
set_field_opts(f, (int) ((unsigned) field_opts(f) & O_ACTIVE));
}
return f;
}`
In
output.push_back(const_cast<char *>(ss.str().c_str()));
的指针ss.str().c_str()
在语句结束时无效。
您需要动态分配副本,或者开始使用std::string
。
还有其他问题;
fields[i] = new FIELD();
if (i == (lineCount + 1)) {
fields[i] = 0;
}
例如,如果条件为真,是内存泄漏。
弃用const
不应不认真考虑后果。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- SIGSEGV, 分段错误. 而 printf() 数组索引的值
- 声明数组时出现 SIGSEGV 错误,而不创建新的 int[size]
- SIGSEGV 在堆上的巨大数组上(我想是的)
- 访问类数组时的SIGSEGV
- char数组c((SIGSEGV)(核心转储))
- 指向结构的指针的动态数组在成员变量赋值时抛出 SIGSEGV
- 试图读取数组时出现SIGSEGV
- c++循环中的SIGSEGV和一个非常大的数组
- 在使用malloc的数组中出现SIGSEGV错误
- SIGSEGV 使用程序集访问数组元素时
- SIGSEGV,使用数组的分段错误