Ольга
\Device\NUL
It's like?
scanf(); getchar(); You don't need to enter twice because the getchar() got newline that remained from previous input
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
Hi guys, so I have this array which has got N entries. Now, I have also got N threads. In this software every thread gets the chance to write on the array. The problem here is: synchronization. Now, since there is no reader in this software, I was asking myself: do I really need to use semaphores? After studying on my book and searching on the Internet, I've come up with two hypotesis: 1) Yes, because even though a thread can only access its entry in the array (that is, thread 1 can only access entry 1 and never entry 2), there's always the possibility of two threads simultaneously writing on the array, therefore causing non-atomic operations at machine-code level; 2) Everything I said is not right, threfore I do not need semaphores. Note: my semaphore would be something like this: int sem1= semget(IPC_PRIVATE, N, IPC_CREAT); where, again, N is the number of threads. Any suggestions? Thank you!
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
Nevermind, I understood. If you are interested: The sense here is to tell the parent process "we are done". In fact, the semaphore can either be: an N-entry semaphore, and every thread brings its "light" to zero; a 1-entry semaphore but the threads increase the value of it (therefore the final value of the semaphore shall be N) Both to tell the parent process "we are done" (Note: each thread can only write ONCE on the array, then the thread goes in while(1) pause;)
Anshul
For this code class Solution { public: bool hasAllCodes(string s, int k) { int need=(1<<k); unordered_set<string> got; for(int i=0;i<=(s.length()-k);i++) { string sub=s.substr(i,k); if(got.count(sub)==0) { need--; got.insert(sub); if(need==0) { return true; } } } return false; } }; and this input: s="0" k=20 i am getting this error terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr: __pos (which is 2) > this->size() (which is 1) but i am unable to understand why iam getting this error. for "0" substr method won't be called according to me. https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/description/
Hussein
Hello I have a text file and I need to read the different information of this file under several functions, but when I start reading the file in a new function, fscanf does not read the information from the continuation and starts reading them from the first line. Can you help me to fix it?
fscanf() is complicated and quite slow compared to fgets() because it does more than reading a file if you use the same file descriptor (FILE *) and call fgets() on the same file descriptor few times you will continue reading the file you should be able to do the same with fscanf() but if you want to specifically use fscanf() and that didn’t work then try to figure out how to keep track of how many characters you have read with fscanf() then use fseek() before calling fscanf() again
Hussein
when i create an array with a size that is unknown at compile time, is that going to be stored on the heap? for example: int n; cin >> n; int arr[n];
yes it is more likely to be stored on the heap the compiler will allocate your memory on the heap and free it automatically even if you don’t use malloc() or calloc() in C style or “new” in c++ style you can check for heap allocations in valgrind if you want to make sure on linux try “valgrind your_executable_file” after installing it ofcourse
Fenimoure
How do I pass a matrix like char charmatr[rows][cols] to a function? I tried this but it didn't work: task5_c(charmas[charmas_size][255])
Hussein
No, that arrays are allocated in stack, not heap
gcc at least accept it and allocates and frees memory on the heap automatically I have checked it with valgrind but to be honest it doesn’t always work in every situation
Αλι
How do I pass a matrix like char charmatr[rows][cols] to a function? I tried this but it didn't work: task5_c(charmas[charmas_size][255])
it decays to a pointer. see this link and learn how to do it. https://www.geeksforgeeks.org/pass-2d-array-parameter-c/amp/
Fenimoure
it decays to a pointer. see this link and learn how to do it. https://www.geeksforgeeks.org/pass-2d-array-parameter-c/amp/
I have tried every one of these. Didn't work for me (Subscripted value is not an array, pointer, or vector)
Hussein
How do I pass a matrix like char charmatr[rows][cols] to a function? I tried this but it didn't work: task5_c(charmas[charmas_size][255])
lets start with this one what error did you get? also can you show me how did you declare that matrix
Fenimoure
can you explain to me what error are you getting exactly?
Definition: void task5_c(char *mas[charmas_size][255]) Call: task5_c(charmas[charmas_size][255]); Errors: No matching function for call to 'task5_c'
Fenimoure
try this now: Definition: void task5_c(char (*mas)[charmas_size][255])
Nothing changed and I think I've tried this already, though I've rechecked. Still, nothing changed
Hussein
Nothing changed and I think I've tried this already, though I've rechecked. Still, nothing changed
how did compile your program and what command did you write in the terminal to compile it exactly?
Fenimoure
how did compile your program and what command did you write in the terminal to compile it exactly?
It did not. Incorrect convertation from 'char' to 'char (*)[7][255]' [-fpermissive] https://pastebin.com/FbGjhy5b
Hussein
how did you declare the original variable that you have passed to your function?
Hussein
can you show me the buggy part of your source code or an example very similar to your code so I may understand the problem?
Евгений
how did you declare the original variable that you have passed to your function?
Oh, Habibi, that's very simple. I'm sure. But I don't know😁
Hussein
Can I send you the file in the PM?
sorry I have no idea what is pm
Fenimoure
Hussein
ok sure
Евгений
Personal message, maybe?:))
● Igor
is there any warn flag in C++ for avoiding using after delete?
Ammar
is there any warn flag in C++ for avoiding using after delete?
I don't think there is. But you can catch a use-after-free bug at runtime by compiling your program with ASAN (Address Sanitizer). g++ -fsanitize=address -Wall -Wextra your_program.cpp -o exe -lasan; ./exe; The compile flag is: -fsanitize=address and the link flag is: -lasan Note: You need to run the program and actually perform use-after-free to catch it.
\Device\NUL
When you declare array like that it's stored on STACK, Not HEAP
● Igor
gcc at least accept it and allocates and frees memory on the heap automatically I have checked it with valgrind but to be honest it doesn’t always work in every situation
thats crazy cause i thought it was a language's feature, but i was actually risking to break something eventually
\Device\NUL
If you compile that with -pedantic-errors You will get error that ISO C++ doesn't support Variable length array
Ammar
thats crazy cause i thought it was a language's feature, but i was actually risking to break something eventually
It's a language feature, but in C, not in C++. You can have VLA in C++ if you are compiling with GNU extension.
● Igor
Somepeople use alloca() to let compiler allocate and free the memory
its weird because the compiler doesn't know which number i'm going to type
\Device\NUL
its weird because the compiler doesn't know which number i'm going to type
Yeah, i've already told you. It's supported on C99 (C11 specs made it optional), but not in ISO C++
Ammar
No, that arrays are allocated in stack, not heap
This is not true, only non-static local variables live on the stack. You can have array on the heap, stack, .data, .rodata, .bss or whatever memory area.
\Device\NUL
he declared the array with type arr[x]; . So I assume its allocated on stack
Ammar
he declared the array with type arr[x]; . So I assume its allocated on stack
I agree with that. My objection is just your sentence: > No, that arrays are allocated in stack, not heap which is not always the case. It's not the "array" that decides where the variable is stored. You can have an array anywhere, not only on the stack.
Ammar
and how the program manage to alloc space in the stack at runtime?
The stack allocation simply moves up and down the stack pointer. For architectures that have VM_GROWSDOWN flag for the stack, it will subtract the stack pointer to allocate space. And add the stack pointer to deallocate space. The reverse happens for the architectures with VM_GROWSUP flag for the stack. For example, on x86-64, allocating space on the stack is done by subtracting the stack pointer register, %rsp.
Αλι
Definition: void task5_c(char *mas[charmas_size][255]) Call: task5_c(charmas[charmas_size][255]); Errors: No matching function for call to 'task5_c'
i guess u should only write this void task5_c(char **mas) then pass two sizes of the dimensions as size_t. then index your ptr inside the func like mas[][]
Αλι
because no matter how many dimensions, it will decay to only one.
Αλι
something like this maybe: https://paste.ubuntu.com/p/q39TyvNtJF/
/get cbook
Anonymous
because no matter how many dimensions, it will decay to only one.
Wrong. int [10][20] will decay to int (*) [20] int [10][20][30] will decay to int (*) [20][30] int [10][20][30][40] will decay to int (*) [20][30][40]
Naina
https://onlinegdb.com/vGnPeYj8s In the above code i have to input two strings one after the other , but the compiler is not letting me do it . It just accepts one string and doesn't let me to input the second string .could someone please help me with this ?
Anonymous
https://onlinegdb.com/vGnPeYj8s In the above code i have to input two strings one after the other , but the compiler is not letting me do it . It just accepts one string and doesn't let me to input the second string .could someone please help me with this ?
Don't use void main(). Use int main() Don't use scanf. Use fgets and sscanf instead. Don't call fflush on stdin. Using fgets and sscanf as mentioned above doesn't require you to do things like fflush on stdin which is Undefined Behavior according to the C standard. Don't exceed your array bounds. Your code exhibits Undefined Behavior. As to how you can fix the problem with inputting two strings in your original code, just add a a call to getchar in-between the two scanf calls.
Sylvester Lim
https://dpaste.org/7HwaG can someone tell me why my loop always repeat even thought my choice is equal to 1,2,3 or 4
Pavel
https://dpaste.org/7HwaG can someone tell me why my loop always repeat even thought my choice is equal to 1,2,3 or 4
My guess would be that you need to use && instead of || in your while condition, because in your case the loop will stop if the value is equal to 1, 2, 3 and 4 at the same time, which is impossible :)
Pavel
https://dpaste.org/7HwaG can someone tell me why my loop always repeat even thought my choice is equal to 1,2,3 or 4
Or more likely the condition is more incorrect than that, I'm not sure when you expect the loop to stop
Sylvester Lim
please correct me if I am wrong , but doesnt || means OR ? so i wrote " if choice is not equal to 1 OR choice is not equal to 2 OR choice is not equal to 3 OR choice is not equal to 4
Sylvester Lim
when i input my choice as "1" or "2" or "3" or "4" , it loops again
Sylvester Lim
omg && works
Pavel
when i input my choice as "1" or "2" or "3" or "4" , it loops again
Let's say you've written 1 then it will be false true true || true which is true, it will continue looping
Pavel
Oh, formatting broke
Pavel
Lol, telegram can't properly escape spoiler tags in code blocks
Sylvester Lim
Thanks bud && worked !
\Device\NUL
vector of pair maybe ?
\Device\NUL
You can store multiple data with different types with std::pair
mahdi13
hi everyone how can I see the source code of any function in a library? for example sqrt() in math library or max() , log()
mahdi13
how we can find out whether a function belongs to which library?
mahdi13
I use vscode and when I did it just highlighted that word
Richard Luo 🐱
I use vscode and when I did it just highlighted that word
That means vscode doesn't find the source code
Richard Luo 🐱
Normally it should jump to source code
mahdi13
yes maybe
Hussein
hi everyone how can I see the source code of any function in a library? for example sqrt() in math library or max() , log()
/usr/include/math.h Is a header file You have to check the gnu libc code repository on their website or github because you only get the binaries