/dpt/ - Daily Programming Thread

What are you working on?
Previous:

Attached: 1393087963461.jpg (400x468, 85K)

Other urls found in this thread:

stackoverflow.com/questions/671790/how-does-sizeofarray-work?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
en.cppreference.com/w/cpp/language/fold
dstsrc.net/2018/04/16/switch-encoded-tries.html
dotnetperls.com/stringbuilder
news.ycombinator.com/item?id=16735011
twitter.com/SFWRedditImages

Is SICP worth reading if you're not a beginner

queers

A lot of code monkeys think that they're not beginners.

Insert shilling for Ruby.

1. Do you want to learn scheme?
That is L I T E R A L L Y the only question you need to ask.
And the answer is no.

Yes.

Is it good style in C++ to explicitly use the namespace of a function when you call it inside a function inside the same namespace?

well I did and I still get the same error

Attached: 1523892238775.jpg (225x225, 7K)

>finally decide to bite the bullet and learn Javascript so I can get rich working on startups
>do some intro courses on the language. It's alright I guess, and the syntax is familiar
>Finally sit my ass down and start listening to Douglas Crockford's lectures on it

Jesus Christ. What a mess.

No.

If it avoids confusion and makes your intent clearer.

How can JS be so slow? Shouldn't the JIT compiler basically be able to optimize it as much as native code given runtime information?

Not really.
But if you are autistic like that, you might as well use a fully qualified symbol that begins with a prefix ::

>A lot of code monkeys think that they're not beginners
When do you stop feeling like a code monkey?
The more I read, the more I learn, the more like a monkey I feel

The more of a monkey you realize you are, the less of a monkey you have truely become.

Thank you for being honest m8

thank you for using an anime image.

yes

>Is it good style in C++
no

On the topic of references in Rust, they're actually quite solid. &mut should be read as "non-atomically mutable", and & should be read as "atomically mutable". That explains how Rust can get away with interior mutability, because it must always behave atomically, whether it's through a mutex, hardware atomic instructions, etc. The only problem is that Rust has only these coarse grained permissions that are also tied to ownership of the backing memory.

The key to my type system is typestate expressed through labeled transition systems. Transitions are atomic actions, permissions are essentially sets of labels (the process may make this transition, the process must make this transition, the process may not make this transition), and can be split up and combined with fine granularity.

That's good, it means you're on your way down the far side of Mount Stupid.

I kind of like being a monkey. I can't find an emplyoyer who needs a monkey, though.

daily reminder that an array is a pointer to a section of memory, plus of offset of the index.

int[6] === [*,0][1][2][3][4][5]

Daily reminder that code monkeys can't into abstractions. That's the best way to spot them.

An OOPL with lots of popular libraries that get used in most of "serious" code.

>Shouldn't the JIT compiler basically be able to optimize it as much as native code given runtime information?
Optimization is a computationally hard problem, and detailed instrumentation is also not free. If the JIT is too eager, it can end up incurring enough overhead to effectively cancel out any optimizations.

A JIT tries to compile code as fast as possible. So you'll never reach the performance of code compiled with -march=native. It does try to optimize hot paths though.

>feel motivated to do some coding
>start messing around a bit
>want to make a discord bot that gives you information about your favorite tv shows
>get the first few commands going
>decide to look up if something along those lines already exists and how well thought out it is
>already a huge tv bot available where it seems people are spending a ton of time developing it

into the trash it goes I guess, I dont really see the point of developing stuff in my free time anymore now that I am out of university. only things I code are little python scripts for myself but that is only to save myself time. I really wanna have a nice little project but when you look around the internet you either cant get enough data for free so your idea is not doable or someone has already built something similar.

Well I guess programming is gonna be limited to my working hours for the forseeable future because no matter which project I come up with it has either been done or the scope would be so massive you would need ages or more than 1 person to build it.

Attached: stress-free-programming-frustration.jpg (640x256, 12K)

>Daily reminder that code monkeys can't into abstractions.
daily reminder that code monkeys can't into underlying systems.
You can't call yourself a car mechanic if you don't know what an engine is, retard.

By your logic, assembly languages are the code monkeys' favourite. Which they are not.

The array isn't the pointer itself, dumbass. You have the array in memory, and a pointer to the array so you know where it is.

You really fail to understand anything. Where did I say that people using assembly languages lack abstraction?

I know that feel.
I fucking hate it when i find out someone already came up with an idea i had, and did it better than i ever could.

having a read of c99

Attached: Capture.png (589x165, 22K)

Also, JS just has a lot of runtime overhead by design that can't be avoided, even by a very smart compiler. Between dynamic type checking and garbage collection, you already account for a big chunk of the performance gap.

>You have the array in memory, and a pointer to the array so you know where it is.
and what is the array in memory dipshit?
a bunch of registers, referenced BY A FUCKING POINTER, and an offset, like i fucking said.

You are not your name. Your house is not its civic address.

Your mistake is doing work before researching.

how does c know when an array is out of bounds if it just does *(a+b)
at least with strings there's a null terminator

Then how come sizeof can give me the size of the array?

If you are talking about C arrays are not pointers
Arrays decay to pointers when you pass them to functions for example
But they are not
>proof
Sizeof of an array is different from a pointer
Also char arrays in stack are modifiable while char pointers in stack segfault

It doesn't.

That's the fun part: C doesn't know. Enjoy your buffer overflows.

It doesn't. It's undefined behaviour and anything could happen. Often an out of bounds access turns into an unauthorized access that your kernel responds to by crashing your program, but that's not guaranteed.

It doesn't
Actually as long as you are in the same scope you can get the size with sizeof
But once you leave the scope the array decays to a pointer and you can't find the size anymore

>Also char arrays in stack are modifiable while char pointers in stack segfault
>But once you leave the scope the array decays to a pointer and you can't find the size anymore

Attached: 1522280161860.jpg (481x523, 49K)

it's not even that I couldnt do it but it is just a pretty big time investment and I dont have that much time, it's not like you could earn any money with it currently anyway so it seems pointless to spend so much time on it.

yeah but it really was just for the fun and everytime I did the research beforehand I found that it already exists and threw the idea away before even starting. so all in all I am happy that I just started this time even if I am gonna throw it away anyway but atleast it was fun until the bubble popped.

If you're happy with the time you spent (which wasn't clear from the first post) then what's the problem? That said, if you want to just make something for yourself, then you shouldn't get hung up on what has already been done.

how does one cope with programming induced insomnia, exhaustion

I guess what frustrates me is that it seems pretty much impossible to build something meaningfull on your own unless you want to commit years to it (or dont have a job and can go full time).

maybe I should try and find likeminded programmers to start a project with instead... because working on your own ultimately feels pointless because either the scope of the idea is too big for one person or it was already done.

>it's hard to do things
Wow never thought about dat

With female companion

An array is a contiguous range of memory. It isn't a pointer to that memory, it is that fucking memory.
int a; // Just an int.
int b[21]; // Statically allocates a 21-int-wide range of memory, in-place, inside this stack frame,
// probably between a and c.
int *c; // Statically allocates an address. It doesn't necessarily point into this stack frame.
// In fact, it doesn't necessarily point anywhere useful at all.
assert(sizeof b == 21*sizeof(int));
assert(sizeof c == sizeof(int *));
assert(21*sizeof(int) != sizeof(int *)); // This last assertion is technically not guaranteed,
// but it's very unlikely to fail. Why would any architecture need 21-word addresses?

An array can be used almost exactly like its own address, and decays into its own address whenever it's passed out of its declared scope, but it is not an address.

sizeof calculates the bytes per word * the offset from the pointer which was defined at compile time.
int[10]
would be a size of 4*10.
stackoverflow.com/questions/671790/how-does-sizeofarray-work?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

>"Ok, final question and you are hired"
>"Write a program in C that calculate the average of two ints"

Attached: 1523820608940.jpg (1024x683, 200K)

Today JS is fast but DOM manipulation and draw process are slow.

if all the smaller projects have already been done by someone and there is no money in it there is literally no reason to invest time in it. I am not gonna start a huge project for no return, I could live with putting in a few hours and building something small. but trying to outdo existing solutions is way too time intensive for working people.

I just wonder who does these big non-profit projects anyway, maybe I am just too selfish to understand it.

inline double avg(int a, int b) {
return a*0.5 + b*0.5;
}

auto fun = [val](auto... i) { return (... || (i == val)); };

what the fuck does that even mean

int average(int a,int b){
return ((a + b)) + (((a&1)+ (b&1)) >> 1);
}

kek kekky

fun is a function object that can be invoked with a variable number of parameters and will return whether any one of them is equal to val.

int a[2][5] = {};
int **b = a;

why is this giving me an incompatible pointer warning they are the same types?

en.cppreference.com/w/cpp/language/fold

i didn't say that wasn't the case.
An array is a pointer to the start of that segment of memory, and the index refers to the address of element 0 (where the pointer is) plus the offset. In other words indirect addressing.
In other words, an array is just a pointer referencing a starting address, and an offset of the index/address past that point referenced.

return a + (b-a)/2.0;

Because arrays are not pointers.

wrong, otherwise the index would start at 1.

because arrays are indirect references (like ponters) while pointers are just direct references.
Why would you reference a reference to a reference with a reference?

>So you'll never reach the performance of code compiled with -march=native
to be fair, neither will most C/C++ software since much of it is distributed as prebuilt binaries. technically it's possible for JIT compilers to (presumably in localized/isolated cases) generate faster assembly than an analogous portion of a C/C++ assembly built to target a general architecture, since they know the set of CPU instructions/extensions for the host machine and can generate assembly that uses them without runtime checks

wow c++ sure is weird. kinda neat though. thanks.

>arrays are indirect references (like ponters) while pointers are just direct references

Attached: 1523386635027.png (645x773, 23K)

Well, no, an array is not just a pointer referencing ***A*** starting address and an offset of the index/address past that point referenced.
An array has a very SPECIFIC starting address.
The starting address of an array is, necessarily and in all cases (excluding arrays as parameters) the address where the array was declared.
This distinguishes it from a pointer, whose "starting address" can be anywhere.

>+ (((a&1)+ (b&1)) >> 1)
or just + a&1&b retard

you haven't seen nothing
struct Lol
{
Lol() = delete;
};

Lol bar{};


> this compiles

#include
using namespace std;

double avg(double a, double b){ return ((a+b)/2); }

int main(){
double c = avg(3, 4);
cout

the index is the starting address + the offset. So referencing array[0] would just be the starting address, and array[2] would be the starting address plus and offset of 2.

hot tbqh

Testing out signals. Made a sample segfault handler.

#include
#include
#include
#include

static jmp_buf backup;

void handler(int num, siginfo_t *si, void *someshit)
{
longjmp(backup, 1);
}

int main()
{
struct sigaction sa;
int someval, *ptr;

ptr = nullptr;
/* ptr = &someval; */

memset(&sa, 0, sizeof(sa));
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
sigaction(SIGSEGV, &sa, NULL);

if (setjmp(backup))
goto whoops;

*ptr = 37;
printf("Success\n");

return 0;

whoops:
printf("There was a segfault\n");
return 1;
}

oh, i see.
So then an array is just a partitioned segment of memory referenced by a default starting address rather than a poiinter, while a pointer is an explicit reference to an address, which can be changed?

Serious question here:

I have been programming (mostly video games, physics simulations, random tidbits that assist me in specific tasks) for 12 years. I have no formal training or education, but I find I can learn to work in almost any software environment as long as there is decent documentation. I love coding.
>For some fucking reason i studied chemistry and now teach high school science
What are the odds that a company would hire me for a coding position (with at least 45k salary)?

Pic unrelated

Attached: 20170703_175008.png (326x376, 115K)

>in C
>two ints

int main()
{
struct sigaction sa;
int someval;
int *ptr = nullptr;
/* ptr = &someval; */

memset(&sa, 0, sizeof(sa));
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
sigaction(SIGSEGV, &sa, NULL);

if (setjmp(backup)) {
puts("There was a segfault");
return 1;
}

*ptr = 37;
puts("Success");

return 0;
}

where are you from?

if USA, you can get $100k+ very easily

whoops lemmie correct that.
#include
using namespace std;

double avg(int a, int b){ return (double(a+b)/2); }

int main(){
double c = avg(3, 4);
cout

you are confusing everything. [] is an operator that works only on pointers. an array is implicitly converted into a pointer to &array[0] when not used with sizeof or with the & operator.

int arr[16];
int *a = &arrr; // type error

Why not try programming chemistry things tho

It's a bretty good field, a buddy of mine who works in chemoinformatics, has formal chemistry training and can code, companies seem to love him a bloody lot. A programmer who actually understands the domain field he is in, anticodemonkey of sorts

The compiler will actually replace printf will puts actually if there is no format.

count = 1;
while(true){
count++;
if(count>10)
break;}

C# Question: Why doesn't string.Replace(string oldValue, string newValue) alter the string that it is called on? I have to assign the result.

how do i program a game as good as celeste

Yes im in the US, Alabama to be specific. Feel a little sheepish since I dont have any "formal" experience and never learned a lot of the lingo. How would i go about finding an employer without lookinh like a total fucking idiot?

dstsrc.net/2018/04/16/switch-encoded-tries.html

it's more about assets and art than programming

C# and Java to avoid a lot errors in special concurrency, make String inmutable.

dotnetperls.com/stringbuilder
If you want mutable string use string builder.

I would love to. Even better, physics. My absolute dream job is to design and code virtual labs for physics students - preferably in VR

Build good portafolio, explain your project and code to people.

but
int *a = &arr[0] does work.
why is that?

there are tons of websites with jobs

check this for example

news.ycombinator.com/item?id=16735011

experience/ portfolio > degree

also check Linkedin,

At arr[0] you get teh value at index 0. &arr[0] is the address of the value at index 0.

arr[0] is of type int
&arr[0] is of type pointer to int

Moat PLs make string immutable though