/dpt/ - Daily Programming Thread

What are you working on, Sup Forums?

Old thread:

Other urls found in this thread:

en.wikipedia.org/wiki/Triangular_arbitrage
devernay.free.fr/hacks/chip8/C8TECH10.HTM
pong-story.com/chip8/
github.com/Valloric/YouCompleteMe
twitter.com/NSFWRedditGif

When I write C, I write in the common C/C++ subset.

Reposting from previous thread

I'm trying to return a double value in Java, but I only want the value to have 2 decimals. Bear in mind this is for an assignment, and I can't use some methods and classes from the library.

I thought of doing it like this:
return (int) mathematicalCalculation*100/100.0;
the problem is, it only returns the value with 1 decimal, and that decimal is 0 regardless. Any solution? I was thinking using printf, but then the method would have to be void, and that's not possible nor allowed in my case

babby haskell, r8

What you're trying to do doesn't really make sense given how floating point numbers are defined under the hood. So you can either implement a fixed point type with two ints or use a double and only print the first two decimals.

numbers that end in zero will always be hidden in number types.
if you need to display to two decimals with trailing zeroes you need to convert to a string

I'm trying to use gprof to profile an application with sld and OpenGL in an unfamiliar code base. Gprof doesn't seem to be working since I think my app is not terminating cleanly. Is there a fix to this?

Or any good alternative profiling tools for C code or c++?

Trying to use websockets.

websocket = new WebSocket(wsUri);
websocket.send("hello world");


I get the following error: frames from client to server must be masked

Anyone know how I can tell my Firefox to mask the damn messages?

fib = 1 : 1 : zipWith (+) fib (tail fib)
sum . filter even . takeWhile (< 4000000) $ fib

fk u what even is haskell

triangular arbitrage bot for cryptocurrency markets

You are almost as bad as sepples shitters.

Proud Pajeet here, learning C programming with the notorious C programming book (for kids).


Nowhere does it explain what the -> is for, and what's the difference between using -> or calling a table element like this "table.element"


What's -> for?

Sounds interesting. Are there any open source APIs/whatever to interact with public blockchains?

you might as well use C++, all the fun C idioms are compiler errors in C++.
C++ doesn't even have compound literals.

-> is for dereferencing a pointer to a given element. . just specifies a local offset.

-> can also be written as (*ptr).element

im not interacting with blockchains. Im interacting with cryptoexchanges using their developer API's to extract a zero risk profit from a 3-way currency exchange
>en.wikipedia.org/wiki/Triangular_arbitrage
I have an API wrapper written for an exchange. The bot itself needs to be crypto-exchange agnostic, so I have a second wrapper for the API that the bot can use, and once the bots done it will be able to operate on any exchange I have a wrapper coded for

It's syntactic sugar for structs.

struct foo { int x; };
struct foo * aFoo = (struct foo *) malloc(sizeof struct foo);

// These are equivalent
int a = (*aFoo).x;
int b = aFoo->x;


Now pretend x is a pointer to some other structure and it becomes obvious why the latter came to exist.

-> is a shorthand way of dereferencing and and accessing a member.

Because *some_struct.some_member is dereferencing the member and not the struct, you'd have to write it like (*some_struct).some_member.

-> allows you to just write some_struct->some_member instead.

Is using windows for dev deprecated in 2017?

Also note that the parens in the former are necessary due to operator precedence.

What's the best "not vim" editor/ide for c/c++ development?

>tfw valgrind clean

Well do you want a text editor or an IDE? In either case, the obvious answer is emacs.

Qt Creator with vim plugin.

noob here
confused a bit about passing as ref in C

example:
int a = 50;
int *p = a;

foo(p);

...


foo(int *lePointer){
//do something
}


so we are passing the address of the pointer p to foo

foo(p)

how is the address of a pointer converted into *lePointer?

how is *lePointer = p ?

the value of *lePointer is the address of p
in my understand, if you wanted to get the value of p in foo() you would need to **lePointer
wtf?
I can't seem to grasp this
any explanation or link would be appreciated

The value of lePointer is the value of p which is the address of a.

>int *p = a;
That's wrong. It's int *p = &a;

>so we are passing the address of the pointer p to foo
No. You're passing the value of the pointer 'p', which happens to be the address of a.

Rather it would be the value of a if you did int *p = &a;. You can also do int *p = (int *) a in which case the value of p is the address 50.

Watch and learn:
↑+⌿⊃+.×\(⊂2 1),10⍴⊂2 2⍴3 2 2 1

my bad on the &

ok so when you call foo

*lePointer = p?
so if you printed *lePointer it should print the address of a?

...

No.
lePointer == p.
*lePointer == a.

If you live in a big city, you can pull in $70-80k starting if you're decent. If you're good at what you do, six figures starting is realistic.

At this point, don't focus on learning things to be "marketable." Build out a decent portfolio on Github, really study the languages you put on your resume, and do practice problems from HackerRank, Cracking the Coding Interview, et cetera.

Technical reference: devernay.free.fr/hacks/chip8/C8TECH10.HTM
Example games: pong-story.com/chip8/

don't worry about it, just keep going
you can get really autistic in haskell if you practice enough, it goes deep

Ok thanks, I think I understand what it is for now.

But the way you declare the pointer to foo struct is new to me.
Would you mind explaining what

struct foo * aFoo = (struct foo *) malloc(sizeof struct foo);

does?

I've never liked Visual Studio or IDEs in general. If you have other software that only runs on windows and don't feel like screwing around with WINE, MSYS2 and notepad++ are just fine.

that's what's confusing me

if the foo accpets int *lePointer

and you pass foo(p)
how is *lePointer not = p?

you can go low if you can (assmebly n shit or whatever) I just want to understand how this works

i thought when you called a function its parameter is just a copy of what you called it with

here:
foo(p)
foo(int *lePointer)

*lePointer = p
how does the compiler know? or am I just stupid and not getting this?

Qt Creator or CLion

Will this pretentious cuck thread ever decease?

None of you can program, fuckwits.

⎕fmt 3 2 2 1
┌4───────┐
│ 3 2 2 1│
└~───────┘
⎕fmt 2 2⍴3 2 2 1
┌2───┐
2 3 2│
│ 2 1│
└~───┘
⎕fmt ⊂2 2⍴3 2 2 1
┌──────┐
│┌2───┐│
│2 3 2││
││ 2 1││
│└~───┘2
└∊─────┘
⎕fmt (⊂2 1),10⍴⊂2 2⍴3 2 2 1
┌11──────────────────────────────────────────────────────────────────────────┐
│┌2───┐ ┌2───┐ ┌2───┐ ┌2───┐ ┌2───┐ ┌2───┐ ┌2───┐ ┌2───┐ ┌2───┐ ┌2───┐ ┌2───┐│
││ 2 1│ 2 3 2│ 2 3 2│ 2 3 2│ 2 3 2│ 2 3 2│ 2 3 2│ 2 3 2│ 2 3 2│ 2 3 2│ 2 3 2││
│└~───┘ │ 2 1│ │ 2 1│ │ 2 1│ │ 2 1│ │ 2 1│ │ 2 1│ │ 2 1│ │ 2 1│ │ 2 1│ │ 2 1││
│ └~───┘ └~───┘ └~───┘ └~───┘ └~───┘ └~───┘ └~───┘ └~───┘ └~───┘ └~───┘2
└∊───────────────────────────────────────────────────────────────────────────┘
⎕fmt +.×\(⊂2 1),10⍴⊂2 2⍴3 2 2 1
┌11────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│┌2───┐ ┌2───┐ ┌2─────┐ ┌2──────┐ ┌2───────┐ ┌2─────────┐ ┌2──────────┐ ┌2───────────┐ ┌2─────────────┐ ┌2─────────────┐ ┌2───────────────┐│
││ 2 1│ │ 8 5│ │ 34 21│ │ 144 89│ │ 610 377│ │ 2584 1597│ │ 10946 6765│ │ 46368 28657│ │ 196418 121393│ │ 832040 514229│ │ 3524578 2178309││
│└~───┘ └~───┘ └~─────┘ └~──────┘ └~───────┘ └~─────────┘ └~──────────┘ └~───────────┘ └~─────────────┘ └~─────────────┘ └~───────────────┘2
└∊─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
⎕fmt ⊃+.×\(⊂2 1),10⍴⊂2 2⍴3 2 2 1
┌2───────────────┐
1 2 1│
1 8 5│
│ 34 21│
│ 144 89│
│ 610 377│
│ 2584 1597│
│ 10946 6765│
│ 46368 28657│
│ 196418 121393│
│ 832040 514229│
│ 3524578 2178309│
└~───────────────┘
⎕fmt +⌿⊃+.×\(⊂2 1),10⍴⊂2 2⍴3 2 2 1
┌2───────────────┐
│ 4613732 2851443│
└~───────────────┘
⎕fmt ↑+⌿⊃+.×\(⊂2 1),10⍴⊂2 2⍴3 2 2 1
4613732
~~~~~~~

Gee, I sure hope Sup Forums doesn't mangle this.

It allocates a new variable of type "foo" on the heap

malloc returns a pointer to the beginning of a block of memory in this case the block of memory is the size of struct foo
he is casting the returned pointer directly to the type of struct foo
he's just allocating the memory in the heap

struct foo * aFoo declares the variable aFoo which points to a struct foo. The function malloc(...) is requesting sizeof struct foo bytes of memory from the OS, where sizeof struct foo is the number of bytes necessary to represent a struct foo. malloc returns a void pointer, so we use (struct foo *) to turn it into a struct foo * that aFoo can store.

Fucked up the formatting:

C is ALWAYS pass-by-value. It never will take a pointer to your variable for you (excluding arrays, but they're special).
A pointer really is just an integer representing a memory address. The CPU can then follow that memory address to find some actual value.

C has a standard type for representing a pointer as an integer: intptr_t (it'll probably be 'long' or 'long long').
Maybe if we remove the pointer part, you'll see that it works exactly like an integer.
#include
#include

void foo(intptr_t lePointer) {
printf("%d\n", *(int *)lePointer);
}

int main() {
int a = 50;
intptr_t p = (intptr_t)&a;

foo(p);
}
We need to add a few extra casts to have it work properly, but this will output the EXACT same assembly code as the normal pointer version.

So basically, pointers are integers, and they're passed to functions in exactly the same way as integers.

How long until I can `some code` like reddit.

>Nobody pointed out the syntax error and useless cast
You fags probably don't even know C.

Syntax is subjective

the activity tracker for git is surprisingly a pretty good tool for keeping up with regular programming. gotta get those greens squares in

Is it a bad idea to pass structs to functions and receive structs back with struct assignment?

Usually, yes. It's very wasteful due to the copying. It's fine if the struct is very small, though.

depends on your what your other choices are of course

I'll never learn C; fuck my life

Only if the struct is

Yes

that's a github feature, it has nothing to do with the git version control system
it actually used to display your commit streak, but that was removed because it lead to unhealthy obsession with completeness, and one guy had a commit streak of nearly 1000 days, and then he complained that nobody would recognize it anymore.

Does anyone know of any good algorithms for pathfinding with some entities that have a higher priority than others, so the lower priority entities will get out of the way or busy wait or take a less optimal route so that the higher priority one arrives quickly?

>i thought when you called a function its parameter is just a copy of what you called it with

It is. lePointer and p are both pointers to an int that hold the same value

so when a function parameter accepts a pointer

foo(int *lePointer)

it actually does
lePointer = p
int case we call foo(p); ?

Yes.

Turns out dude is right, the cast is unnecessary (though some might argue it's good style.. maybe) and you apparently need parens for sizeof(struct foo). /shrug

Can someone explain to me why my timed loop is failing? I swear it was working yesterday.

I have a program running on an infinite loop that polls something else 60 times per second. Its not a game, but I thought since games do this I could just copy it. However I cant get a 60hz loop to work reliably. Sleep is terrible and it will sleep from 20 to 30 ms instead of 16~17.
So I tried clocks, but it still arbitrarily spikes to like 24. To the point where what should have been 10 seconds worth of frames are 12 seconds worth.
high_resolution_clock::time_point t1 = high_resolution_clock::now();
while (true) {
high_resolution_clock::time_point t2= high_resolution_clock::now();
double t3 = 1000 * duration_cast(t2- t1).count();
if(t3>=16.667){
t1=t2;
do stuff{}
}
}

I dont get why Im getting t3 to read 25 sometimes.

>though some might argue it's good style.. maybe
Only retarded sepplesfags do.

ok but why?

isn't it more logical that it should be *lePointer = p?
I need to read more on this I guess

for example:
int a = 5;
int b = 6;

int *p = &b;

foo1(a);
foo2(p);

..

foo1(int kek) //blabla
foo2(int *kek) //blabla


in foo1 kek = a
in foo2 *kek = *p ?
wtf
why not in foo2 *kek = p ?

You do not need parenthesis for sizeof, but it's good style

Because C is pass-by-value. IT WILL NEVER TAKE A POINTER TO YOUR VARIABLES FOR YOU.

When you're doing it for a type, you do.
Only for variables is it optional.

No

fuck this guy

Wrong

I mentioned earlier that arrays are an exception, but they have an interesting relationship with pointers, so you can argue either way that it's "taking a pointer for you" or not.

Truly, tooling, not languages, is the valid approach to memory correctness.

Dude wtf are you even saying. This is why I always declare int* p and not int *p. Because for some reason people cant understand that p is a pointer and *p is what the pointer points to.

rude

This

What's the minimum size (in bytes) that aes 256 cdc can encrypt

Friendly question: doens't passing by reference in C++ accomplish the same as passing the pointer value and changing it by dereferencing it?

Usually. CPU cache will eliminate any added dereferencing penalty with just sending a pointer. Struct can be copied by the function itself if needed.

Yes, but C++ references are garbage.
Invisible state changes are unacceptable.

Can you elaborate?

What does this C++ code print?
int a = 10;
my_fn(a);
std::cout

Not him but Im so tired of this shitty complaint. If something isnt doing what you expect follow the code.

The '*' in "int* p" is not an operator it is part of the type declaration. Basically the compiler will read the type first and then notice the '*' and realize it is a pointer to that type. When you use "*p" outside of a declaration it is an operator indicating you want to dereference the pointer p. So when you write "foo2(int *kek)" you are declaring an integer pointer variable called kek not dereferencing kek. When you call foo2(p) you are setting that pointer variable equal to whatever value you pass it.

>If something isnt doing what you expect follow the code
This misfeature deliberately makes it harder to follow the code, you shiteater.

What does this C++ code print?
int a = 10;
my_fn(&a);
std::cout

>You don't know regardless
Yes, but I want to keep stupid uncertainty to a minimum. When you see that &, you know that some silly shit may be going on, so you pay extra attention to it.
Having be the EXACT same syntax as a normal function call it just retarded, and only betrays expectations.

Easy, it either prints 10 or some value that can be determined by reading my_fn documentation. Where is my cookie?

That makes sense. Pass by const reference when not changing the value and pass by pointer when you are

Its not different than anything else. Its only a problem if you arent aware that pass by reference is a thing

struct bar { int x; };

void foo(bar & aBar) { aBar.x += 1; }

int main()
{
struct bar aBar;
aBar.x = 0;

foo(aBar);
}


Pretend you don't know the prototype of foo. You don't really have any way of knowing whether aBar is getting modified inside the function, especially if whoever wrote foo isn't disciplined with his usage of const. At least with C you know you're passing by refence from context, though you still don't really know if things get modified behind the scenes. Be disciplined with your consts, Sup Forumsuys!

>Pretend you don't know the prototype of foo.
Retarded assumption

True. It's that way mainly for constructor calls, but people misuse it. Just like they misuse pointers, unions, and just about any other feature in existence.

Not when you're skimming unfamiliar code.

>Pretend you don't know the prototype of foo.
you serious. f12 takes me there so easy.

I often call functions of which I can't access the prototype

What the fuck are you doing skimming code if the function prototypes aren't relevant but it's still a problem not knowing them. What kind of oxymoronic situation do you have in mind exactly.

>hovers mouse over function call
>prototype appears
magic!

this doesn't work in vim so I choose to ignore this post

>At least with C you know you're passing by refence from context
Why?

github.com/Valloric/YouCompleteMe
thank me later

I passed your mom around by reference.