/dpt/ - Daily Programming Thread

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

Other urls found in this thread:

gnu.org/software/make/manual/html_node/Shell-Function.html
is.Sup
is2.Sup
twitter.com/SFWRedditImages

First for D

hi anons, i have a game server on python, im currently passing it to ctypes cause slow

but

i just figured, is linux faster than python? ( i think not cause ram) i mean, say instead of loading a bunch of classes and shit i just created temporary folders with data in it and used C programs to asses shit like "can USER move there?" "can USER attack there?" "how much DAMAGE did it make" etc?

would it be faster? like, operating in linux instead of operating withing python?

Or am i just being retarded?

(pls just answer even if its to call me a retard, don't make fun of me)

Operating with data files on the filesystem is going to be a lot slower than operating with classes etc loaded into memory whether you do it in python or in a shell script (which is what I assume you mean by operating in linux)

thank you a lot for giving me a straight answer and not an anime face, ask me for anythnig and i shall deliver

I'm a complete noob to C++, but can someone optimize or shorten this for me? Just want to see how short you can make a program and still make it run with C++.

// typecast.cpp -- forcing type changes
#include "stdafx.h"
#include "math.h"
#include
#include


int main()
{
using namespace std;
cout degrees;
cout > minutes;
cout > seconds;
const float conversionRatio = 60;
float decimalDegrees;
float decimalMinutes;
float decimalSeconds;
decimalSeconds = seconds / conversionRatio;
decimalMinutes = (decimalSeconds + minutes) / 60;
decimalDegrees = decimalMinutes + degrees;

cout

you're not gonna make it bruh

not C++ wizard, but input a string, split by "," and do the math in a single line

wont be faster but will look shorter

Is this messy code?

if ((time = (!i) ? sec % lmt[0] : sec % lmt[i] / lmt[i - 1]) ||
(!i && !iters))

Fucking shit AMD and their shitty drivers.
I was getting massive screen tearings on my app and I tried everything to fix it. After much work I found out that were AMD drivers that were causing the fucking tearing.

Anything with a tertiary operator is messy code, in my opinion.

*ternary

is this better?
#include "stdafx.h"
#include "math.h"
#include

int main()
{
using namespace std;
cout degrees;
cout > minutes;
cout > seconds;
const float r = 60; // Conversion Ratio
cout

best programming language for programming?

Ebin overflows senpai

this program is not feasible in the real world, try again

1. Remove stdafx.h. It's not portable.
2. Remove unnecessary calls to cin.get(); You aren't using the value from them. It just unnecessarily stalls the program when the ideal behavior is to return as quickly as possible to the shell that called it.

Like your waifu, the one language you cherish and love.

Why isn't my makefile recognizing the pkg-config bit?

main : main.cpp
g++ main.cpp $(pkg-config --cflags --libs sdl2) -o output

As written, the g++ line works fine in the command line, but in a makefile it reads as
g++ main.cpp -o output

What exactly does the $( ... ) do? Other time I saw it, it was for noting a variable OBJS = main.cpp and $(OBJS) at the target.

And this is how the man details using it in a Makefile:

program: program.c
cc program.c $(pkg-config --cflags --libs gnomeui)

$() is variable expansion in Make. I think you need to use the shell function?

gnu.org/software/make/manual/html_node/Shell-Function.html

gimme a simple example of the benefits of pointers in C

Try $(shell pkg-config --cflags --libs sdl2) instead for command subtitution. Also, it may be possible pkg-config returns nothing if the package is not installed.

What does this even mean?

teach me senpai, i'm doing questions out of c++ primer plus 6th edition.

is this better, or does it still overflow?
#include "stdafx.h"
#include

int main()
{
using namespace std;
cout degrees;
cout > minutes;
cout > seconds;
const short r = 60; // Conversion Ratio
cout

Adding shell did it, thanks.

Why doesn't the manual show that though?

The benefits of pointers in C are strictly speaking the same as the benefits of indirection in programmation in general. That is, you don't have to depend on fixed addresses to write algorithms, your algorithms work wherever its inputs are located in memory.

Fuck I hate everything about pkg-config and it's related autotools.

>Why doesn't the manual show that though?
If by manual, you mean "man make", it is the manual of the make program, but not of the Makefile specification.

Most arithmetic has the risk of overflow unless you sanitize the valid range of your variables. Also cin isn't safe from overflows either, so even the input can be read wrong.

no, I meant man pkg-config from here
That's the example of how to put pkg-config into a makefile given by the pkg-config manual.

is.Sup Forums.org/g/1484199873139.jpg
is2.Sup Forums.org/g/1484201701201.webm

>compile in visual basic
You mean Visual Studio? Also, I'm pretty sure there's a flag somewhere buried under mountains of graphical interface to turn on or off precompiled headers, which is what you need stdafx.h for.

>cin.get(); stops program from closing after it has finished it's calculations.
No, user, you don't get it. The program is supposed to stop. And when it stops, the command line that you called it from resumes control. If you did not call it from a command line, you are running it wrong. Console applications by definition are intended to be run within the context of a console. But nonetheless, since you are using Visual Studio... "start without debugging". And then get your ass a real compiler.

nice slowdown senpai.
Why are you making a gif-viewer in 2016+1?
Can you scroll the dir with keys?

okay, so how would I make sure it couldn't overflow? I haven't learnt if statements yet but would I use an if statement to end the program or display a message if data not in the range of it's intended variable is entered?

oh okay, sorry senpai, what do you recommend I use instead of visual studio?

More or less, except for getting the input from the user. There are functions that handle it, you can put it in your list of things to look into when you finish your book or whatever.

MinGW-w64, which is a proper port of GCC to Windows. What you learn from using it will translate seamlessly to every other platform. For your editor, use whatever you like. Notepad++ is a common choice for Windows users who don't want to learn anything new

>"start without debugging"
why? learning gdb as early on as possible is a blessing.

>Can you scroll the dir with keys
yes if you right click and select the option to slideshow with arrows

#include
#include
#include
#include

#define RATIO 60

static long str_to_long(const char *str)
{
char *end;

errno = 0;
long ret = strtol(str, &end, 10);

if (errno == ERANGE) {
fprintf(stderr ,"%sflow for '%s'\n",
ret == LONG_MAX ? "Over" : "Under", str);
exit(1);
}

if (end == str || *end != '\0') {
fprintf(stderr, "Invalid number '%s'\n", str);
exit(1);
}

return ret;
}

int main(int argc, char *argv[])
{
if (argc != 4) {
fprintf(stderr, "usage: %s \n", argv[0]);
return 1;
}

long degrees = str_to_long(argv[1]);
long minutes = str_to_long(argv[2]);
long seconds = str_to_long(argv[3]);

seconds /= RATIO;
if (seconds > LONG_MAX - minutes)
goto overflow;

long tmp = seconds + minutes;

tmp /= RATIO;
if (tmp > LONG_MAX - degrees)
goto overflow;

printf("%ld\n", tmp + degrees);

return 0;

overflow:
fprintf(stderr, "Calculation overflowed\n");
return 1;
}

friendly reminder that the only way to really understand programming is to manufacture a computer from scratch

I've written a simple microprocessor (to run on an FPGA) in Verilog before, as part of a university assignment.
Does that count?

no you have to mine your own silicone ore

I will do, hopefully the book touches on it later anyway.

Thanks, would it better for me to download a Linux kernel like arch instead?

thanks, I can't understand most of that lol

maybe not the only one, but surely one of the quickest ways

It's a Visual Studio thing to keep the output window from destroying itself.

>silicone ore

...rocks?

But the paper mentioned how all of that shit is done.
Silicon doping, transistor logic, and all that.

Programming on Linux is less of a pain in the ass as on Windows, but it isn't wholly necessary. There's not too much difference between using GCC on Windows and using GCC on Linux (though any other supporting tools on Windows will make you want to tear your hair out). If you would nonetheless like to work in Linux, I would recommend using an easier distro than Arch. Ubuntu, OpenSuSE, Fedora, Mint... these all work rather fine for beginners and have excellent package managers for getting the tools that you need. It is even possible to develop Windows applications on Linux by getting the MinGW-w64 cross compiler. Testing is as simple as running Wine on the application. Though not every Windows application works perfectly in Wine, almost every application that works in Wine works on Windows... with a few odd exceptions of some really old applications that no longer work in compatibility mode on Windows.

>silicone
Fucking idiot. We aren't making fake tits here.

>got hired
>got to work with Plone and shit
>currently being fired
>got to explain the system to newbie
>mfw I have no clue how that shit works
It seems I'm just a shit programmer, then.

With all of the traps lurking around here... we might as well.

>all of the traps
There would be 2 on here at most. They are just very vocal.
I wish they would shut up and stop pushing their stupid forced meme, like they have been for the last several years.

Can they actually program though?

Probably not.

Kinda wonder why they come here of all places to attention whore... and why they don't put on a fucking trip.

Can you give an example piece of code?

1. Allocate buffers of arbitrary amounts of memory without the possibility of stack overflow or trashing a register to keep track of the length of a variable length array.

2. Outputting variables from a function while also returning a status code (pass variable as pointer argument, set it from the function).

3. Passing large data structures around without copying memory.

4. Any linked data structure (trees, graphs, linked lists).

5. Anything involving strings.

Any non-trivial piece of C code will use pointers extensively.
They are absolutely fundamental to the way C works.

It's kind of hard to come up with an example, because I could post practically anything.
Here is the most trivial example I can think of:
int n;
scanf("%d", &n);
scanf couldn't work unless you give it a pointer to an existing memory location.

this do?

int a[3];
for(int i=0; i

I get that, I mean &n obviously means "into the memory address of n"
What I'm confused about are variables that begin with *

Mostly pointer math. Just look up linked list double pointers.

It's also used to easily break the type system since any type of pointer can be cast to any type. That way you can easily write your own allocators.

It's what they did for the curiosity rover navigation system since it doesn't have any memory protection or virtual addresses.

They allocated a pool of memory and used placement new.

"the memory address of n" is of type int*.

Think about it from the function's perspective:
#include

void set_int(int *ptr)
{
*ptr = 2;
}

int main()
{
int n = 10;
printf("%d\n", n);

set_int(&n);
printf("%d\n", n);
}
It allows you to modify local variables in other functions.

In my examples, 'ptr' refers to the pointer itself, and '*ptr' refers to the value that the pointer is pointing at.

this makes much sense, thanks

having pointers to functions and using them as callbacks is a neat thing too

why static/heap memory tho?

>static
The function has no external linkage, so it should be declared as static.
For a trivial program, it's not really important though.
>heap memory
My program didn't make use of the heap.

A while ago, I found myself regularly having a section of code where I would fprintf some error message at stdout, and then exit with a failure status. Easiest to condense it into one function. It's a little cleaner than a goto fail kinda thing.

#include
#include
#include
#include

/* Exit program with error message */
noreturn void bail(const char *fmt, ...)
{
va_list args;

va_start(args, fmt);
fprintf(stderr, fmt, args);
va_end(args);
abort();
}

Could you please stop tripfagging, bro?

No thank you.

u go to uoft?

No.

Yes, 'die' functions are nice and all, but I couldn't be bothered with that for my example.
I can't wait until C2X and getting __VA_OPT__ (assuming it gets through, but it's likely), making these sorts of functions much nicer to use.

how many programs do you have to write to become a programmer

Technically, one.

>silicone ore

>silicone
>not graphite

What lang + framework to use to develop mobile applications?
I know python and kivy but afraid of the performance.
I know java and javafx but it has poor performance. you can't style stuff easily in java and basically the documents say you should write css and then apply the css class for your objects which is fucking retarded IMO.
I could go with web apps but I could as well just kill myself now.

(C and NOT A) or (B and NOT A)

Can this be shortened? My brain has shut down for the night.

(c or b) and a

¬A ^ (B ∨ C)

Yes that worked thanks.

suppose i am to implement list that keep its elements sorted
what sorting algorithm is best for this? everytime an element is added to the list, i should run this sorting algorith,

If it's a list of n elements and it's already ordered, inserting in order should be O(n) at worst, no need to sort the list again after insertion.

Does it have to be a linked list? It's extremely easy to keep a binary tree sorted as you go.
>everytime an element is added to the list, i should run this sorting algorith,
You don't even need a sophisticated sorting algorithm for this. You just need to do a linear search through the list, and then insert the element at the correct place.
Unfortunately for a linked list, there isn't really a more efficient way to do this, unless you're trying to do it all up front.

This is actually for a wrapper class. It can be backed by array or linked list.
Isn't insertion sort too slow for this? I'm thinking something like how binary search looks for elements. Check middle, if less than, check left, etc etc.. until the insertion point is found.

>take a bunch of programming courses in school
>proudest achievement was a working version of pong
>drop out for a year
>don't write any code during that time
>don't remember the exact syntax involved to write hello world anymore in java
>remember after a few hours of failure and despair everything is a fucking class andI've been trying to do it like I would in C this whole time
this language is stupid

>I'm thinking something like how binary search looks for elements
You can't binary search a linked list, because you don't have random access.

me again, of course for linked list, i have no choice but to crawl from index 0 until the insertion point is found

>Graphite
>Not titanium trisulfide

I have a Java job interview in two hours.

What buzzwords should I drop? Already prepared Spring, Maven and Hibernate.

design patterns
model-view-controller
service-oriented-architecture
WSDL
Java 8
Lambdas
Streams

I'd rather become a beggar than work as a java programmer.

Thx

Stay a Haskell NEET then

I just wrote my first simple program in c++ and I feel like I'm on top of the world.

Must read programming books for beginners?
Thanks Sup Forums

>reading programming books

The functional stuff in Java 8 is pretty good.
Mention how you can now do method chaining without getting fucked over by nulls due to monads (Optional being the main hero here), which lets you write much better looking and easy to understand code.

That at least makes you sound like you have an interest in the development Java goes in.

It's like Oracle wants a language with the flexibility of Common Lisp and the type safety of Haskell. They just don't care enough to get it done.

>not just watching YouTube programming tutorials