Ammar
Hey Ajay, take a look to std::fill_n. It is probably what are you looking for.
Ajay
I often get segmentation fault in my cpp code, debugging is a way but are there any other ways to tackle with this problem or ways that can help to find at which line did the exception occurred?
Ammar
Valgrind should also be helpful, it also tracks invalid read/write and memory leaks.
Dima
Use smart pointers Use code static analysis tools
smart pointers are making it way much easier:/
Ammar
Best tools for debugging: gdb, valgrind, radare2
Ammar
Also use gef for your gdb. It makes you easier to see instruction, registers and stack.
Mar!o
my classic was: void free_stuff(stuff **restrict const stuff) { free(stuff); <— should be *stuff *stuff = NULL; }
Anonymous
I'm running a function on a thread, and it contains a gets that asks for input. Now, after starting this thread, I decide that I don't need the input anymore. Is there any way I can "close" the gets prompt? Like supplying it with some random data?
Ammar
I am unsure btw
Anonymous
That's the problem. It cannot wait. I won't know if the input comes first, or the decision that I don't need the input does.
Ammar
Just thought that it was Linux.
Ammar
I wonder what is the purpose of that folder.
Ammar
I see, thanks.
Ammar
Try this one btw https://stackoverflow.com/questions/38329044/closing-then-opening-standard-input-in-c-under-linux
Anonymous
Well, I'm trying to run a chat server, that constantly asks for user input, and prints a message immediately when it's received. The receiver function also runs on a thread. I'll try to think of some other way then.
Ammar
I haven't tried but this eww https://serverfault.com/questions/188936/writing-to-stdin-of-background-process
Ajay
Compile your code with flag -ggdb3, and run it with gdb. It will tell you the line of code which it gets SIGSEGV.
I just started using gdb so I wrote a simple hello world program, and ran gdb, after gdb starts, run command should work right? and should output on the console right?
Ajay
Yes, sure.
it kinda stops there and does nothing
Ajay
like this, IDK why it isn't outputting anything.
Ammar
Ok, I agree with you.
Ammar
Ajay
Looks it is waiting for something.
I didn't provide any breakpoint
Ammar
Try to send SIGINT and check the current instruction.
Ammar
CTRL + C x/10i $rip
Ammar
I assume the arch is x86_64
Ludovic 'Archivist'
It is the same amount of skipped items for any hash, it is an increasingly large amount until you have failed more than 3000 probings and then it flips back to linear probing
Ajay
it is simple hello world, I was just trying it for my previous code but it kinda started waiting, so I wrote a simple hello world to check if it still waits and see it still is waiting. IDK why?
Anonymous
If you want the details, the receiving function stops as soon as the connected socket is closed. This is the point when I want the gets of the sending function to be closed too. It does stop when it fails to send whatever input you give, but that's one useless input I'm trying to get rid of.
Ludovic 'Archivist'
Your thing reminds me of cuckoo hashing, which I did use at some point
Ammar
I didn't provide any breakpoint
Or try gdb -tui ga It shows the code being executed.
Ludovic 'Archivist'
You can find an implementation of this here (albeit not for a hashmap but a quotient filter): https://git.nekoit.xyz/Archivist/gplib/src/branch/master/include/gp/quotient_filter.hpp
Anonymous
Anonymous
Reminder that compilers special case functions like memset so sometimes they may not work
Ammar
yeah it shows the correct code
Still no clue, try to press CTRL + C and write x/10i $rip It shows the Assembly instructions being executed.
Ammar
yeah it shows the correct code
It usually shows an arrow to corresponding line, but it does not.
Ajay
yeah it shows the correct code
Actually, I'm using MacOS mojave and so when i sought the problem online, i followed this link which says gdb is not compativle with MacOS sierra(didn't check for mojave)
Ammar
Reminder that compilers special case functions like memset so sometimes they may not work
explicit_bzero is just a prevention for optimization, he needed multibyte fill.
Ajay
aah man, earlier I was doing ctrl+z which was working(stopped the running program) because ctrl+c wasn't but now even ctrl+z isn't working.
Ammar
https://stackoverflow.com/questions/40110435/unknown-ending-signal-when-using-debugger-gdb
I never used gdb on Mac OS. Don't really know if it is not compatible.
Ammar
Try valgrind sir.
Sasuke
My program uses SDL library which is cross platform but installation is needed for every platform which is different accordingly so I wanted to ship my program without hassle to the user like installing a compiler and installing required libraries how can I do that?
Ajay
Try valgrind sir.
g++ -g filename.cpp valgrind ./a.out is this right?
Ammar
g++ -g filename.cpp valgrind ./a.out is this right?
Yes, some additional flags may be suggested in runtime.
Ajay
Yes, some additional flags may be suggested in runtime.
I just wanted the segmentation fault to be caught, I hope just that to work.
Ajay
Try to run a segfault code then
yes, It is showing Invalid write of size 4 ==6760== at 0x100000DA9: solve() (ga.cpp:11) ==6760== by 0x100000DC2: main (ga.cpp:17)
Ammar
Good, happy to work.
Ajay
But, when I simply create int a[3000000]; inside main and run then why doesn't that give segmentation fault? But, it does give segfault in that code. Any reason?
Ammar
But, when I simply create int a[3000000]; inside main and run then why doesn't that give segmentation fault? But, it does give segfault in that code. Any reason?
Your program may still have available stack. When you start to call other functions your stack will start increasing until the called function reaches epilogue (return).
Ajay
I tried experimenting doing long long a[3000000000000000000000]; then the compiler says error: size of array 'a' is negative, why? Even adding ll at the end of that number says that too.
Ammar
I wish we could see the stack space allocated to my current cpp process.
Probably can't, but you can see the max limit stack by using getrlimit.
Ajay
long long is ~ 1e18, so i think that shouldn't be the problem
Anonymous
The largest array I've ever seen was like 3000 in size
Ajay
oh, didn't see that 😅
Anonymous
Where? In Linux kernel COMMAND_LINE_SIZE constant
Ludovic 'Archivist'
Anonymous
I use bunches of 4096 elements arrays
Still nowhere close to 3e21
Anonymous
Or his numbers
Ludovic 'Archivist'
Still nowhere close to 3e21
Yeah, I think the largest array I ever used was like, 16'000'000 elements or so
Ajay
Still nowhere close to 3e21
I was just experimenting, didn't use it for any purpose. 😅
Ammar
https://github.com/torvalds/linux/blob/master/arch/x86/include/asm/setup.h#L7
Anonymous
I found this uapi/asm/setup.h
Not a mainline kernel
Anonymous
It was an android kernel
Anonymous
Besides if you've ever seen an android kernel cmdline it's way bigger than 255 chars
Ammar
It was an android kernel
Which architecture of Android? I think it is separated by archtecture.
Mar!o
Yeah, I think the largest array I ever used was like, 16'000'000 elements or so
Pffff the biggest array I've ever created was a dynamic which reserved the whole RAM left without 100 MB for other programs