==45==ERROR: AddressSanitizer: heap-use-after-free on address 0x610000000048 at pc 0x0000003ff511 bp 0x7ffd35c52530 sp 0x7ffd35c52528
READ of size 8 at 0x610000000048 thread T0
#4 0x7f14c1edb82f (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
0x610000000048 is located 8 bytes inside of 192-byte region [0x610000000040,0x610000000100)
freed by thread T0 here:
#4 0x7f14c1edb82f (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
previously allocated by thread T0 here:
#6 0x7f14c1edb82f (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
Shadow bytes around the buggy address:
0x0c207fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c207fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c207fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c207fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c207fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c207fff8000: fa fa fa fa fa fa fa fa fd[fd]fd fd fd fd fd fd
0x0c207fff8010: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
0x0c207fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c207fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c207fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c207fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
Shadow gap: cc
==45==ABORTING
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Codec {
public:
int finddepth(TreeNode* root){ //need to learn recurssion for finddepth
if(!root){
return 0;
}
else return max(1+finddepth(root->left),1+finddepth(root->right));
}
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
int maxdepth=finddepth(root);
int maxnumber=pow(2,maxdepth)-1; //find max element number
string str=string();
queue<TreeNode*> q;
q.push(root);
int visited=1;
//use BFS to put store element in str
while(q.size()>0){
if(q.front()==NULL){
str=str+"null,";
}
else{
str=str+to_string(q.front()->val)+",";
}
if (visited < maxnumber) {//check whether put in redundant element
if (q.front() != NULL) {
q.push(q.front()->left);
q.push(q.front()->right);
visited += 2;
}
else {
q.push(NULL);
q.push(NULL);
visited += 2;
}
q.pop();
}
cout<<str<<endl;
return str;
}
TreeNode get(string s){
if(s=="null"){
return NULL;
}
else {
int a=0;
stringstream x; //convert string to int
x<<s;
x>>a;
return TreeNode(a);
}
}
// Decodes your encoded data to tree.
vector<TreeNode> TN;
TreeNode* deserialize(string data) {
int lp=0,rp=0;
while(rp<data.size()){
rp=data.find(",",rp+1);
TN.push_back(get(data.substr(lp,rp-lp)) );
lp=rp+1;
}
int index=0;
int pchild=1;
while(pchild<TN.size()-1){
TN.at(index).left=&TN.at(pchild++);
TN.at(index).right=&TN.at(pchild++);
index++;
pchild++;
}
return &TN[0];
}
};
// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));