/dpt/ - Daily Programming Thread

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

Other urls found in this thread:

doc.scrapy.org/en/latest/intro/install.html
twitter.com/NSFWRedditGif

C is deprecated

Currently trying to come up with schemes to "rank" stuff, like shows or books based on someone's preferences.
Anyone know of good resources to learn about different algorithms for this ranking problem?

Fight me

Viva la diversity! who here /diverse/?

Unsupervised machine learnig

>tfw can't into complex numbers

C is just the quotient field R[x]/(x^2 + 1), what's the problem?

How would I check if a string input is two space-separated numbers in C++? Regex? I've never learnt it before.

How do I print the contents of a text file with C++?

std::ifstream in(path);
std::cout

Thanks user.

I want to use scrapy but it's a pain in the ass on Windows. Using pip install scrapy doesn't work so I've looked it up and have downloaded this thing called Anaconda which apparently will make it work on Windows. I've done pip install scrapy through the Anaconda command prompt program and it all seemed to install but when I open IDLE and type import scrapy I get a ModuleNotFoundError. Any ideas, /dpt/?

anaconda sets up its own environment, so you want to use conda and not global pip. read
doc.scrapy.org/en/latest/intro/install.html

c++ is the greatest language of all time

Nuked contribution on github or hacker rank problems as viable CV.

Rust will eventually depreciate C++

My language will be the greatest language of all time when I finally get around

TypeScript will save systems programming

Ah, hold on one sec, I did do just that. I did it in the Anaconda console using the conda command supplied in that how-to article you linked. Still, no dice.

I'll restart my PC and see if something works then.

...

...

That's it. I am a C++ guy now.

What's a good way of doing C++ user console IO?
At the moment I've just got a string for in then loads of if/statements checking what to do next. Is that the best way of doing it?

How do I set something in java to the garbage can so it gets deleted by the GC?

That's done automatically

Just install C++.

That's it. I'm a Rust guy now.

how the GC tells apart something that needs to be deleted from something that don't?

Because the GC will delete the object when you no longer will use it.

Go away Bjarne!

If no references to the object that are reachable from the call stack exist, then the object is garbage.
You can sometimes help the GC by explicitly nulling out a field. You should be particularly aware if this if you have any static fields, because otherwise they'll never get collected.

don't worry about it, just leave it on the flor and mommy will pick it up next time she cleans your room

>tfw no mommy gc

That's not Bjarne, that's Herb Sutter.

so basically I just set the object to null, right?

Set it to null, overwrite it with a reference to some other object, or just wait for it to go out of scope.

>tfw no GC (manual)

ok, so I asked what happens if I have a quadtree with moving stuff on SO and they didn't answer.

do I need to rebuild the entire quadtree every frame?

A CPU L1d/i cache can hold maybe 64K of memory.
What is the algorithm for deciding what to evict when pulling something new in?

What do you mean exactly?

If you want to call a garbage collection run manually, I know you can do System.gc() in Java.
If you're in C/C++ you could maybe build up a deallocation list manually instead of freeing/deleting constantly and then go through it when it is full.

>building your own free list to put into the real free list

How the fuck do these "memory cleaners" work?

Surely if it was fine to retrieve memory the Os would have already reclaimed it? Or are they running through applications, doing their own backyard reference counting and hoping they don't mess anything up?

I don't understand your point.

That was a joke, ma'am.

I lost a fucking hour because I forgot the fucking internet permission.

it's snake oil
at best they terminate bloat windows processes that don't do anything useful

Just finished making a backgammon program in Java (PvP only for now)

They might kill some useless processes and maybe flush cache, or they might not do anything at all.

Can you work with general iterators in C++?
For instance is it possible to just give a function any two iterators and check if one against the other? Or do you have to explicitly state the type data structure the iterator is related to?

The idea is not a joke. It can take a significant amount of time to call free/delete. If you do it often it can be a bottleneck. It is possible that you have some places in your program where it would be timed better to free at that point, such as when a menu screen is opened in a video game.

When a program allocates memory and then frees it, it may not be given back to the OS immediately. Allocating and freeing memory with the OS can be a slow process, which a motivation for this behavior.
Some of these programs can probably force the OS to go reclaim what they can, as well as flush any cached files. The last thingy is that the drive that your files are on is pretty slow, even if it is an SSD. Recently used files will be replicated in spare RAM to speed things up when those files are needed again.

tl;dr it's mostly bullshit and you should not use such programs.

Hard to say really, the details of such a thing would be kept under lock and key for competitive advantage. A simple cache residency algorithm would be to base the eviction priority on infrequency of access as well as the amount of time since the last access.

LRU

dude templates lmao
dude type inference lmao

So, lets say I learn one of these languages, how would I go about getting a job? Wont most places want you to have a bachelor in CS?

>contribution on github

Always was curious, how do you find some beginner level projects on github and contribute to those?

>The idea is not a joke. It can take a significant amount of time to call free/delete. If you do it often it can be a bottleneck. It is possible that you have some places in your program where it would be timed better to free at that point, such as when a menu screen is opened in a video game.
Sure, if you're using a general purpose heap allocator. But if those are calls to the GC (where delete simply gets rid of a reference), it would be just as fast as having them implicit based on liveness. Of course the joke is that implicit based on liveness is optimal as well as much more convenient.

It was a pun on GF (male), inspired me. Fuck you for making me explain this.

It is possible but you would have to implement it yourself. It would have a significant runtime overhead. If I remember my benchmarks right when I did this, it 2-3x slower.
The usual solution is to use templates, or an actual Iterator design pattern, or if the range you expect must be contiguous, use gsl::span.

>Wont most places want you to have a bachelor in CS?
no, they don't care
they all say "BS or equivalent experience"
which, when translated, means "don't fucking suck"

what's wrong with developers?

I have no clue what to do here
this is terrible but I don't know what else to do
window *w = malloc(4 * sizeof(window));
w->title = "Menu";
w->size = (SDL_Rect){821, 128, 256, 256};
w->labeltxt = malloc(8 * sizeof(char*));
w->labeltxt[0] = "Item 1";
w->labeltxt[1] = "Item 2";
w->x = malloc(8 * sizeof(short)), w->y = malloc(8 * sizeof(short));
w->x[0] = 850, w->x[1] = 970;
w->y[0] = 140, w->y[1] = 140;
w->labels = malloc(8 * sizeof(enum label));
w->labels[0] = INFO, w->labels[1] = INV;

where the struct is
typedef struct{
char *title;
SDL_Rect size;
char **labeltxt;
short *x, *y;
enum label *labels;
} window;

it keeps giving me segfaults and dumping the memory map

So I know/can learn the computer science behind programming.
I can learn what pointers are, what arrays are, when to use which, what data structures are, what a linked list is, how to optimize a program, how to abstract functions, etc etc

how do I actually bridge the gap between theoretical knowledge to writing actually useful programs, like a text editor, terminal emulator, or a file browser?

>which, when translated, means "don't fucking suck"

How do you go about not fucking sucking?

to clarify I free each thing I malloc after use
the problem occurs when I malloc again

C developers are fucking insane, the amount of compiler specific code, importing fucktons of libraries to do basic shit, custom build solutions, maintaining backwards compatible with ancient Linux versions for the sake of portability despite Linux not being a target platform, etc I see on a daily basis is enough to drive anybody insane.

Is dynamic memory allocation even required for window? What exactly are you trying to do?

practice enough until you're at the point where you can sit down and write simple programs without having to refer to the internet or other documentation more than once

>w->size = (SDL_Rect){821, 128, 256, 256};
you are treating the size member as an array.
>w->labeltxt[0] = "Item 1";
>w->labeltxt[1] = "Item 2";
no allocated memory for the labeltxt member
not sure if i missed anything else, im a beginner myself.

the window contains label which can be a different amount of labels each time I call the function that returns a pointer to the window

disregard the labeltxt thing, i suck cocks.

>you are treating the size member as an array.
it's an initializer list
it's pretty much {.x = 821, .y = 128, .w = 256, .h = 256}

The thing that's always tripped me up in programming is the scope of variables between functions

I'm trying again and am trying to write good code- but it always ends up with me just nesting functions cause i cant pass variables

what the fuck can i do to learn this? i guess practice eh

There's really no reason to use C unless you're forced to.

Read SICP

Got any specific examples?

how could nesting functions be simpler for a beginner than passing variables
are you learning in haskell or something?

pardon my ignorance, but since that member is not an array, wouldn't the correct be
>w[0]->size = 821;
>w[1]->size = 128;
>w[2]->size = 256;
>w[3]->size = 256;
?

I'm seriously just trying to read a list from a txt file called wallet with 3 integers
this function is read_wallet()
so i'm parsing wallet by the commas into wallet[0], wallet[1], wallet[2], and then trying to return these to write_wallet(wallet) which will update the contents, but then when i say wallet[0] += 5 it says undefined variable wallet

size if of type SDL_Rect which is a struct that contains 4 ints x,y,w,h
I cast the list as a SDL_Rect so it size get a SDL_Rect

post dat code yo

Could you post the actual code? Show us how you're trying to pass it?

aight one sec im on the bus home from uni

Just went through the trouble of openong the thread to tell you that you are an idiot that confuses languages with libraries,frameworks, operating systems that are not the fault of the language

Why are you using malloc? And why are y and x pointers?

that makes
>w[1]->size;
>w[2]->size;
>w[3]->size;
undefined.

What are labeltxt, x, y, and labels supposed to be?

I want y and x to be arrays that are the same size as labels/labeltxt
labeltxt is the collection of strings on a window
x and y are where the string will show up
and label is the 'type' of label which is an enum
whoops, didn't mean to make an array out of them
but I never try to access them so that shouldn't be the problem

pretty much I want labeltxt, x, y, labels to contain the same amount of elements, but I want window to have the option to have nth amount of labels

>labeltxt is the collection of strings on a window
>x and y are where the string will show up
>and label is the 'type' of label which is an enum
This just screams at me that you should be using a struct for these variables, and making a single array of this struct instead of 4 different arrays.

Also, how are you keeping track of the number of labels in the window?

>char **labeltxt;
does it need to be a pointer to pointer, user? make that *labeltxt instead and see if it works.

yeah I thought of that after I made my last post
struct label{
char *labeltxt;
short x, y;
enum labeltype lt;
};

also changed the names abit

ur mom is deprecated

I think you need to rethink the problem.
So we have a window anyway, right? There's only ever one SDL_Window.

struct Text {
SDL_Rect size;
char *text;
};

struct Text text[MAX];

Do you really care about the type of label or its name?
It all should be on the one window anyway. There's no need for malloc just because you want it to be dynamic.

Then just run a for loop through the render function.

Linear types will save systems programming

not that kind of window
I'm going for pic related
the one on top

how come?

just rewrote it with the struct label but it never fixed my initial problem

what's happening is I'm malloc and then I free, that works fine, and then I malloc again and then it segfaults or it works but when I go to free it segfaults

def __init__():
self.wallet

def read_wallet(self):
with open("wallet.txt", 'r') as wallet_file:
contents = wallet_file.read().split
one = int(contents[0].strip("["))
five = int(contents[1])
ten = int(contents[2].strip(']\n'))
self.wallet = [one, five, ten]
return(wallet)

def write_wallet(wallet):
ones = input('ones?')
fives = input('fives?')
tens = input('tens?')
wallet[0] += ones
wallet[1] += fives
wallet[2] += tens
print(wallet)

NameError: name 'wallet' is not defined


But I'm returning it!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Maybe he has no imagination.

Can you post the relevant code exactly where it crashes? I can't see the problem from what you posted so far

whitespace error dumb dumb