我如何最大程度地减少此类的内存​​泄漏/堆栈缓冲区溢出错误?

时间:2018-12-21 09:11:25

标签: c++

我试图解决Codeforce上的一个问题,我试图计算我必须删除的字母数,以便没有两个邻居使用相同的字母。

 #include<iostream>
 #include<string>
 #include<algorithm>


 int main(){

    int n,count=0,i=0,j=1;
    std::cin>>n;
    std::string s;
    std::cin>>s;
    if(n==1){
      std::cout<<count;
    }
    else {
      while(n--||s[j]){
        if(s[i]==s[j]){
          count++;
          j++;
        }
        else {
          i=j;
          j=j+1;
        }
      }
      std::cout<<count;
   }

   return 0;

}

我遇到运行时错误。 诊断检测到问题[cpp.clang ++-diagnose]:======================================= ========================

  

== 2064 ==错误:AddressSanitizer:PC 0x010a148f bp 0x1184f8f4 sp 0x1184f8f0地址0x1184f938上的堆栈缓冲区溢出读取大小为1   在0x1184f938线程T0处   == 2064 == AddressSanitizer检查失败:C:\ src \ llvm_package_600-final \ llvm \ projects \ compiler-rt \ lib \ sanitizer_common \ sanitizer_symbolizer_win.cc:64   “(((dbghelp &&”无法加载dbghelp.dll“))!=(0)”(0x0,0x0)   == 2064 == AddressSanitizer检查失败:C:\ src \ llvm_package_600-final \ llvm \ projects \ compiler-rt \ lib \ sanitizer_common \ sanitizer_win.cc:795   “((owner_))==((LOCK_READY))”(0xe14,0xffffffff)   == 2064 == AddressSanitizer检查失败:C:\ src \ llvm_package_600-final \ llvm \ projects \ compiler-rt \ lib \ sanitizer_common \ sanitizer_win.cc:795   “((owner_))==((LOCK_READY))”(0xe14,0xffffffff)   == 2064 == AddressSanitizer检查失败:C:\ src \ llvm_package_600-final \ llvm \ projects \ compiler-rt \ lib \ sanitizer_common \ sanitizer_win.cc:795   “((owner_))==((LOCK_READY))”(0xe14,0xffffffff)   == 2064 == AddressSanitizer检查失败:C:\ src \ llvm_package_600-final \ llvm \ projects \ compiler-rt \ lib \ sanitizer_common \ sanitizer_win.cc:795   “((owner_))==((LOCK_READY))”(0xe14,0xffffffff)   == 2064 == AddressSanitizer检查失败:C:\ src \ llvm_package_600-final \ llvm \ projects \ compiler-rt \ lib \ sanitizer_common \ sanitizer_win.cc:795   “((owner_))==((LOCK_READY))”(0xe14,0xffffffff)   == 2064 == AddressSanitizer检查失败:C:\ src \ llvm_package_600-final \ llvm \ projects \ compiler-rt \ lib \ sanitizer_common \ sanitizer_win.cc:795   “((owner_))==((LOCK_READY))”(0xe14,0xffffffff)   == 2064 == AddressSanitizer检查失败:C:\ src \ llvm_package_600-final \ llvm \ projects \ compiler-rt \ lib \ sanitizer_common \ sanitizer_win.cc:795   “((owner_))==((LOCK_READY))”(0xe14,0xffffffff)   == 2064 == AddressSanitizer检查失败:C:\ src \ llvm_package_600-final \ llvm \ projects \ compiler-rt \ lib \ sanitizer_common \ sanitizer_win.cc:795   “((owner_))==((LOCK_READY))”(0xe14,0xffffffff)   == 2064 == AddressSanitizer检查失败:C:\ src \ llvm_package_600-final \ llvm \ projects \ compiler-rt \ lib \ sanitizer_common \ sanitizer_win.cc:795   “((owner_))==((LOCK_READY))”(0xe14,0xffffffff)   == 2064 == AddressSanitizer检查失败:C:\ src \ llvm_package_600-final \ llvm \ projects \ compiler-rt \ lib \ sanitizer_common \ sanitizer_win.cc:795   “((owner_))==((LOCK_READY))”(0xe14,0xffffffff)

1 个答案:

答案 0 :(得分:1)

问题在于此行:

while(n--||s[j])

现在,如果n是字符串的长度,那么s[j]将导致超出范围的内存访问,因为jwhile中总是递增循环。

以简单的情况为例,当字符串为“ me”并且n2时。 s[2]不是合法访问权限。