How to solve this ==31==error Address sanitizer heap buffer overflow error? - hashtable
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
char memo[10];
int count[500]={0,};
typedef struct Trie{
int count;
struct Trie * next[26];
}Trie;
void Initnode(struct Trie *node){
node->count=0;
for(int i=0;i<26;i++){
node->next[i]=NULL;
}
}
void fixtrie(struct Trie *root, char *wordsitem){
int pw=0;
struct Trie* cur=root;
while(wordsitem[pw]!='\0'){
if(cur->next[wordsitem[pw]-'a']==NULL){
cur->next[wordsitem[pw]-'a']=malloc(sizeof(struct Trie));
Initnode(cur->next[wordsitem[pw]-'a']);
cur=cur->next[wordsitem[pw]-'a'];
pw++;
}
else{
cur=cur->next[wordsitem[pw]-'a'];
pw++;
}
}
(cur->count)++;
}
void dfs(struct Trie *root, char**result, int k,int pm){
if(root->count>0){
for(int i=0;i<k;i++){
if(root->count >count[i]){
for(int j=k-1;j>i;j--){
for(int c=0;c<10;c++) result[j][c]=result[j-1][c];
count[j]=count[j-1];
}
count[i]=root->count;
for(int j=0;j<10;j++) result[i][j]=0;
for(int j=0;j<pm;j++){
result[i][j]=memo[j];
}
printf("// %s //",result[i]);
break;
}
}
}
printf(" %d ",pm);
for(int i=0;i<26;i++){
if(root->next[i]!=NULL){
printf(" %d ",pm);
memo[pm]= i+'a';
printf("%s",memo);
dfs(root->next[i],result,k,pm+1);
}
}
return;
}
char ** topKFrequent(char ** words, int wordsSize, int k, int* returnSize){
for(int j=0;j<500;j++){
count[j]=0;
}
for(int j=0;j<10;j++){
memo[j]=0;
}
*returnSize=k;
struct Trie* root=malloc(sizeof(struct Trie)*1);
Initnode(root);
for(int i=0;i<wordsSize;i++){
fixtrie(root,words[i]);
}
for(int i=0;i<26;i++){
if(root->next[i]!=NULL){
}
}
char**result=(char**) malloc (sizeof(char*)*k);
for(int i=0;i<k;i++){
result[i]=malloc(sizeof(char)*10);
}
dfs(root,result,k,0);
printf("happy");
for(int i=0;i<k;i++){
printf("\n %s",result[i]);
}
printf("\n");
return result;
}
This problem is 692.problem of Leetcode.
I detect when words[I].length equals to 10, error occurs. but I don't know how to fix this code.
Input which occurs error is that ["glarko","zlfiwwb","nsfspyox","pwqvwmlgri","qggx","qrkgmliewc","zskaqzwo","zskaqzwo","ijy","htpvnmozay","jqrlad","ccjel","qrkgmliewc","qkjzgws","fqizrrnmif","jqrlad","nbuorw","qrkgmliewc","htpvnmozay","nftk","glarko","hdemkfr","axyak","hdemkfr","nsfspyox","nsfspyox","qrkgmliewc","nftk","nftk","ccjel","qrkgmliewc","ocgjsu","ijy","glarko","nbuorw","nsfspyox","qkjzgws","qkjzgws","fqizrrnmif","pwqvwmlgri","nftk","qrkgmliewc","jqrlad","nftk","zskaqzwo","glarko","nsfspyox","zlfiwwb","hwlvqgkdbo","htpvnmozay","nsfspyox","zskaqzwo","htpvnmozay","zskaqzwo","nbuorw","qkjzgws","zlfiwwb","pwqvwmlgri","zskaqzwo","qengse","glarko","qkjzgws","pwqvwmlgri","fqizrrnmif","nbuorw","nftk","ijy","hdemkfr","nftk","qkjzgws","jqrlad","nftk","ccjel","qggx","ijy","qengse","nftk","htpvnmozay","qengse","eonrg","qengse","fqizrrnmif","hwlvqgkdbo","qengse","qengse","qggx","qkjzgws","qggx","pwqvwmlgri","htpvnmozay","qrkgmliewc","qengse","fqizrrnmif","qkjzgws","qengse","nftk","htpvnmozay","qggx","zlfiwwb","bwp","ocgjsu","qrkgmliewc","ccjel","hdemkfr","nsfspyox","hdemkfr","qggx","zlfiwwb","nsfspyox","ijy","qkjzgws","fqizrrnmif","qkjzgws","qrkgmliewc","glarko","hdemkfr","pwqvwmlgri"]
14
I expect well answer.
But it occurs
enter image description here
How to fix my code?
Related
the count of subsequence of aray where arr[i]-arr[i-1] is constant for the array
we are given an array and its size and a interger k we need to find all/count of all subsequence that has constant arr[i]-arr[i-1] diff for all element and it size should be k i have tryed my approach and it is clear to understand also but it just printing count i.e cnt as 0 why any one #include<bits/stdc++.h> #include using namespace std; void subseq(vector<int>&arr,int idx,int n,int k,int& cnt,vector<int>&nums){ if(idx==n-1){ if(nums.size()==k){ bool flag=1; int dist=nums[1]-nums[0]; for(int i=2;i<n;i++){ if(nums[i]-nums[i-1]==dist){ continue; }else{ flag=0; } } if(flag) cnt+=1; } return; } nums.push_back(arr[idx]); subseq(arr,idx+1,n,k,cnt,nums); nums.pop_back(); subseq(arr,idx+1,n,k,cnt,nums); } int main(){ int t; cin>>t; while(t--){ int n,m; cin>>n>>m; vector<int>v(n); // cout<<1<<endl; for(int i=0;i<n;i++) cin>>v[i]; vector<int>x; int cnt=0; // cout<<1<<endl; subseq(v,0,n,m,cnt,x); cout<<cnt<<endl; } return 0; }
C string capitalize problem. I encounter bus error
I am making capitalized function. But when execute this code, the bus error occur. Is there anyone can debug this? I really need your help!! please help me . I am computer programming novice without c knowledge. #include <stdio.h> void up(char *c) { if((*c < ‘z’) && (*c > ‘a’)) { *c -= 32; } } char *ft_strcapitalize(char *str) { int i; i=0; while (str[i]) { up(&str[i]); i++; } return str; } int main() { char *str = “salut, comment tu vas ? 42mots quarante-deux cinquante”; ft_strcapitalize(str); printf(“%s”, str); }
This solution should work for you. #include <stdio.h> void up(char *c) { if (!c) return; if((*c <= 'z') && (*c >= 'a')) *c -= 32; } char *ft_strcapitalize(char *str) { int i; for (i = 0; str[i]; i++) up(&str[i]); return str; } int main() { char str[] = "salut, comment tu vas ? 42mots quarante-deux cinquante"; ft_strcapitalize(str); printf("%s\n", str); return 0; }
I am trying to print Reverse of a Sequence of Numbers using Stack. Stack is implemented using Vector. But I am getting Segmentation Fault
Can you please help me find the error in printing the reverse of sequence using stacks implemented by vector? I am getting a Segmenattion fault #include <iostream> #include<vector> using namespace std; class stack{ public : int top; vector<int> data; bool isempty(){return top == -1;} void push(int x){data[++top] = x;} void pop(){--top;} int topper(){return data[top];} }; int main() { stack s; int n; s.top = -1; cout << "enter the number of integers" << endl; cin >> n; for(int i =0; i < n; i ++){ s.push(i); } while(!s.isempty()){ cout << s.topper(); s.pop(); } return 0; }
This problem occurs, because a vector has size = 0 by default. You can either resize the vector, before you add values into it like so: #include <iostream> #include<vector> using namespace std; class stack { public: int top; vector<int> data; bool isempty() { return top == -1; } void push(int x) { data.resize(++top+1); data[top] = x; } void pop() { --top; } int topper() { return data[top]; } }; int main() { stack s; int n; s.top = -1; cout << "enter the number of integers" << endl; cin >> n; for (int i = 0; i < n; i++) { s.push(i); } while (!s.isempty()) { cout << s.topper(); s.pop(); } return 0; } Or you can use the built-in functionality for vectors like that, which I think is the far better solution: #include <iostream> #include<vector> using namespace std; class stack { public: vector<int> data; bool isempty() { return data.size() == 0; } void push(int x) { data.push_back(x); } void pop() { data.pop_back(); } int topper() { return data.back(); } }; int main() { stack s = stack(); int n; cout << "enter the number of integers" << endl; cin >> n; for (int i = 0; i < n; i++) { s.push(i); } while (!s.isempty()) { cout << s.topper(); s.pop(); } return 0; }
Segmentation fault inside range
#include <iostream> #include <vector> #include <algorithm> #include <queue> // std::priority_queue using std::vector; using std::cin; using std::cout; struct fj{ int indexI=0; int freeT=0; }; struct DereferenceCompareNode : public std::binary_function<fj, fj, bool> { bool operator()(const fj lhs, const fj rhs) const { return lhs.freeT > rhs.freeT; } }; class JobQueue { private: int num_workers_; vector<int> jobs_; vector<int> assigned_workers_; vector<long long> start_times_; void WriteResponse() const { for (int i = 0; i < jobs_.size(); ++i) { cout << assigned_workers_[i] << " " << start_times_[i] << "\n"; } } void ReadData() { int m; cin >> num_workers_ >> m; jobs_.resize(m); std::cout<<"Read fault"<<"\n"; for(int i = 0; i < m; i++) cin >> jobs_[i]; std::cout<<"Read fault ends"<<"\n"; } void AssignJobs() { // TODO: replace this code with a faster algorithm. std::cout<<"Fault point 1"<<"\n"; assigned_workers_.resize(jobs_.size()); start_times_.resize(jobs_.size()); vector<long long> next_free_time(num_workers_, 0); std::priority_queue<int, vector<int>, std::greater<int> > thread; std::priority_queue<fj, vector<fj>, DereferenceCompareNode > freeJob; /* for (int i = 0; i < jobs_.size(); ++i) { int duration = jobs_[i]; int next_worker = 0; for (int j = 0; j < num_workers_; ++j) { if (next_free_time[j] < next_free_time[next_worker]) next_worker = j; } assigned_workers_[i] = next_worker; start_times_[i] = next_free_time[next_worker]; next_free_time[next_worker] += duration; } */ std::cout<<"dump point 2"<<"\n"; for(int i=0;i<num_workers_;i++){ thread.push(i); } std::cout<<"dump point 1"<<"\n"; int counter = 0; while(jobs_.size()!=0){ std::cout<<"jobs_.size:"<<jobs_.size()<<"\n"; std::cout<<"freeJob.size:"<<freeJob.size()<<"\n"; //check logic do{ if(freeJob.top().freeT == counter){ std::cout<<"freeJob.top().freeT:"<<freeJob.top().freeT<<"\n"; std::cout<<"counter:"<<counter<<"\n"; thread.push(freeJob.top().indexI); freeJob.pop(); }else{ break; } } while(freeJob.size()!=0); std::cout<<"Thread:"<<thread.size()<<"\n"; while(thread.size()!=0){ if(jobs_.size()!=0){ fj currA; currA.indexI = thread.top(); currA.freeT = jobs_.at(0)+counter; std::cout<<"currA.indexI:"<<currA.indexI<<"\n"; std::cout<<"currA.freeT:"<<currA.freeT<<"\n"; thread.pop(); jobs_.erase(jobs_.begin()); assigned_workers_.push_back(currA.indexI); start_times_.push_back(currA.freeT); }else{ break; } } counter++; } } public: void Solve() { ReadData(); AssignJobs(); WriteResponse(); } }; int main() { std::ios_base::sync_with_stdio(false); JobQueue job_queue; job_queue.Solve(); return 0; } I am getting segmentation fault in function ReadData while taking inputs for vector jobs. I am getting fault even when I am inside bounds of defined size. Everything was fine when have not written AssignJob function. Am I doing something wrong with some bounds or taking illegal inputs format or messing with some other stuff?
Am I doing something wrong Yes, you are: freeJob starts out empty, so this is undefined behavior: if(freeJob.top().freeT == counter){ In fact, you never push anything into freeJob, you only pop() things from it.
reason for runtime error in my c program &?
#include<stdio.h> main() {char *names[4]; int i,a; printf("ënter the guests names\n"); for(i=0;i<=3;i++) { scanf("%s",names[i]); } char *yourname; printf("\nenter your name "); scanf("%c",yourname); for(i=0;i<=3;i++) {a=strcmp(names[i],yourname); if(a==0) printf("\nwelcome"); break; } if(a!=0) printf("\naccess denied"); return 0; } this is a program to check your entry in a show. first we give permitted names & then it asks your name ,it compares your name with the names in the guest list. i m getting runtime error, plz tell me the correction.i want to use pointers to string so plz suggest correction in the existing program when i run this program in devc++ after entering first name it gives program.exe stopped working.
The code will be like this: #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char *names[4]; int i,a; printf("ënter the guests names\n"); for(i=0;i<=3;i++) { names[i] = (char*) malloc(100 * sizeof(char)); scanf("%s", names[i]); } char yourname[100]; printf("\nenter your name "); scanf("%s",yourname); for(i=0;i<=3;i++) { a = strcmp(names[i], yourname); if (a == 0) break; } if (a==0) printf("\nwelcome"); else printf("\naccess denied"); for(i=0;i<=3;i++) free(names[i]); return 0; }
Your code have to be formated so we can give you a better answer. Now, use gets to take your input, verify the guest match with your name inside the for loop, stop the loop when a match is found. #include<stdio.h> #include<string.h> int main() { char names[4][20]; int i = 0; int a = 0; printf("Enter guests names: \n"); for(i=0; i<3; i++) { gets(names[i]); } char yourname[20]; printf("\n Enter your name "); gets(yourname); printf("\n Verify access right:"); for(i=0; i<3; i++) { a=strcmp(names[i], yourname); if(a==0) { printf("\n welcome"); break; } else { printf("\n access denied"); } } return 0; } Although this looks like a homework assignment.