AMT2K5写道:感谢您的提示。我尝试删除整个功能代码和留下大小++;但是我仍然有段故障,所以我不确定现在可能出现什么问题。 const char * Bank :: operator + =(const char a []) { size ++; 此函数的返回值类型为''const char *''。我没有看到任何 ''返回''声明... 这可能是也可能不是您的分段错误的原因。不可能 告诉我们没有看到如何使用这个功能。 } V Hello folks, I seem to have recieved a segfault within my function butam unsure on how to resolve it. I understand that it means thatsomewhere something is trying to access memory that is not prohibitedor not available.The following function is an overloaded += operator that reads instrings in a record format seperated by comma delimiters except for thelast data which terminates with a semicolon. I am adding these recordsto an array of objects which stores the record. I hope the folowingcode is enough to identify the problem. The static array''s are definedas 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 herereturn 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 usingchar 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 thesemicolon to some unsuspecting part of memory -- undefined behaviour.BTW, are you sure there is enough room in ''stringTemp'' to append thesemicolon? savings[size].changeCustomerInfo(stringTemp); size++; //Core dump here return a; }VThanks for the tips. I tried removing the entire function code andleaving size++; but I still have a seg fault, so I''m not sure what theproblem 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. Impossibleto tell without seeing how the function is used. }V 这篇关于分段错误 - 需要帮助解决{新手C ++程序员}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-27 12:24
查看更多