分段错误 - 需要帮助解决{新手C ++程序员} [英] Segmentation Fault - Need assistance resolving {Novice C++ Programmer}

查看:59
本文介绍了分段错误 - 需要帮助解决{新手C ++程序员}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我似乎在我的功能中收到了一个段错误但是

不确定如何解决它。我明白这意味着

某处正试图访问未被禁止的内存

或不可用。


以下函数是一个重载的+ =运算符,它以逗号分隔符分隔的记录格式读取
字符串,但

最后一个以分号结尾的数据除外。我将这些记录

添加到存储记录的对象数组中。我希望下面的

代码足以识别问题。静态数组的定义是

,如我的作业所述。


const char * Bank :: operator + =(const char a [])< br $>
{


char stringTemp [316];

char accountTemp [16];

int balanceTemp ;

const char * ptr = a;


sscanf(ptr,"%15 [^,],%d,%315 [^;] ;",accountTemp,& balanceTemp,

stringTemp);

储蓄[size] .changeAccountNumber(accountTemp);

储蓄[size ] .changeBalance(balanceTemp);

strcat(stringTemp," ;;");

savings [size] .changeCustomerInfo(stringTemp);

size ++; //核心转储


返回;


}

解决方案

AMT2K5写道:

大家好,我似乎在我的功能中收到了一个段错误但是不确定如何解决它。我明白这意味着某些地方正试图访问未被禁止或无法使用的内存。

以下函数是一个重载的+ =运算符,它读入
以逗号分隔符分隔的记录格式的字符串,除了以分号结尾的最后一个数据。我将这些记录添加到存储记录的对象数组中。我希望下面的代码足以识别问题。静态数组的定义是在我的任务中说明的。

const char * Bank :: operator + =(const char a [])
{

char stringTemp [316];


确保你使用


char stringTemp [316] = {0};

char accountTemp [16];


在这里相同。

int balanceTemp;


这个还未初始化

const char * ptr = a;


这里没有必要,有吗?你为什么不直接把''a''传递到下面的'/ b $'''扫描片'?

sscanf(ptr,"%15 [^,] ,%d,%315 [^;];",accountTemp,& balanceTemp,
stringTemp);

储蓄[size] .changeAccountNumber(accountTemp);
储蓄[size] .changeBalance(balanceTemp);
strcat(stringTemp," ;;");


由于''stringTemp''没有初始化为0,它可能会将

分号连接到一些毫无防备的内存部分 - 未定义的行为。 br />

BTW,你确定'stringTemp'中有足够的空间来附加

分号吗?

储蓄[size ] .changeCustomerInfo(stringTemp);
size ++; //核心转储

返回;

}




V


感谢您的提示。我尝试删除整个功能代码和

留下大小++;但是我仍然有段故障,所以我不确定现在可能是什么问题。


const char * Bank :: operator + = (const char a [])

{

size ++;

}

AMT2K5写道:

感谢您的提示。我尝试删除整个功能代码和
留下大小++;但是我仍然有段故障,所以我不确定现在可能出现什么问题。

const char * Bank :: operator + =(const char a [])
{
size ++;


此函数的返回值类型为''const char *''。我没有看到任何

''返回''声明...


这可能是也可能不是您的分段错误的原因。不可能

告诉我们没有看到如何使用这个功能。

}




V


Hello folks, I seem to have recieved a segfault within my function but
am unsure on how to resolve it. I understand that it means that
somewhere something is trying to access memory that is not prohibited
or not available.

The following function is an overloaded += operator that reads in
strings in a record format seperated by comma delimiters except for the
last data which terminates with a semicolon. I am adding these records
to an array of objects which stores the record. I hope the folowing
code is enough to identify the problem. The static array''s are defined
as told in my assignment.

const char* Bank::operator+=(const char a[])
{

char stringTemp[316];
char accountTemp[16];
int balanceTemp;
const char *ptr = a;

sscanf(ptr, "%15[^,],%d,%315[^;];", accountTemp, &balanceTemp,
stringTemp);
savings[size].changeAccountNumber(accountTemp);
savings[size].changeBalance(balanceTemp);
strcat(stringTemp,";");
savings[size].changeCustomerInfo(stringTemp);
size++; //Core dump here

return a;

}

解决方案

AMT2K5 wrote:

Hello folks, I seem to have recieved a segfault within my function but
am unsure on how to resolve it. I understand that it means that
somewhere something is trying to access memory that is not prohibited
or not available.

The following function is an overloaded += operator that reads in
strings in a record format seperated by comma delimiters except for the
last data which terminates with a semicolon. I am adding these records
to an array of objects which stores the record. I hope the folowing
code is enough to identify the problem. The static array''s are defined
as told in my assignment.

const char* Bank::operator+=(const char a[])
{

char stringTemp[316];
Make sure you initiliase it using

char stringTemp[316] = {0};
char accountTemp[16];
Same here.
int balanceTemp;
This one is left uninitialised as well
const char *ptr = a;
There is no need in this, is there? Why don''t you just pass ''a'' to the
''scanf'' below?

sscanf(ptr, "%15[^,],%d,%315[^;];", accountTemp, &balanceTemp,
stringTemp);
savings[size].changeAccountNumber(accountTemp);
savings[size].changeBalance(balanceTemp);
strcat(stringTemp,";");
Since ''stringTemp'' wasn''t initialised to 0 it probably concatenates the
semicolon to some unsuspecting part of memory -- undefined behaviour.

BTW, are you sure there is enough room in ''stringTemp'' to append the
semicolon?
savings[size].changeCustomerInfo(stringTemp);
size++; //Core dump here

return a;

}



V


Thanks for the tips. I tried removing the entire function code and
leaving size++; but I still have a seg fault, so I''m not sure what the
problem may be now.

const char* Bank::operator+=(const char a[])
{
size++;
}


AMT2K5 wrote:

Thanks for the tips. I tried removing the entire function code and
leaving size++; but I still have a seg fault, so I''m not sure what the
problem may be now.

const char* Bank::operator+=(const char a[])
{
size++;
This function has the return value type of ''const char*''. I don''t see any
''return'' statement...

That may or may not be the cause of your segmentation fault. Impossible
to tell without seeing how the function is used.
}



V


这篇关于分段错误 - 需要帮助解决{新手C ++程序员}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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