【发布时间】:2017-07-31 16:06:40
【问题描述】:
好的,两个问题。 首先
Q1. 我在运行时分配了足够的内存,但在连续两次运行案例 6 后,s1 打印垃圾,第三次运行案例 6 时的值s1消失了
the first image shows the garbage value at the end of string
[第二张图片显示字符串 1 已消失][2]
Q2.STCPY( COPY FUCTION) 无法正常工作,它显示segmentation fault,但我再次重新分配了足够的内存。(我需要将s2 复制到s1,所以我将L2(string 2's length)重新分配到s1,这样就不会浪费内存了)
代码如下:
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int STRLEN(char* S){
int i=0;
while(S[i] != '\0'){
i++;
}
return i;
}
int SUBSTR(char* str1, char* str2){
//string str1;
//string str2;
int L1=0,L2=0;
int i,j,flag=0;
int count=0;
/*cout<<"\nEnter main string greater characters(str1) : ";
cin>>str1;
cout<<"\nEnter phrase to find (str2) : ";
cin>>str2;
*/
L1 = STRLEN(str1); //calculate legth of strings
L2 = STRLEN(str2);
for(i=0;i<L1;i++){
count = 0;
flag = 0;
for(j=0;j<L2;j+=1){
if(str1[i]==str2[j])
{
//good
if(j==L2) //successful phrase's each character
traversed
{
break;
}
i++;
count++;
//j++;
}
else
{
break;
}
} //terminate inner for loops
{
i -= count; //i was incremented explicitly in
inner loop. for normal operation,
} // i has to be decremented by equal
no. of increments in inner loop
if(j==L2) //flag for successful traversing of phrase 2
in string 1
{
flag = 1;
break;
}
}
if(flag == 1)
{
cout<<"\nSUBTRING PRESENT AT "<<(i+1);
}
return 0;
}
int STREQL(char* str1, char* str2){
int flag = 0;
int L1=0,L2=0;
int i,j;
int count=0;
/* cout<<"\nEnter first word : ";
cin>>str1;
cout<<"\nEnter second word : ";
cin>>str2;*/
char* p1;
char* p2;
p1 = str1;
p2 = str2;
while(*(p1) != '\0')
{
if(*(p1) == *(p2))
{
//good
p1++;
p2++;
}
else{
flag = 1;
break;
}
}
if(flag == 1 )
{
cout<<"\n STRINGS ARE NOT EQUAL";
}
else{
cout<<"\n STRINGS ARE EQUAL";
}
return 0;
}
int STCPY(char* s1, char* s2){
int L2 = STRLEN(s2);
int L1 = STRLEN(s1);
cout<<"\nPASS 1";
char* s3 = (char*)realloc(s1,L2); //Note :- s3 and s1 point to
same memory location
cout<<"\nPASS 2";
while(s2 != '\0')
{
*s3 = *s2;
s3++;
s2++;
}
cout<<"\nPASS 3";
s3 -= L2;
s2 -= L2;
cout<<"\n STRING 1 = "<<s1;
cout<<"\n STRING 2 = "<<s2;
return 0;
}
int STRREV(){
return 0;
}
int STRLEN(char* str1, char* str2){
int L1=0,L2=0;
L1=STRLEN(str1);
L2=STRLEN(str2);
cout<<"\nSTRING 1 LENGTH = "<<L1;
cout<<"\nSTRING 2 LENGTH = "<<L2;
return 0;
}
int STRCAT(char* s1, char* s2)
{
int L1=0,L2=0;
int i,j;
L1=STRLEN(s1);
L2=STRLEN(s2);
//cout<<"\nSTRING 1 LENGTH = "<<L1;
//cout<<"\nSTRING 2 LENGTH = "<<L2;
char* s3 = (char*)realloc(s1,(L1+L2));
int cnt=0;
for(i=L1,s3 = s3+L1; *s2 != '\0'; i++)
{
*s3 = *s2;
s2++;
s3++;
cnt++;
}
s3 = s3-cnt-L1;
s2 = s2-cnt;
//cout<<"\nConcatenated string s3 = "<<s3;
//cout<<"\nConcatenated string s1 = "<<s1;
cout<<"\nSTRING 1 = "<<s1;
cout<<"\nSTRING 2 = "<<s2;
return 0;
}
int main(){
int i,j,choice;
char* s1;
char* s2;
cout<<"\nEnter string : ";
s1 = (char*)malloc(50);// ok i made this 50 bytes instead of 10
cin.getline(s1,50); // cause u guys arguing, but still that doesn't change the output
//cout<<s1;
cout<<"\nEnter string : ";
s2 = (char*)malloc(50);
cin.getline(s2,50);
//cout<<s2;
cout<<"\n------------MENU------------";
cout<<"\n1.SUBSTRING FIND";
cout<<"\n2.EQUAL CHECK";
cout<<"\n3.COPY STRING";
cout<<"\n4.REVERSE";
cout<<"\n5.STRING LENGTH";
cout<<"\n6.STRING CONCATENATION";
cout<<"\n7.EXIT";
cout<<"\n----------------------------";
do{
cout<<"\n\nEnter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
SUBSTR(s1, s2);
break;
case 2:
STREQL(s1, s2);
break;
case 3:
STCPY(s1, s2);
break;
case 4:
STRREV();
break;
case 5:
STRLEN(s1, s2);
break;
case 6:
STRCAT(s1, s2);
break;
case 7:
break;
}
}while(choice != 7);
return 0;
}
【问题讨论】:
-
分段错误 = 是时候打开该调试器并找出原因了。
-
使用调试器逐行执行代码时,您观察到了什么?
-
是否有理由重写
substr和strlen之类的基本原理?我希望这是一个学术练习。如果是这样,那么将其标记为 C++ 就很奇怪了,因为除了使用cout之外,这基本上是 C 代码。还值得一提的是,在 C/C++ 中,函数名称通常小写或大小写混合,其中ALL_CAPS按照惯例暗示您正在使用宏。 -
使用
std::string并将内存管理留给实现。正如你所看到的,当第一次学习语言时,这真的很容易搞砸。 -
首先你 malloc 10 个字符并尝试读取 50 个字符
标签: c++ string pointers segmentation-fault