繁体   English   中英

在 C++ 中保存来自 function 的值的问题

[英]Problem with saving values from function in C++

我对指针和引用不好,所以我有一个问题,为什么在 function hoffman 中的值很好,但是当我尝试从 function hoffman 在 main() 中看到它们时,我几乎看不到这些值。 这是我的代码:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

struct element {
char letter;
int number;
string code;
element *left;
element *right;
};

bool my_cmp(const element& a, const element& b)
{
  return a.number<b.number;
}



void hoffman(element &tab)
{
   if(tab.left!=NULL)
   {
    tab.left->code=tab.code;
    tab.left->code.push_back('0');
    if(tab.left->letter!=' ')
    {
        cout<<tab.left->letter<<" "<<tab.left->code<<endl;
    }
    hoffman(*tab.left);
}
if(tab.right!=NULL)
{
    tab.right->code=tab.code;
    tab.right->code.push_back('1');
    if(tab.right->letter!=' ')
    {
        cout<<tab.right->letter<<" "<<tab.right->code<<endl;
    }
    hoffman(*tab.right);
     }
    }

 int main()
 {
string C="abcdefghijklmnopqestuvwxyz",text;
int T[26]={0};
vector<element> tab;
cin>>text;
int rozmiar=text.length();
int rozm=0;
for(int i=0;i<rozmiar;i++)
{
    for(int j=0;j<26;j++)
    {
        if(text[i]==C[j])
        {
            T[j]++;
            break;
        }
    }
}
for(int j=0;j<26;j++)
{
    if(T[j]!=0)
    {
        tab.push_back({C[j],T[j],"",NULL,NULL});
    }
}

sort(tab.begin(),tab.end(),my_cmp);



while((rozm+1)!=tab.size())
{
    tab.push_back({' ',tab[rozm].number+tab[rozm+1].number,"",&tab[rozm],&tab[rozm+1]});
    rozm+=2;
    sort(tab.begin()+rozm,tab.end(),my_cmp);
}

hoffman(tab[tab.size()-1]);
cout<<endl;

for(int i=0;i<tab.size();i++)
{
    if(tab[i].letter!=' ') cout<<tab[i].letter<<" = "<<tab[i].code<<endl;
}

return 0;

}

在 function 霍夫曼中,我正在为结构的每个元素分配字符串代码,但我无法使用最后一个循环从 function 中看到它,有什么想法吗?

这段代码是错误的

tab.push_back({' ',tab[rozm].number+tab[rozm+1].number,"",&tab[rozm],&tab[rozm+1]});

您将指针指向向量中的元素并将项目推回向量上。 这样做的问题是,如果向量重新分配所有指针,您的指针就会变得无效。

最简单的解决方法是使用索引而不是指针并将向量传递给hoffman例程。

问题是您的代码(又名 UB)中有未定义的行为。 第一次执行以下循环时:

while ((rozm + 1) != tab.size()) {
    tab.push_back({' ', tab[rozm].number + tab[rozm + 1].number, "", &tab[rozm], &tab[rozm + 1]});

tab是空的,但是您使用tab[rozm]取消引用它的元素,这会导致未定义的行为。 此地之后的一切都是未知之地。

您可以使用清理快速在代码中找到任何此类 UB,即。 与 clang 一起使用参数-fsanitize=undefined 例子:

https://coliru.stacked-crooked.com/a/037fb17598ea2050

output 为您提供准确的错误位置:

clang++ -fsanitize=undefined -std=c++17 -O0 -g -Wall -pedantic -pthread main.cpp && ./a.out
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.5.0/../../../../include/c++/5.5.0/bits/stl_vector.h:780:16: runtime error: reference binding to null pointer of type 'element'
main.cpp:80:24: runtime error: member access within null pointer of type '__gnu_cxx::__alloc_traits<std::allocator<element> >::value_type' (aka 'element')
bash: line 7: 24391 Segmentation fault      (core dumped) ./a.out

但最好在调试器中执行它以查看完整的调用堆栈、变量查找等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM