清除矢量列表后,我收到了分段错误 [英] I'm getting a Segmentation fault after I clear a vector list

查看:69
本文介绍了清除矢量列表后,我收到了分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



大家好。


我有分段故障问题。底部的代码部分

在第二次进入IF块时为

继续抛出Segmentation故障。


const int WORDS_PER_LINE = 4;


当counter == 7时,字符串连接在IF

块内失败。


但是,奇怪的部分是如果不清除列表(// list.clear())

它有效......但我害怕我当我没有释放内存时,
为向量重新分配不同的字符串。


代码片段

----- -------------------------------------------------- --------------------------------


while(str!= NULL )

{

index = counter%WORDS_PER_LINE;

list [index] = * str;


cout<< " I:" << index<< " C:" <<计数器<< "字: << * str<< endl;


if(index ==(WORDS_PER_LINE-1))

{

string ref =


string(" INSERT INTO BANKING(DATE,TRANS_TYPE,AMOUNT,BALANCE)

VALUES(TO_DATE(''")+

list [0] +

string("'',''MM / DD / YYYY''),''")+

list [1] +

string("'',")+

list [2] +

string(",")+

list [3] +

string(");");


cout<< 处理...... << ref<< endl;


list.clear();


}


counter ++;

delete(str);

str = parser.readField();

}

解决方案
" fudmore" <福***** @ mindspring.com>写道...

我有一个Segmentation fault问题。底部的代码部分在第二次进入IF块时不断抛出Segmentation故障。

const int WORDS_PER_LINE = 4;

当counter == 7时,字符串连接在IF
块中失败。

但是,奇怪的部分是如果不清除列表(//列表。 clear())
它的工作原理......但是当我向矢量重新分配不同的字符串时,我恐怕不会释放记忆。


什么''列表"在你的程序中?


Code Snippet
---------------------------- ----------------------------------------------
-------------
while(str!= NULL)
{
index = counter%WORDS_PER_LINE;
list [index] = * str ;

cout<< " I:" << index<< " C:" <<计数器<< "字: << * str<< endl;

if(index ==(WORDS_PER_LINE-1))
{
string ref =

string(" INSERT INTO BANKING(DATE) ,TRANS_TYPE,AMOUNT,BALANCE)
VALUES(TO_DATE(''")+
list [0] +
string("'',''MM / DD / YYYY'' ),''")+
list [1] +
string(&''',")+
list [2] +
string(", ")+
list [3] +
string(");");

cout<< 处理...... << ref<< endl;

list.clear();


为什么要在这里清除它?什么是清算的目的?

}

计数器++;
删除(str);
str = parser.readField();
}



我想''list.clear()''没有用,因为''list''是

修复为包含4个字符串。


调用''list.clear()'',''list''的大小更改为0,

然后''list [index]''将生成错误(索引超出范围)。

因为''WORDS_PER_LINE''是一个常量,也许可以使用C数组。

std :: string args [WORDS_PER_LINE];


" fudmore" <福***** @ mindspring.com>写了...

抱歉没有提到,但列表是

vector< string> list(WORDS_PER_LINE);


所以,你得到了答案吗?当您定义列表时像这样,

它包含''WORDS_PER_LINE''元素开头。一旦你清除了它,就没有更多的元素了,你不能再使用operator []来引用任何

,直到你调整向量的大小。 br />

因此,解决方案不是清除它。简单。


Victor

Victor Bazarov写道:

" fudmore" <福***** @ mindspring.com>写道......

我有一个Segmentation fault问题。底部的代码部分在第二次进入IF块时不断抛出Segmentation故障。

const int WORDS_PER_LINE = 4;

当counter == 7时,字符串连接在IF
块中失败。

但是,奇怪的部分是如果不清除列表(//列表。 clear())
它的工作原理......但是当我向矢量重新分配不同的字符串时,我恐怕不会释放记忆。



什么是'列表'?在你的程序中?



代码片段


-------------------------------------------------- ------------------------

-------------
< blockquote class =post_quotes> while(str!= NULL)
{
index = counter%WORDS_PER_LINE;
list [index] = * str;

cout << " I:" << index<< " C:" <<计数器<< "字: << * str<< endl;

if(index ==(WORDS_PER_LINE-1))
{
string ref =

string(" INSERT INTO BANKING(DATE) ,TRANS_TYPE,AMOUNT,BALANCE)
VALUES(TO_DATE(''")+
list [0] +
string("'',''MM / DD / YYYY'' ),''")+
list [1] +
string(&''',")+
list [2] +
string(", ")+
list [3] +
string(");");

cout<< 处理...... << ref<< endl;

list.clear();



为什么要在这里清除它?什么是清算的目的?

}

计数器++;
删除(str);
str = parser.readField();
}





Hello Everybody.

I have a Segmentation fault problem. The code section at the bottom
keeps throwing a Segmentation fault when it enters the IF block for the
second time.

const int WORDS_PER_LINE = 4;

when counter == 7 is when the string Concatenation fails within the IF
block.

BUT, the strange part is if the don''t CLEAR the list ( // list.clear())
it works... but i''m afraid that I''m not freeing up memory when i
reassign different strings to the vector.

Code Snippet
---------------------------------------------------------------------------------------

while(str != NULL)
{
index = counter % WORDS_PER_LINE;
list[index] = *str;

cout << "i:" << index << " c:" << counter << " word : " << *str << endl;

if(index == (WORDS_PER_LINE-1))
{
string ref =

string("INSERT INTO BANKING(DATE,TRANS_TYPE,AMOUNT,BALANCE)
VALUES(TO_DATE(''") +
list[0] +
string("'',''MM/DD/YYYY''),''") +
list[1] +
string("'',") +
list[2] +
string(",") +
list[3]+
string(");");

cout << "processing .." << ref << endl;

list.clear();

}

counter++;
delete (str);
str = parser.readField();
}

解决方案

"fudmore" <fu*****@mindspring.com> wrote...

I have a Segmentation fault problem. The code section at the bottom
keeps throwing a Segmentation fault when it enters the IF block for the
second time.

const int WORDS_PER_LINE = 4;

when counter == 7 is when the string Concatenation fails within the IF
block.

BUT, the strange part is if the don''t CLEAR the list ( // list.clear())
it works... but i''m afraid that I''m not freeing up memory when i
reassign different strings to the vector.
What''s "list" in your program?

Code Snippet
-------------------------------------------------------------------------- -------------
while(str != NULL)
{
index = counter % WORDS_PER_LINE;
list[index] = *str;

cout << "i:" << index << " c:" << counter << " word : " << *str << endl;

if(index == (WORDS_PER_LINE-1))
{
string ref =

string("INSERT INTO BANKING(DATE,TRANS_TYPE,AMOUNT,BALANCE)
VALUES(TO_DATE(''") +
list[0] +
string("'',''MM/DD/YYYY''),''") +
list[1] +
string("'',") +
list[2] +
string(",") +
list[3]+
string(");");

cout << "processing .." << ref << endl;

list.clear();
Why do you want to clear it here? What''s the purpose of clearing?

}

counter++;
delete (str);
str = parser.readField();
}



i guess there has no use for ''list.clear()'', because ''list'' is
fixed to hold 4 strings in it.

after you invoke ''list.clear()'', the size of ''list'' change to 0,
then ''list[index]'' would generate an error(index out of range).
Since ''WORDS_PER_LINE'' is a const, maybe C arrays can be used.
std::string args[WORDS_PER_LINE];


"fudmore" <fu*****@mindspring.com> wrote...

sorry for not mentioning that but list is

vector <string>list(WORDS_PER_LINE);
So, did you get your answer? When you define your "list" like this,
it contains ''WORDS_PER_LINE'' elements to begin with. As soon as you
clear it, there are no more elements, and you cannot refer to any of
them using operator[] any longer until you resize the vector.

So, the solution is not to clear it. Simple.

Victor

Victor Bazarov wrote:

"fudmore" <fu*****@mindspring.com> wrote...

I have a Segmentation fault problem. The code section at the bottom
keeps throwing a Segmentation fault when it enters the IF block for the
second time.

const int WORDS_PER_LINE = 4;

when counter == 7 is when the string Concatenation fails within the IF
block.

BUT, the strange part is if the don''t CLEAR the list ( // list.clear())
it works... but i''m afraid that I''m not freeing up memory when i
reassign different strings to the vector.


What''s "list" in your program?



Code Snippet


--------------------------------------------------------------------------

-------------

while(str != NULL)
{
index = counter % WORDS_PER_LINE;
list[index] = *str;

cout << "i:" << index << " c:" << counter << " word : " << *str << endl;

if(index == (WORDS_PER_LINE-1))
{
string ref =

string("INSERT INTO BANKING(DATE,TRANS_TYPE,AMOUNT,BALANCE)
VALUES(TO_DATE(''") +
list[0] +
string("'',''MM/DD/YYYY''),''") +
list[1] +
string("'',") +
list[2] +
string(",") +
list[3]+
string(");");

cout << "processing .." << ref << endl;

list.clear();


Why do you want to clear it here? What''s the purpose of clearing?

}

counter++;
delete (str);
str = parser.readField();
}




这篇关于清除矢量列表后,我收到了分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆