/dpt/ - Daily Programming Thread

Old thread: What are you working on, Sup Forums?

Other urls found in this thread:

gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
twitter.com/SFWRedditVideos

workin on line bots

>Now with anime.
Well at least we had some code going in the last thread.

Fuck anime faggots, reposting

for(unsigned long i = 1; i

Print __builtin_clz(i - 1), __builtin_clz(i), and __builtin_clz(i) + __builtin_clz(i - 1). See which one differs.
My alternatives are:
libtorrent-rasterbar (very well documented library), C++
libtorrent-rakshasa (don't know how well documented but not as good), C++
libtransmission (not documented at all besides from some comments in header files), C
various python libraries (all in beta and last updated years ago)

libtorrent-rasterbar seems best.

do "people" still shill rust or are they on vacation

>be C programmer
>start getting into C#/.NET
>actually seems feasible to do large projects on my own without spending my entire life debugging

Managed languages are the future.

Thanks for using anime picture.
Also, Ruby is a comfy language.

>upscaling a jpg image
fucking hell it looks terrible, get the original and restore it then upscale it
Do you have the unedited one?

Looks normal to me.

gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
>Built-in Function: int __builtin_clz (unsigned int x)
>Returns the number of leading 0-bits in x, starting at the most significant bit position. If x is 0, the result is undefined.
>__builtin_clz(i - 1)
>where i can be 1
>"If x is 0, the result is undefined."
There's your problem.

>Print __builtin_clz(i - 1), __builtin_clz(i), and __builtin_clz(i) + __builtin_clz(i - 1). See which one differs.

The sums differ for 1, where it's 31 and 0 vs 62 and from 2^32 upwards, where it gets -183716000 for summing them in the same line.

anime always looks terrible

So original reusing the same pic as yesterday.

RTFM dumb shit

>managed
Languages are not managed. If you are a shitty C programmer that is your fault.

>make a nice /dpt/ logo
>put text on it
>the text is all about who we don't want here
wow you must be a happy human being

It gets weird at 2^32 and higher.
Use #include for LONG_MAX.

That makes no sense, because after 2^32 none of the 2 halves of the number are 0.

After 2^32 you get overflow.
>Built-in Function: int __builtin_clz (unsigned int x)
It takes an int.
It doesn't matter that your i is a long, it takes an int anyway.
If you pass a long, it will be implicitly cast to an int, resulting in overflow, in turn resulting in repetition of test cases.

Just make everything long long

sure is summer

use __builtin_clzl

for fuck's sake

gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html

That won't help. See:

Ok I guess I'm retarded, thanks. Didn't find that.

Question regarding Visual Studio Community:
Is there a downside to using the 2013 version instead of the 2017 one?
I don't want to install anything on my Win7 laptop that has the touch of death of Microsoft post Win10 if I can help it.

There is only one correct OP image.

...

That image is gay.

int countleadingzeros(int toCount){
for (int i = 64; toCount & 1

This.

it aint
the guy needed one that takes a long
yours still takes an int

What's the most sneaky way to trick normalfag phone users to do a grabcut for image segmentation?

>drag a box around the foreground object
>color in the foreground object
>tap a few points on the foreground object
Any other ideas to trick the normies?

Ok thanks a lot, managed to do what I needed.
int clzl = __builtin_clzl(x);
int size = (sizeof(x - 1) * 8 - clzl) / 8 + (clzl % 8 != 0);

Calculates the size in bytes needed to represent a number.

how far squidways goeth your nose

long countleadingzeros(long toCount){
for (int i = 64; toCount & 1

What if there was a Rust based language that doesn't shove all these security measures on your throat

A language with
- ADT
- Pattern matching
- Currying
- HKTs
- Callbacks / Closures / Delegates
- No automatic pointer de-referencing
- No Classes, only structs and impls
- No :: and No < >
- Templates, Mixins and CTFE
- No GC
- RAII
- UFCS
- Abstraction with no runtime overhead
- Lazy iteration tools
- Compiles to LLVM IR

...

>unironically using a loop to do what a single instruction can do in silicon

Half of these are anti features.

Which one?

>posting disfigured faces to call people stupid
>being this insensitive to intelligent people with disfigured faces

Can't you just use sizeof to get the number size in bytes?
The compiler will optimize it, someone else's problem.
>hand optimized code will never be relevant again
why even live

Oh and just for keeping brainlets out: No for loops, only loop { }

>Can't you just use sizeof to get the number size in bytes?
That gets the size of the type
The point is to get the minimum size needed to represent the object losslessly in any type, regardless of what type currently holds it

>Oh and just for keeping brainlets out: No for loops, only loop { }
but that would make the language less expressive

>Can't you just use sizeof to get the number size in bytes?
I use it for packing an array, if I know my max value is going to be something like 4398046511002 I would like to know it only needs 5 bytes, not the full 8 it's currently in.

use sequtils instead of for. And for maths use loops

>no gc
You're deluded if you believe you won't benefit from an optional gc in your language
>raii
This should not be forced on the user either, esp since it restricts symbols' lifetime to the scope of the owning object.
>no classes
How is that bad? Fundamentally with UFCS the usual classes are structs that can hide their fields.
>abstraction with no runtime overhead
Aka losing portability

Im trying to get familiar with MIPS assembly.

I made this as an attempt to create my first function, and it seems to work fine, but is it cancer?

# multiply two unsigned words
# v0 = a0 * a1
# adds a0 a1 times
multiplyu:
li $v0, 0
multiply_loop:
beqz $a1 exitMultiply
add $v0, $v0, $a0 # v0 = v0 + a0
sub $a1, $a1, 1
j multiply_loop
exitMultiply:
jr $ra

can someone point me in the right direction on the real proper multiplication done by people smarter than me, like with signs n shit?

Misquoted but it's not exhaustive

int getRequiredSize(long number) {
!number&&return 0;
int neededBytes = 8;
while(!(0xFF

>using anything but pure x86 in real mode on ring 0
>not knowing true freedom

int getRequiredSize(long number) {
!number&&return 0;
int neededBytes = 8;
while(!(number & (0xFF

wtf?

Slow as shit, might as well use javascript.

What OS does Ruby use?

Classes are useless if you have structs, impls and traits

>The compiler will optimize it
Probably not.

There is never a good reason for high level exception handling prove me wrong.
How a C programmer gets around quicksand:
>use your fucking eyes and observe that there is quicksand
>walk around it
>continue on your way
How a C++ or Java programmer gets around quicksand:
>fall into the quicksand
>struggle your way out of it
>continue on your way
Pic related: anime

Compilers can't optimize away the retarded dev, yet.

Stop living in the 70's grandpa
How a modern programmer gets around quicksand:
>use your fucking eyes and observe that there is quicksand
>walk around it
>if you fall into the quicksand {
>struggle your way out of it }
>continue on your way

People use exception handling because it makes handler errors much easier rather than percolating the error upwards. A big grip people have with go is that it has primitive exception handling and you constantly have to have if err != nil {} which gets annoying.

How an assembly programmer gets around quicksand:
>undergo mitosis
>walk in all directions at once
>whoever doesn't fall in the quicksand gets to live
How a Rust programmer gets around quicksand:
>observe that there is quicksand
>panic loudly from a safe distance for 5 hours
How a Haskell programmer gets around quicksand:
>observe that there is no quicksand

When you don't know whether in.PajeetSoftwareDevelopment.app.CoreDisplayContainer.ReactorPooInstanceFactory.AllNodes(7) will work as intended or not.

Handling error conditions in C fucking sucks. Proof: experience, of which I have more than you.

This

Which one should you choose in these situations?

this

How a Common Lisp programmer gets around quicksand:
>use your eyes and observe that there is quicksand
>test the waters and analyze the pit
>determine whether walking around it, struggling through it or building a bridge yields the most efficient way of passing the quicksand
>do as concluded
>continue on your way
It's a disgrace that no language has yet to surpass CL's condition system.

I used to believe this. With even more experience, the issue has been muddied for me. Exception are often convenient in simple situations, but in certain more complex situations they start sucking a lot more. I'm not even sure which I'd choose given the entirely free choice.

what's the best C++ and javascript references?
isn't hackers delight just bit twiddling? i read a little of a pdf i downloaded of it

Given the choice, I would use a language that gives the programmer the power to define their own complex control flow constructs.

I normally try to rotate the images I post a bit, but due to know anti-anime the last OP was, I went with the most anime pic I had.

I'm coming from the world of Java where everything and someone's grandmother can be caught as an exception. Error handling in C appears to be on the side of "the user isn't that stupid".

I need to get into the weeds of it more. There's a lot I don't know.

>You're deluded if you believe you won't benefit from an optional gc in your language
There is a C library that can provide a GC to any language that needs it.

>it restricts symbols' lifetime to the scope of the owning object.
If a symbol has no owner, then logically it can't be used. Resources that are not in use should be destroyed, preferably in a way that can be consistently predicted. The most predictable manner to destroy resources is immediately when they are no longer usable. This isn't necessarily the fastest way to destroy them, but this is a factor that the programmer can optimize.

>Aka losing portability
Having no runtime overhead doesn't ruin portability at all. It just means that you have to compile a new binary for each platform.

Ubuntu.

What is the point of interfaces?

I'm just downloading the PDF to see what it's all about.

>write a specification for something
>pajeet team a can implement stuff against it
>pajeet team b can implement it
as I was taught anyway

How do you link in libraries in C++? The example code looks like this:
~/Documents/libtorrent/libtorrent-rasterbar-1.1.4> cat test.cc
#include
#include
#include

#include
#include
#include
#include

They make references to a libtorrent/ directory. This is found in include/libtorrent/. How do I reference it in the source file? Do I just move the source file into the include directory?
test.cc:5:34: fatal error: libtorrent/session.hpp: No such file or directory
#include
^
compilation terminated.
exit 1
~/Documents/libtorrent/libtorrent-rasterbar-1.1.4>

>pajeet team a
>pajeet team b
There's more than one team??

yes, one in mumbai and one in uttar pradesh

R-Ruby senpai, what OS and WM do you use?

>Error handling in C appears to be on the side of "the user isn't that stupid".
The user IS stupid though, and lazy. When was the last time anybody checked the return value of printf? How many C programmers check for malloc returning NULL?

Are you fat Ruby? Do you have a beard? What colored eyes do you have? What is your favorite programming language?

to separate the interface from the implementation

the user of the interface doesn't need to know about the implementation

you can provide better/different implementations of the same interface

>The user IS stupid though
I don't debate that. I agree. I'm just saying Java appears to be easier to handle shit hitting the fan better than C.

Like I said, I need to experiment with C more. I could be just judging early.

I want to use Ubuntu, but right now I am stuck with Gentoo :(

What did he mean by this?

it means your racist

> When was the last time anybody checked the return value of printf?
Who the fuck cares what the return value of printf is? Either you've passed an invalid format string, which the compiler can easily pick up on anyway, or the underlying file has been closed, in which case, who the hell cares and what are you going to do about it?
>How many C programmers check for malloc returning NULL?
Actually, most C programmers do this. If you look at actual projects, this does happen.
If someone really doesn't give a shit about recovering from OOM errors, it's extremely likely they'll add an "xmalloc" function and use that instead.
void *xmalloc(size_t n)
{
void *ptr = malloc(n);
if (!ptr)
exit(1);

return ptr;
}

>exit(1);
>not using abort();
why do I still live with all this suffering that I see?

If someone really doesn't give a shit, why can't they just ignore the error?

Ubuntu+Unity. I'm lazy.

I'm sort of a skinnyfat, I have a beard, my eyes are blue, and I prefer Ruby and C++.

Did you not install it through your package manager? Your compiler by default should look in /usr/include for headers, so the libtorrent folder with all of the *.hpp files should be installed to that directory. If you don't want to install it to your system directories, gcc and clang both use the -I flag (that's a capital 'i', not a lowercase 'L') can be used to tell your compiler to look in additional directories for headers.

I want the proper exit functions to be called, buffers flushed, file descriptors closed, and all that to happen, though.

>why can't they just ignore the error?
In an xmalloc function, you can log why your program closed unexpectedly.
Relying on a NULL pointer dereference and undefined behaviour is just bad form.

No, I just compiled it and gave it the include directory manually. It worked though, but now something else (boost?) is acting up:
~/Documents/libtorrent/libtorrent-rasterbar-1.1.4> g++ test.cc -B . -std=c++11 -lboost_system
/tmp/cc62M7th.o: In function `main':
test.cc:(.text+0xc7): undefined reference to `libtorrent::default_storage_constructor(libtorrent::storage_params const&)'
test.cc:(.text+0x150): undefined reference to `libtorrent::session_handle::add_torrent(libtorrent::add_torrent_params const&)'
test.cc:(.text+0x178): undefined reference to `libtorrent::session_handle::pop_alerts(std::vector*)'
test.cc:(.text+0x303): undefined reference to `libtorrent::session::~session()'
test.cc:(.text+0x39a): undefined reference to `libtorrent::session::~session()'
/tmp/cc62M7th.o: In function `libtorrent::session::session(libtorrent::settings_pack const&, int)':
test.cc:(.text._ZN10libtorrent7sessionC2ERKNS_13settings_packEi[_ZN10libtorrent7sessionC5ERKNS_13settings_packEi]+0x62): undefined reference to `libtorrent::ipv6_deprecated_()'
test.cc:(.text._ZN10libtorrent7sessionC2ERKNS_13settings_packEi[_ZN10libtorrent7sessionC5ERKNS_13settings_packEi]+0x7a): undefined reference to `libtorrent::session::start(int, libtorrent::settings_pack const&, boost::asio::io_service*)'
collect2: error: ld returned 1 exit status
exit 1
~/Documents/libtorrent/libtorrent-rasterbar-1.1.4>

>in which case, who the hell cares and what are you going to do about it?
According to POSIX printf can fail with ENOMEM, so by the same logic that you would check for malloc returning NULL, you should check the return value of printf.

>relying on
Your program's behavior is undefined for when it's out of memory, simple as that.

No it's not. There are programs that can run into OOM and need to be able to recover from it and keep operating.

It's not going to lead to any sort of bad memory references. You won't get a message shown, and that's it. Maybe that's a big deal to your program, but checking printf's return value sure as fuck isn't going to fix whatever the underlying problem actually is.

W-Why do you like C++ and not C, senpai?

>by the same logic that you would check for malloc returning NULL, you should check the return value of printf.
Why though?
If malloc returns 0, and you don't check it, you'll probably try to use it, resulting in undefined behavior. That's why to check it.
If printf returns ENOMEM, that won't invalidate anything else you try to do. Including trying to continue to print, which would be well defined, but just no longer useful, which is not a big deal.