Anonymous
Hi, what's the best way to embed python module or at least some code into c++ code? Unfortunately the python codebase is simply too big and rewriting it would be an overkill.
https://docs.python.org/3/extending/embedding.html I have not done it but I guess official documentation is what you are looking for. Alternatively you can experiment with Boost.Python
Anonymous
To print string
what stops you from just doing std::cout << ch?
Official hooligan of Pius XII
Official hooligan of Pius XII
Boost.Python?
kay, i'll look at it
Official hooligan of Pius XII
also i'd like to use it in a c++ qt app if it makes any difference
Manav
But why loop is not working
Anonymous
also i'd like to use it in a c++ qt app if it makes any difference
I have no idea as I havent ever tried doing that.
Anonymous
But why loop is not working
It is not working because your for loop is: for(int i=0; i != 0; ++i) Since i!=0 condition fails before the loop body is executed, nothing is printed. If you must use a loop, you can do: char* p = ch; while(*p) cout << *p++;
Official hooligan of Pius XII
I have no idea as I havent ever tried doing that.
Fair. I'll compare Python/C API with Boost and if they will be more problematic than helpful then I'll write everything in python. Thanks for your support! :)
Anonymous
Fair. I'll compare Python/C API with Boost and if they will be more problematic than helpful then I'll write everything in python. Thanks for your support! :)
look at this too https://cffi.readthedocs.io/en/latest/index.html https://cffi.readthedocs.io/en/latest/overview.html#embedding
Anonymous
Pls why does this code say Segmentation fault?? #include <stdio.h> int main(char *dest, char *src) { int index; int len1 = 0, len2 = 0; dest = "osei"; src = "richard"; while(*dest++) len1++; while(*src++) len2++; for (index = 0; index<=len2; index++) { dest[len1 + index] = src[index]; } printf("%s", dest); }
Naggafin
hello everyoneeee
Naggafin
I think too you're changing dest and src w/ your ++ arg so by the time they get referenced again it's beginning at the terminator rather than the start of your string
Shreyas
Thank U
Shreyas
Can I get the link to download C/C++ software
Diego
Can I get the link to download C/C++ software
If you're using Telegram Desktop, you're already using C/C++ software
Shreyas
No
ELAF🌻
Anonymous
String & operator= (construction string &other) what's meaning of this method?
Anonymous
*Const string &other
Anonymous
if i declare vector<vector<int>> ans; and then use resize(), it doesn't work if i assign a default size: vector<vector<int>> ans(n); and then use resize() it works Why?
Anonymous
if i declare vector<vector<int>> ans; and then use resize(), it doesn't work if i assign a default size: vector<vector<int>> ans(n); and then use resize() it works Why?
When you say something works or doesn't work, tell us what you did to check that it doesn't work. Learn how to ask questions. Usually showing code where you do the checking will suffice. We don't have crystal balls here to predict what you did or want to do
Anonymous
String & operator= (construction string &other) what's meaning of this method?
It is the assignment operator. Used when you assign one string to another
Anonymous
When you say something works or doesn't work, tell us what you did to check that it doesn't work. Learn how to ask questions. Usually showing code where you do the checking will suffice. We don't have crystal balls here to predict what you did or want to do
wrote this code for pascal's triangle: vector<vector<int>> generate(int numRows) { vector<vector<int>> ans(numRows); for(int i=0; i<numRows; i++){ ans[i].resize(i+1); // first and last values are always 1 ans[i][0] = 1; ans[i][i] = 1; for(int j=1; j<i; j++){ ans[i][j] = ans[i-1][j-1] + ans[i-1][j]; } } return ans; } It works But when I do vector<vector<int>> ans; and try to resize it, it doesn't
Anonymous
wrote this code for pascal's triangle: vector<vector<int>> generate(int numRows) { vector<vector<int>> ans(numRows); for(int i=0; i<numRows; i++){ ans[i].resize(i+1); // first and last values are always 1 ans[i][0] = 1; ans[i][i] = 1; for(int j=1; j<i; j++){ ans[i][j] = ans[i-1][j-1] + ans[i-1][j]; } } return ans; } It works But when I do vector<vector<int>> ans; and try to resize it, it doesn't
See. This makes it clear that you are doing a resize on the inner vector and not on the outer vector. There is no way we could have predicted this from your original question which was idiotic to say the least. In the case where you do vector<vector<int>> ans, the inner vectors don't exist yet. So you can't use index operator to access these vectors and resize them. To make it work, you can do something like vector<vector<int>> ans; for (int I=0; I<numRows;++I){ ans.push_back({}); ans[ans.size()-1].resize(I+1) .... }
Anonymous
I need a channel in YouTube it, s a best
They only cover syntax unfortunately which is why they are so short they don’t cover memory management and pointers in detail.
Anshul
#include<iostream> #include<queue> using namespace std; class Node { public: int data; Node *left; Node *right; Node(int d) { data=d; } }; Node* insert(Node *root,int val) { if(root==NULL) { root=new Node(val); return root; } if(val<=root->data) { root->left=insert(root->left,val); return root; } else if(val>=root->data) { root->right=insert(root->right,val); return root; } } Node* build() { int d; cin>>d; Node *root=NULL; while(d!=-1) { root=insert(root,d); cin>>d; } return root; } Node* searchInBST(Node *root,int key) { if(root==NULL) { return NULL; } if(root->data==key) { return root; } if(key<root->data) { return searchInBST(root->left,key); } return searchInBST(root->right,key); } void bfs(Node *root) { if(root==NULL) { return; } queue<Node*> q; q.push(root); q.push(NULL); while(!q.empty()) { Node *f=q.front(); if(f==NULL) { cout<<endl; q.pop(); if(!q.empty()) { q.push(NULL); } } else { q.pop(); cout<<f->data<<" "; if(f->left) { q.push(f->left); } if(f->right) { q.push(f->right); } } } } void inorder(Node *root) { if(root==NULL) { return; } inorder(root->left); cout<<root->data<<" "; inorder(root->right); } int main() { Node *root=NULL; root=build(); bfs(root); cout<<endl; inorder(root); cout<<endl; cout<<searchInBST(root,6)->data; return 0; }
Anshul
this piece of code runs well on online ide but not on my visual Studio Code
Anshul
it doesn't even give any errors
Pavel
this piece of code runs well on online ide but not on my visual Studio Code
When you say it doesn't run, you mean it crashes or gives a wrong answer?
Pavel
#include<iostream> #include<queue> using namespace std; class Node { public: int data; Node *left; Node *right; Node(int d) { data=d; } }; Node* insert(Node *root,int val) { if(root==NULL) { root=new Node(val); return root; } if(val<=root->data) { root->left=insert(root->left,val); return root; } else if(val>=root->data) { root->right=insert(root->right,val); return root; } } Node* build() { int d; cin>>d; Node *root=NULL; while(d!=-1) { root=insert(root,d); cin>>d; } return root; } Node* searchInBST(Node *root,int key) { if(root==NULL) { return NULL; } if(root->data==key) { return root; } if(key<root->data) { return searchInBST(root->left,key); } return searchInBST(root->right,key); } void bfs(Node *root) { if(root==NULL) { return; } queue<Node*> q; q.push(root); q.push(NULL); while(!q.empty()) { Node *f=q.front(); if(f==NULL) { cout<<endl; q.pop(); if(!q.empty()) { q.push(NULL); } } else { q.pop(); cout<<f->data<<" "; if(f->left) { q.push(f->left); } if(f->right) { q.push(f->right); } } } } void inorder(Node *root) { if(root==NULL) { return; } inorder(root->left); cout<<root->data<<" "; inorder(root->right); } int main() { Node *root=NULL; root=build(); bfs(root); cout<<endl; inorder(root); cout<<endl; cout<<searchInBST(root,6)->data; return 0; }
In your insert function you don't return anything if you find a node with the same value as val. This can result in UB.
Anshul
When you say it doesn't run, you mean it crashes or gives a wrong answer?
It does nothing. I don't know what is the meaning of crashes. But it just accept the input and when I press enter. The execution is done. Nothing Output on screen
Anshul
In your insert function you don't return anything if you find a node with the same value as val. This can result in UB.
That shouldn't be a problem because even if the value is equal to root->data I make a call to insert in the tree rooted at root->left
.:**:.*.:。.✿KK✿.。.:* :**:.
/stat
Anonymous
#include<iostream> #include<queue> using namespace std; class Node { public: int data; Node *left; Node *right; Node(int d) { data=d; } }; Node* insert(Node *root,int val) { if(root==NULL) { root=new Node(val); return root; } if(val<=root->data) { root->left=insert(root->left,val); return root; } else if(val>=root->data) { root->right=insert(root->right,val); return root; } } Node* build() { int d; cin>>d; Node *root=NULL; while(d!=-1) { root=insert(root,d); cin>>d; } return root; } Node* searchInBST(Node *root,int key) { if(root==NULL) { return NULL; } if(root->data==key) { return root; } if(key<root->data) { return searchInBST(root->left,key); } return searchInBST(root->right,key); } void bfs(Node *root) { if(root==NULL) { return; } queue<Node*> q; q.push(root); q.push(NULL); while(!q.empty()) { Node *f=q.front(); if(f==NULL) { cout<<endl; q.pop(); if(!q.empty()) { q.push(NULL); } } else { q.pop(); cout<<f->data<<" "; if(f->left) { q.push(f->left); } if(f->right) { q.push(f->right); } } } } void inorder(Node *root) { if(root==NULL) { return; } inorder(root->left); cout<<root->data<<" "; inorder(root->right); } int main() { Node *root=NULL; root=build(); bfs(root); cout<<endl; inorder(root); cout<<endl; cout<<searchInBST(root,6)->data; return 0; }
You are assuming the node you are seraching for will always be present. If it is not present then the line cout << searchInBST(root, <value>)->data; will throw an exception. Otherwise the code should run fine based on a cursory glance. And learn to use a debugger. This is probably the 3rd time I am telling you this. Without knowing how to use a debugger, you cant go much further
Anshul
But then what can be the reason due to which it's running on online ide and not on visual studio code
Anshul
Oh okay okay thanks
I tried to remove this problem but still it does nothing
Anonymous
But then what can be the reason due to which it's running on online ide and not on visual studio code
How would I know? There could be some other errors in your code as well. A debugger can help you find it. Probably there is some place where you are referencing memory that you are not supposed to and the online IDE run time environment slided past it while visual studio ran into problems because of this.
Anonymous
Yea you have told me previously to use a debugger. I'll try to use it from now on. Actually I thought it's not a good practice to use debugger to find errors.
Whoever gave you that idea is wrong. Debuggers are one of the most important tools in a software developer's toolbox
Anonymous
Ok thanks.
Moreover your logic for insert and searchInBST is contradictory
Anshul
how ?
Anshul
and also please can you share some good link to how to use a debugger?
Anonymous
how ?
In insert if a key is equal to root's key, you insert it in the left sub tree. In searchInBST, you search for it in the right sub tree. This doesnt matter in your case where key is the only data stored. But if suppose there was some other data also stored in the nodes, you will never be able to reach those nodes
Anshul
if key is equal to root's data then i don't go anywhere i directly return root, otherwise depending on the condition i go in left or right sub tree
Anonymous
#include<iostream> #include<queue> using namespace std; class Node { public: int data; Node *left; Node *right; Node(int d) { data=d; } }; Node* insert(Node *root,int val) { if(root==NULL) { root=new Node(val); return root; } if(val<=root->data) { root->left=insert(root->left,val); return root; } else if(val>=root->data) { root->right=insert(root->right,val); return root; } } Node* build() { int d; cin>>d; Node *root=NULL; while(d!=-1) { root=insert(root,d); cin>>d; } return root; } Node* searchInBST(Node *root,int key) { if(root==NULL) { return NULL; } if(root->data==key) { return root; } if(key<root->data) { return searchInBST(root->left,key); } return searchInBST(root->right,key); } void bfs(Node *root) { if(root==NULL) { return; } queue<Node*> q; q.push(root); q.push(NULL); while(!q.empty()) { Node *f=q.front(); if(f==NULL) { cout<<endl; q.pop(); if(!q.empty()) { q.push(NULL); } } else { q.pop(); cout<<f->data<<" "; if(f->left) { q.push(f->left); } if(f->right) { q.push(f->right); } } } } void inorder(Node *root) { if(root==NULL) { return; } inorder(root->left); cout<<root->data<<" "; inorder(root->right); } int main() { Node *root=NULL; root=build(); bfs(root); cout<<endl; inorder(root); cout<<endl; cout<<searchInBST(root,6)->data; return 0; }
In Insert if(val<=root->data) { root->left=insert(root->left,val); return root; } If val == root->data, you will insert this val again in the left tree. If you are not going to ever search for this node, what is the point of inserting it in the tree? This insert will matter in cases where your nodes may have the same key but have some other data where these nodes may differ. Like I said earlier, it doesnt matter in your simple example. In searchInBST if(key<root->data) { return searchInBST(root->left,key); } return searchInBST(root->right,key); Here, if suppose searching were based on both key and other data, then your if condition should be if (key <= root->data)
Anshul
right?
Anshul
Whoever gave you that idea is wrong. Debuggers are one of the most important tools in a software developer's toolbox
I tried to debug it and it says "paused on exception" but I really can't understand how to use debugger and can't find any good video either. Can you help please
Anshul
Can you help me with how to use a debugger
Anonymous
right?
yes. I use GDB mostly. If you are using Visual Studio, search for its documentation or go to Youtube and search for tutorial videos
Anshul
Exception thrown at 0x004014AD in buildBST.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00422FFC). how to know which line of code it is talking about
Anonymous
Exception thrown at 0x004014AD in buildBST.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00422FFC). how to know which line of code it is talking about
Set a break point in your code and step through line by line using a debugger. On GDB, I can usually get a backtrace to see where the exception was thrown and read the data at that point of time to see what the problem was. I can also analyze the core file. Visual Studio should also have support for it (not the core file though)
Anshul
use a sanitiser
I don't know that either 😅
Anonymous
I don't know that either 😅
add -fsanitze=address to the compilation command (works on gcc and clang. check documentation for MSVC, you might also need to install the component separately). run the program as before. it will give colourful details of the stack overflow.
Anonymous
Do you mean in the terminal add this command?
terminal or whatever build system you are using. gcc a.c becomes gcc -fsanitize=address a.c
Anonymous
https://devblogs.microsoft.com/cppblog/address-sanitizer-for-msvc-now-generally-available/
Anshul
Okay
Anshul
I'll go through these terms now
Anonymous
I'll go through these terms now
You have a whole lot of sanitizers provided by build tools like Static Analyzer, AddressSanitizer, ThreadSanitizer, MemorySanitizer, UndefinedBehaviorSanitizer, DataFlowSanitizer, LeakSanitizer and some more