0

Im bad with pointers and references so i have a question why in function hoffman the values are good and fine but when im trying see them out of function hoffman in main() i cant see almost none of this values. This is my code:

#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;

}

In function hoffman im assigning string code to every element of struct but i cant see it out of function using the last loop, any ideas?

4
  • 1
    Advice: learn about clangFormat. Commented Jun 1, 2020 at 19:08
  • Which values are "the values" and what does it mean to "see" them? Are you asking about why your debugger is behaving a particular way, or are you printing them out to stdout in order to inspect their contents, or what? Commented Jun 1, 2020 at 19:09
  • 1
    You need to tell us at least (i) what you expect to see, and (ii) what you actually do see. Commented Jun 1, 2020 at 19:11
  • imgur.com/a/nSGOaBm thats my example input and first output is from my function hoffman and the second one is from my last loop in main and i want to have as my output exactly the same from the function and from the last loop but in this case as u can see the loop gives nothing Commented Jun 1, 2020 at 19:16

2 Answers 2

1

This code is wrong

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

You are taking pointers to elements in your vector and pushing back items onto the vector. The problem with this is that if the vector reallocates all your pointers become invalid.

Simplest fix would be to use indexes instead of pointers and pass your vector to the hoffman routine.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh my god i see now, so my all pointers were valid. I reserved my vector with tab.reserve(1000); and it works now. Stupid solution but it is enough for now. Thanks a lot
1

The problem is you have undefined behaviour in your code (aka UB). When following loop is executed first time:

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

tab is empty, but you dereference its element with tab[rozm] which causes undefined behaviour. Everything after this place is an unknown land.

You can quickly find any such UBs in your code using sanitization, ie. with clang use parameter -fsanitize=undefined. Example:

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

output gives you exact place of error:

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

but its better to execute it in debugger to see full callstack, with variables lookup etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.