2017-07-31 37 views
-1

好吧,有两个问题。 首先是分割错误和动态内存处理

Q1。我在运行时分配了足够的存储器,仍在运行的情况下6连续两次,将s1打印垃圾,并且在第三次的s1值消失

the first image shows the garbage value at the end of string

[运行壳体6之后第二张图显示字符串1已经消失] [2]

Q2。STCPY(COPY FUCTION)不能正常工作,其显示segmentation fault,但同样,我重新分配足够的存储空间。(我需要s2复制到s1,所以我重新分配L2(string 2's length)s1,这样就不会有内存浪费)

segmentation fault

下面的代码:

#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; 
} 
+3

分段错误=打开调试器并查明原因的时间 – tadman

+2

当您逐步执行你的代码是否与调试器一致? – user0042

+0

是否有任何理由重写'substr'和'strlen'这样的基础知识?我希望这是一个学术练习。如果是这样,这很奇怪,它被标记为C++因为除了使用'cout'之外,这基本上是C代码。值得一提的是,函数名称通常在C/C++中较低或混合大小写,其中'ALL_CAPS'是由conv提示您使用的是宏。 – tadman

回答

1

“PASS 2” 中取代一切登录到这一点:

while(*s2 != '\0') 
     { 
      *s3 = *s2; 
      s3++; 
      s2++; 
     } 

观察到s2!='\ 0'的意思是基本上:s2!= nullptr永远不是真的。这就是为什么你得到分段错误。

+0

上添加一个零字节,它仍然不起作用。仍然分段错误(核心转储) – officialnitish

+0

也没有任何意义,s3和s2是指针而不是常规变量 – officialnitish

+0

@officialnitish我测试了它并且工作。 ++操作符表示指针(current + sizeof(当前类型)),即指向下一个位置并重新解释它,就好像它是相同类型的(在我们的情况下)s2。 –

0

C字符串以最后的\ 0结尾。所以内存占用是strlen(str)+ 1个字节。 (s3 = realloc(s1,L1 + L2 +1),并且你忘记添加最后的\ 0(s​​3 [L1 + L2] = 0) 为您的STCPY功能,+ s2!= 0替换为* s2!= 0