首页 > 解决方案 > coredump 用于字符串排列代码

问题描述

我正在尝试编写一些代码来检查两个字符串是否相互排列,但我看到错误提示存在 coredump,我试图打印输出但没有得到错误的线索,代码很简单,任何人都有任何线索?谢谢!

#include <iostream>
#include <vector>
using namespace std;


bool Permutation(string &A, string &B) {
    
    vector<int> a1(26);
    vector<int> b1(26);
    for (const char ch:A)
    {
        a1[ch-'a']++;
    }
    for (const char ch:B)
    {
        b1[ch-'b']++;
        
    }
  

    if(a1==b1)
   {
       return true;
    }else
   {
        return false;
   }
}
int main() {
  string a="abcd";
  string b="bcda";
  bool res=Permutation(a, b);
  if (res==true)
  {
   cout<<"permutation!"<<endl;
  }else
  {
  cout<<"not permutation!"<<endl;
  }
  return 0;
}

看到的错误:

==================================================== =================16==错误:AddressSanitizer:堆缓冲区溢出地址 0x60b0000000ec 在 pc 0x561be10aed68 bp 0x7ffcbc608970 sp 0x7ffcbc608960 在 0x60b0000560ec 线程 T0 #6701 处读取大小 4在排列(std::__cxx11::basic_string<char, std::char_traits, std::allocator >&, std::__cxx11::basic_string<char, std::char_traits, std::allocator >&) /home/ coderpad/solution.cpp:16 #1 0x561be10af0f8 in main /home/coderpad/solution.cpp:31 #2 0x7f7785647b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96) #3 0x561be0fa2e09 in _start (/home/coderpad/解决方案+0x8e09)

0x60b0000000ec 位于此处由线程 T0 分配的 104 字节区域 [0x60b0000000f0,0x60b000000158) 左侧 4 个字节:#0 0x561be1071220 in operator new(unsigned long) (/home/coderpad/solution+0xd7220) #1 0x561be10b179c in_c_ :new_allocator::allocate(unsigned long, void const*) /usr/include/c++/8/ext/new_allocator.h:111 #2 0x561be10b15ae in std::allocator_traitsstd::allocator<int >::allocate(std:: allocator&, unsigned long) /usr/include/c++/8/bits/alloc_traits.h:436 #3 0x561be10b128d in std::_Vector_base<int, std::allocator >::_M_allocate(unsigned long) /usr/include/c++ /8/bits/stl_vector.h:296 #4 0x561be10b0b46 in std::_Vector_base<int, std::allocator >::_M_create_storage(unsigned long) /usr/include/c++/8/bits/stl_vector.h:311 # 5 0x561be10aff2e 在 std::_Vector_base<int, std::allocator >::_Vector_base(unsigned long, std::allocator const&) /usr/include/c++/8/bits/stl_vector.h:260 #6 0x561be10af703 in std::vector<int, std::allocator >::vector(unsigned long, std::allocator const&) /usr/include/c++/8/bits/stl_vector.h:416 #7 0x561be10aea27 in Permutation(std::__cxx11::basic_string<char, std::char_traits, std::allocator >&, std::__cxx11::basic_string<char, std::char_traits, std::allocator >&) /home/coderpad/solution.cpp:9 #8 0x561be10af0f8 in main /home/coderpad/solution.cpp:31 #9 0x7f7785647b96在 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)416 #7 0x561be10aea27 in Permutation(std::__cxx11::basic_string<char, std::char_traits, std::allocator >&, std::__cxx11::basic_string<char, std::char_traits, std::allocator >& ) /home/coderpad/solution.cpp:9 #8 0x561be10af0f8 in main /home/coderpad/solution.cpp:31 #9 0x7f7785647b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)416 #7 0x561be10aea27 in Permutation(std::__cxx11::basic_string<char, std::char_traits, std::allocator >&, std::__cxx11::basic_string<char, std::char_traits, std::allocator >& ) /home/coderpad/solution.cpp:9 #8 0x561be10af0f8 in main /home/coderpad/solution.cpp:31 #9 0x7f7785647b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)

fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa ==16==ABORTING /usr/local/bin/startshell: line 49: 16 Aborted (core dumped) ./solution 检测到核心转储,启动 GDB?[是/否]

标签: c++algorithmvectorpermutation

解决方案


推荐阅读