gpt4 book ai didi

c++ - SIGSEGV 同时尝试读取数组

转载 作者:行者123 更新时间:2023-11-28 02:18:23 25 4
gpt4 key购买 nike

我已经对此进行了一些研究,在发现没什么帮助之后,然后用尽我所有的选择来尝试解决这个问题,我现在决定向 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;
}`

最佳答案

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

关于c++ - SIGSEGV 同时尝试读取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33332223/

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