/dpt/ - Daily Programming Thread

What are you working on, Sup Forums?


Previous Thread:

Other urls found in this thread:

richardfarrar.com/plato-and-object-oriented-programming/
twitter.com/SFWRedditVideos

>want to work on something
>can't stop browsing the internet

thank you hime

let's be honest, C++ is for autists

normies can't keep in their heads the thousands of essential edgecases and nuances of core c++

This stupid fucking danish gnome and those fucks behind Java set back programming by decades

why would anyone do that?

So basically, if I have no pointers, then the complexity is going to not be O(1)?

thankfully this is not the 90s and we can stop pretending that OOP makes sense

In C, what is the difference between | and || in terms of boolean? Why can't I just do this for boolean statements?
if (A | B){...}
// instead of
if (A || B) {...}

|| returns true if all it's operands are not zero
| returns the bits that are flipped on in both operands
if this new bitfield is not zero, the if conditional returns true

| is a bitwise inclusive OR operator.
|| is a logic operator with short circuit evaluation.

That's the point tho? 10 == 10 is 1, 12 < 10 is 0, so
if (10 == 10 | 12 < 10) {...}
=> if (1 | 0) {...}
=>if (1) {...}


>short circuit evaluation
I guess this is the only reason.

The double || will always return either 1 or 0. The single | can return any bit pattern.

they do completely different things
learn how expressions work in C

Yes, and? Would you trust a normie to do systems programming?

Explain why programming in insecure languages is still permitted.

Because secure languages are hella slow.

an excellent point

creating jobs

We don't need more internet-connected lightbulbs, cryptographically insecure 'smart' heating or leaky web apps, thank you very much.

you can do it all and more efficiently in C where you just have to be smart

Moore's law held true for many years. Today's computers are fast enough.

name a secure language

name a language that can't be used securely

it's never fast enough

Java

Java

>office PC has Fedora installed
Niiiccceeeeee

>name a secure language
One that doesn't let you directly access memory

>name a language that can't be used securely
There's several decades of evidence showing that programmers cannot write secure code in any non-trivial project

still working on that language. This is an example from the Ada95 book translated into my language.
Sum is
first, second: i32
Sum

And since it's essentially supposed to be ada with symbols, you can either specify with words:
proc Sum is
first, second: i32
begin
put "Enter two intergers: "
get first
get second
put "The sum is #{first + second}"
newline
=>Sum

or go full symbolshit
Sum is
first, second := 0
>first>secondSum

>not gentoo

for what reason?

>he works for the CIA
must be cool

>One that doesn't let you directly access memory
you can have bounds checking etc

I mean nothing can be made completely idiot proof, see halting problem

Can I use strncmp to compare two data structures?
struct State {
int i;
int a;
}

struct State m = {1, 2};
struct State n = {1, 3};
strncmp(&m, &n, sizeof(struct State));

>you can have bounds checking etc
If you can circumvent a safety feature then you can bet programmers will do it

"I know what I'm doing, it'll make it run faster, look at that 2% speedup, don't worry this will never overflow"

Many of C++'s abstractions are zero-cost. The equivalent code written in C would not offer any benefit in efficiency. And strictly speaking, you have to be smart to use either language, but even a smart person makes a mistake every so often, and these are often intolerable in low level code. RAII prevents a lot of mistakes, so why not use it?

Everything not explicitly forbidden is permitted. Programming should not be regulated in any way.

>One that doesn't let you directly access memory
Oh, so you want people to not be able to write device drivers. That's lovely.

Most programmers have no need of a Turing-complete language.

Most programming problems can, in practice, be defined in terms of total functions and/or functions that are provably productive on an infinite, lazy data structure in finite time.

>only worry about C and procedural programming for months
>read some OOP introductory concepts but can't really see the whole picture
>more months pass
>read Plato
>OOP makes sense now

>Oh, so you want people to not be able to write device drivers. That's lovely.
I don't want programmers anywhere near device driver source code. Engineers only, please. Programmers cannot be trusted.

memory manipulation is not everything, you can't stop people from making high level algorithmic mistakes either

But at least if everything is written in a secure language, there's no risk of a bad algorithm in one program being able to break other programs.

Why is it so ugly
Do those symbols have useful meaning?
Give me 4 reasons why this is better than just making most operations function calls, and using lambda calculus based syntax/semantics

>Many of C++'s abstractions are zero-cost. The equivalent code written in C would not offer any benefit in efficiency.
That's complete bullshit. Also, enter the benchmark game and prove me wrong if you're so smart.

>Plato
richardfarrar.com/plato-and-object-oriented-programming/
this?

>Many of C++'s abstractions are zero-cost
inheritance and vtables are not zero-cost
RAII is not zero-cost
smart pointers are not zero-cost
exceptions are not zero-cost
copying everything at every turn is not zero-cost

>Why is it so ugly
It's for low-level system writing. I don't get what's so ugly about it, it retains ada's readability while also making it easier on the programmer
>Do those symbols have useful meaning?
is print
> is puts (or put, in the language's case)
>Give me 4 reasons why this is better than just making most operations function calls, and using lambda calculus based syntax/semantics

They are function calls, i just broke it down to it's base elements

What was the other big ebook library everyone used besides shodan? Trying to find a programming book not in the library.

Yes that plato, but I didn't read that, just studied Plato in general and when I was going through his theory of forms it sounded familiar and when I revisited OOP concepts it just clicked.

In retrospect I don't know why the concept of objects and classes confused me, I think the usual examples they give of a a car class and an animal class confused me because I just never understood why anyone would want to program that.

unique_ptr is allegedly zero cost

Sure, but shared pointers aren't.

>tfw codeblocks is so shitty I have to turn emacs into my debugger

When you move out a unique_ptr, it just nulls the old unique_ptr.
This means that if you forget that you moved it, you'll likely get null errors. Also, the destructor will do a null check, so you have to bet on compiler optimizations removing that check (also on this point: a lot of C++'s "zero cost" comes from extremely aggressive compiler optimization; the base C++ language is laiden with empty procedure calls). An affine type system would be able to both prevent the bug and statically decide if the destructor needs to be called.

I don't get why people use shared pointers instead of an actual garbage collector. Reference counting has the disadvantage of high overhead with the disadvantage of not being accurate. Worst of both worlds.

embrace it

ok, thanks

>it just nulls the old unique_ptr.
>This means that if you forget that you moved it, you'll likely get null errors
Hmmmm. It's as if being uniquely owned is the point of unique_ptr.

>not using gdb in terminal to debug everything

It's like you're actively trying not to make it.

What I'm saying is that with affine types, it will literally be a compile error to try to access the memory after it has been moved.

Also reference counting has horrible effects on multithreading performance if done naively (i.e. a mutex for every refcount increment/decrement). But you have to do something if you're going to use them across multiple threads.

One thing that I like about Rust is its distinction between Rc and Arc. You can't use the former across multiple threads, so it doesn't pay the performance penalty.

1. The definition of a programmer is any person who creates programs.
2. A program is defined as a collection of instructions to be executed by a computer.
3. A device driver is a collection of instructions executed by a computer to control a piece of hardware.
4. Because of 2 and 3, a device driver is a program.
5. Because of 1 and 4, a person who creates a device driver is a programmer.
6. Therefore, if we consider that you do not want programmers anywhere near device driver source code, then no person can create and edit device driver source code. We know from 5 that even an engineer who develops device drivers is also a programmer.
6.5. Therefore we can conclude that you do not want device drivers to be created by anyone.

>inheritance and vtables are not zero-cost
>exceptions are not zero-cost
These are fair points, and I would argue that these are not tools that one should jump to every time, but instead, evaluate whether the cost is worth it.

>RAII is not zero-cost
Trivial destructors are aggressively inlined by modern compilers, often making them zero cost.

>smart pointers are not zero-cost
unique_ptr is zero-cost in a modern compiler.
shared_ptr is not zero-cost. Again, a choice for the programmer to evaluate whether it is a good idea.

>copying everything at every turn is not zero-cost
Then stop copying everything all the time.

>learning R(J)ust

Nothing wrong with learning that. Using it, though...

>Then stop copying everything all the time.
Do you realize that this falls into the terrible argument of "just be a better programmer!" that you're probably using to justify C++ over C?

Companies like Google that are supposed to have very talented engineers have had huge problems with excessive copying in C++

>a lot of C++'s "zero cost" comes from extremely aggressive compiler optimization; the base C++ language is laiden with empty procedure calls
Yes, and? Is there any particular reason why you are not using aggressive compiler optimization on everything?

fuck my logic doesn't make sense here but the point is that "stop copying" doesn't work in C++ because const references are easy and rvalues are fucking confusing, and it's super easy to implicitly do a huge deep copy in C++

>tfw delete all my copy constructors by default

I wanted to know whether it is worthy of its hype.

>using a language that you have to manually patch all the holes and deficiencies
have fun with that

It does not take a lot of effort to pass a big fucking struct by const reference. Any C++ programmer with experience will do this instinctively, and it is harder to forget than accidentally leaking memory or doing a use after free in C. The only time people don't do this is when they don't fucking understand the language. The reason Google has problems with excessive copying in C is because their programmers are in their own words, too stupid for C, which is why they made Go.

But now that C++ has move semantics and rvalue references, sometimes passing values instead of references results in more efficient code.

You are not an woman.

If it results in more efficient code, why are you complaining?

Thank you for pointing out the obvious, but you should check your grammar. It should be "a woman," not "an woman."

>deficiencies
What did he mean by this?

I didn't take this question seriously at first, but then I did it and it actually fucking worked.

#include
#include

#include
typedef char ElemType; /* to make changing
the type of the LL easier*/
struct Node
{
ElemType Item;
struct Node* Next;
};

struct Header
{
struct Node* Head;
struct Node* Rear; /* To make insertion into back O(1) */
int size;
};

typedef struct Header* List;
/* Done defining auxiliary structures */

struct Node*
InitializeNode(ElemType X, struct Node* nextNode)
{
/* Initialize Node* and create enough space for the ptr */
struct Node* toRet;
toRet = (struct Node*) malloc(sizeof(struct Node));


toRet->Item = X, toRet->Next = nextNode;

return toRet;
}
/* Inserts an element at the start of the list
(If we were to insert it at the end, we would have
cons('T', cons('o', list)) == 'oT')

List
cons(ElemType elem, List ll )
{
struct Node* toInsert;
toInsert = InitializeNode(elem, NULL);
if(ll->Rear == NULL) /* Initialize rear */
{
ll->Rear = toInsert;
return ll;
}
toInsert->Next = ll->Head;
ll->Head = toInsert;
return ll;
}

int main()
{
List list1, list2;
list1 = (List) malloc( sizeof(struct Header) );
list1->Head = list1->Head = NULL;

list2 = (List) malloc(sizeof(struct Header));
list2->Head = list2->Head = NULL;

list1 = cons('T', cons('E', cons('S', cons('T', list1))));
list2 = cons('T', cons('E', cons('S', cons('T', list2))));

printList(list1);
printList(list2);

printf("Comparing using strcmp... %d\n", strcmp(list1, list2));
printf("Comparing using strcmp... %d\n", strcmp(list1, list1));

}

The point is that "pass things by const reference" isn't optimal advice. Sure, in many situations it is best, but there are a few edge cases where it is not. On top of the many other edge cases C++ has. The language is practically nothing but edge cases at this point, and remembering all of them is something very few people are likely capable of doing.

Not first language. Was actually going to spell it write but changed my mind in the last second.

But none of this matters. You are not a real woman. You degenerate fuck.

>When you move out a unique_ptr, it just nulls the old unique_ptr.
>This means that if you forget that you moved it, you'll likely get null errors
that's why move semantics are explicit in that context. forgetting you invalidated an object a few lines after you explicitly asked for that would be a pretty stupid mistake. it'd be like assigning a literal value to a variable and then being surprised that it's not some other value when you refer to it soon after. the common case is moving ownership *out* of a scope anyway, in which case it's not an issue

Fuck. forgot the output:
Test_strncmp.c:74:53: warning: incompatible pointer types passing 'List'
(aka 'struct Header *') to parameter of type 'const char *'
[-Wincompatible-pointer-types]
printf("Comparing using strcmp... %d\n", strcmp(list1, list2));
^~~~~
/*
...
Compiler Warnings
*/
4 warnings generated.

T ->E ->S ->T ->
T ->E ->S ->T ->
Comparing using strcmp... -64
Comparing using strcmp... 0

>very few people are likely capable of doing
And they don't have to. All these edge cases just give people who are aware of them the ability to consider them in particular. Otherwise it'd happen as with any language that has a unified way of handling everything: better working in some cases than others.

Because strncmp compares each bytes of the structure. I came to the conclusion that it shouldn't be used because I don't think the C standard guarantees that each element in a structure are aligned together.

Of course it's gonna work, char are bytes aka the smallest addressable unit.

Holly fuck just stopped to read it. I swear I'm not that uneducated, it was the the phone spell checking.

OMFG now everyone thinks I'm a phone poster.
It was only this time guys, I swear.

FUCK I JUST TO FIT IN

I downloaded a Java library I want to use, which put in the lib folder of my project. The library I downloaded has its own lib folder with the library's JAR files and a source folder.

For my own classes in my src folder how can I import the library from my lib folder?

>rule of 5
>have to write 4 almost identical initialization functions to instantiate an object
fuck this Im goin back to structs

Stop embarrassing yourself

>FUCK I JUST TO FIT IN

PHONEPOSTER BTFO.

>structs
*Rust
fixed that for you

>heh nice argument kid but I see you made a typo

...

HOLY FUCKING SHIT NOT FUCKING AGAIN

Is it actually portable to use strncmp on a structure or any arbitrary array????

I'm writing a http media player for android but should I target android 5.0 for flac support?
It'll cut out 30% of users

If the standard library is properly implemented I don't see why not. But again

Just compare them using == and then let the compiler deal with optimization.
With arrays you might have issues with padding bytes not being equal when all the fields are equal

The thing is you might want to know how much they differ.

i want to like rust but holy shit it's so ugly
it's like C++ devs are so used to eating shit that the rust team has to wipe their asses with it to make it palatable

How will strncmp tell you this correctly? I would suggest just writing the code that conveys your intent the best and then benchmark later

just use pod/aggregate/standard layout types when you can. you can even still use inheritance costlessly in some cases to avoid redundancy/simplify composition, and aggregate initialization still works in many of those cases. a lot of people would benefit from taking a more data-oriented approach to C++

Returns -1 if the first argument was lower and +1 otherwise. The point is it's not semantically equivalent to ==.