/dpt/ - Daily Programming Thread

Previous Thread: What are you working on, Sup Forums?

Other urls found in this thread:

duramecho.com/ComputerInformation/WhyCPointers.html
twitter.com/SFWRedditVideos

First for Haskell

>anime
not technology

Can someone help explain the n-i part of random? n is size of deck.
I understand it's a better method of shuffling versus just producing a number from 1-52 every-time.
Just want a better understanding as to why that is.

//Shuffle 52 card deck
for (int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (n-i));
String temp = deck[r];
deck[r] = deck[i];
deck[i] = temp;
}

So i want to learn programming but i am not gonna have limited internet. Is there something i can dowload/buy that will show me a video and then have me try it myself?

Get a book.

K&R

Ask the Don

i + (int) (Math.random() * (n-i))

[Math.Random()] is a real number in the range [0, 1]

[(int) (Math.random() * (n-i))] is an integer in the range [0, n-i]

[i + (int) (Math.random() * (n-i))] is an integer in the range [i, n]

It generates an integer between i and n

Except that one is, it's Battle Programmer Shirase.

Shut the fuck up or I'll DOUBLE COMPILE your anus.

Can someone explain me pointers? i know it's to point to some other stored value but why?

duramecho.com/ComputerInformation/WhyCPointers.html

This explains it best imo.

So you don't have to copy the stored value around.

int a = 1;
addOne(&a);

void addOne(int *p){
*p +=1;
}


I actually took the time to copy paste my example for you

First for low-latency Javascript apps!

>apps
It's called programs.

I understand but why between i and n and not 0 and n

Why are some anime so badly drawn?

No, it's called cancer.

So is this to not create another extra value in the function? am i understanding that right?

Pointers let you refer to the same piece of memory by different names and also to pass around these names. Like when you order something from the web, you give them your shipping address, which is a pointer to your house.

Jesus no. This would be slower than just passing an int and returning the new int.

>low-latency
>javascript

Sort of yes, instead of copying the 'a' variable to the function you copy the address to the 'a' variable instead. When variables are bigger it's worth using pointers or preferably references

No shit retard

/dpt/-chan, daisuki~

Ask your much beloved programming literate anything (IAMA)

>not ++(*p)

>Definition: A pointer is a variable containing the address of another variable.
Wrong

>To do this one puts a ‘&’ infront of a variable to mean “give me the address of ”
Wrong

>them your shipping address, which is a pointer to your house.
Wrong

Thank you for using an anime image.

consider suicide

Playing with Processing.

Why is the probability you quote the wrong user in all of your posts so high?

pointers exist because your physical memory is divided into cells (blocks)
when a program executes some code, it needs an ADDRESS of the specific cells it's going to read data from in a memory array (your physical RAM or CPU cache or whatever)


think of memory like an apartment building IRL.
your data are the people living in it, and pointers are the address references for those people
(ex. apartment 205)

you cant just visit someone without knowing which apartment they live in first...

How do I declare an object member variable in a header file of a class, and then initialize its constructor outside of a member initialization list?

You don't, unless it's static.

>processing

Literally just vanilla java diluted for braindead "visual artists". Use proper tools like C/C++ and OpenGL, or you'll quickly hit walls with that crappy processing shit.

you're welcome

But its constructor throws exceptions! What do I do?

>u_int_2b_r_ntb_32_x64_sig = 2
lol c fags

>initialize its constructor

What did he mean by this?

That's exactly what function try-catch is for.

// Your class constructor
A::A()
: willthrow(arg) // Your throwing member ctor
try {

} catch(...) {
// Handle errors here
}

>"A pointer is a variable that contains the address of a variable" -ANSI K&R
Kys weeb

int *p = nullptr;
int *p = malloc(sizeof int);

nevermind that there's extra data too (size)

Oh, I didn't know you could actually put try catches inside member initalization lists. Thanks so much user, that's perfect.

Initialize the object using its constructor. Run the object's constructor.

still wrong.

>Oh, I didn't know you could actually put try catches inside member initalization lists. Thanks so much user, that's perfect.
It's not actually inside the list, it replaces the body of the function.

Instead of
T f() {}
You can write
T f() try{} catch () {}

A pointer does not contain size, you idiot. It's up to you to use it properly and not touch unallocated memory.

>A pointer does not contain size, you idiot.
The type does, dumbass

Wew

compile time

That's debatable. The following is valid, defined behavior, and the type doesn't include the size:
unsigned char* a = malloc(64);
printf("%d\n", a[1]); // The free may be arbitrarily far from the malloc, so no way to statically determine the size
free(a);

It defines the size of the type you get from dereferencing, not the size allocated

A pointer is SIMPLY A REFERENCE TO AN ADDRESS IN MEMORY.

sizeof( x ) is defined by your operating system and the architecture of your CPU...
for example sizeof( float ) will be different on 36-bit and 64-bit computers and such.

however if you do sizeof( * float ) (notice the pointer reference), it will always give you the size needed for allocating a single memory pointer, and not the size of bytes it takes to store the actual floating point variable.

Except because of aliasing, this is meaningless.
time_t* a = malloc(sizeof(time_t) * 18;)
unsigned char* b = (unsigned char*)a;
printf("%d\n", b[1]);
free(b);

*32-bit

I think the rationale is that the deck is being partitioned between shuffled and unshuffled, so that no card will be swapped back to the same position except the last one. It also means cards are shuffled progressively worse as you iterate through.

sorry, but contrary to Ritchie, i actually have studied PLT.

It's time to ban anime, everyone

Ok, I get what you're saying you nitpicking bastard. On a fundamental level pointers are just memory addresses. In the context of C, you also have the type you get when the pointer is dereferenced (except for void* which cannot be dereferenced).

Are you satisfied faggot?

>a reference to an address
No, that would be int**

>On a fundamental level pointers are just memory addresses
It's not nitpicking.
It's important, because otherwise, all pointers would be void pointers and they'd all be the same.

No, that would be int*&

I want to start learning how to make android apps since I know Java already.

I want to make an app with user accounts. How does one do that? Do I need to set up a server with a LAMP stack and then somehow connect the app to it or what? Kind of lost on this desu.

no

Interesting, I was thinking something similar.

>C

>I want to make an app with user accounts. How does one do that?
If you have to ask, I can already see the security vulnerabilities from here.

>C

False. A card can be swapped with itself if i == r.

If you're interested in the analysis of the algorithm, search for "Knuth shuffle analysis" and you can read the proper treatment which proves that it produces all n! permutations with uniform probability. You can probably also learn why 0 < r < n would produce a _non-uniform_ distribution.

>Javascripty

It's only for practice now. First figure this out then security. What vulnerabilities can you see?

>Deprecated

It's clearly a cancer to the brain

Recently I made some chef recipes that allow me to deploy a hadoop cluster on AWS EC2 with a single command and now I am able to practice distributed computing when I would want to and then destroy it when I am done with it.

Currently I am playing around with data processing with spark on this cluster.

I'm arguing that the type is only for the compiler to know the dereferenced value's memory layout. You could rewrite your code to use void* everywhere and cast it to the correct type everytime you dereference it, and the compiler would treat it equivalently.

>you nitpicking bastard
He's not. A pointer type is the combination of a type and an address; it's a typed address. The whole of point having pointers over mere addresses is the typing.

& doesn't return an address, this is a common error made by almost every C tutorial and even by the standard. No, what & actually does return is an pointer.

If you had learned assembly programming beforehand, you would have saw that on the first sight.

>skinning
>< 1.0

Wtf, I smell bullshit.

>It's important
It's not.
>all pointers would be void pointers and they'd all be the same.
They effectively are (practically all operating systems relies on that fact, and they even rely on function and data pointers being the same).

The JIT was able to use hot new SIMD instructions, the compiler targeted older CPUs for compatibility.

So they're cheating.

Yep.
Whenever there's a JIT vs AOT benchmark, people always omit -march=native.

webassembly still needs to be compiled, since it's not x86 asm.

Yes, it gets JITed, this is exactly what this benchmark is about ...

All interpreted languages should be disqualified unless their interpreter is bootstrapped in its own language, otherwise you could just submit the C++ version as v8 (that's your C++ source for the benchmark) + javascript code as just data to your C++ program.

I guess I'm a little confused, so why wouldn't SIMD instructions be used?

What's the point of the web?
Why don't social networks and wikipedia just make native apps?

If it costed just $1 or $2 advertisements would barely be useful.

Because then everybody would want to have a native app. That's what we wanted to prevent in the first place.

They do on mobile platforms.

C clang: 9.971914301 seconds time elapsed
C++ clang++: 5.518077517 seconds time elapsed
C++ G++: 4.659448453 seconds time elapsed
Dart: 2.585097637 seconds time elapsed
Javascript node.js: 10.406505448 seconds time elapsed
Javascript SpiderMonkey: 10.216595020 seconds time elapsed


v8 is a jit compiler.

The original codebase is in C++, it gets translated to JS/WASM through Emscripten.

WASM and asm.js are JITed by SpiderMonkey, firefox's JS engine, which is able to take advantage of SIMD instructions.

On the other hand when the C++ source is compiled with standard compiler options (-O2, no -march=native), the compiler has to assume the binary will run on older CPUs, and so it can't use all the latest SIMD instructions.
Sometimes, the compiler can build different versions of a function for different CPUs and use dynamic dispatch, but that's not done by default.

Since the benchmark was probably run on a recent CPU, the JIT has an advantage.

>v8 is a jit compiler.
That's my point.
If Javascript beats your C++ version?
Submit v8 as your C++ version and now they are at least equal.

Still working on my toy compiler.

oh, i'm stupid.

Yes, yes you are.

Wait... what went wrong...

the program to be benchmarked is still written in javascript.

Normies. As always.

No, they're written in C++.

No, the javascript code is just data to your C++ program, no different from example a lookup table.

All C++ programs should just be shipped as LLVM IR in the installer and codegen themselves on first use for maximum performance.

provemewrongprotipyoucant.jpg

I'm working on a calculator program and I'm having a weird glitch

for example if I enter the word "nigger" I get an error

If i use improper formatting "6+6" I get an error

If I put in a fuckhuge number or nonsensical characters "mogjswj939wgsg" "888888888" I get an error

But if I do pick related, I get an error. What can I do to fix this shit senpai? Heres my code for the input.


try
{

System.out.println("Seperate numbers with white space, such as 'A + B'");
System.out.println("Enter your calculation: ");

firstInput = in.nextInt();
operation = in.next(".").charAt(0);
secondInput = in.nextInt();

}
catch(Exception e)
{
System.out.println("Error: improper formatting detected.");

}

What went wrong is people wanting to put application-like functionality on a website, which resulted in the abomination we call webapps, and the heap of crap known as (((javascript))).

data that is used for jit compilation.

having something like

char *s = "function() { ... };"

compile(s).run();


doesn't change the fact that you wrote the program to be benchmark in javascript, you just embedded the source code into the hosting environment.