[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
/dpt/ - Daily Programming Thread
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 255
Thread images: 20
File: 1463210410094.png (1 MB, 1000x1400) Image search: [Google]
1463210410094.png
1 MB, 1000x1400
Old thread: >>55012631

What are you working on, /g/?
>>
>3 posts early

consider completing sudoku
>>
File: keanu trapped.jpg (283 KB, 1277x1065) Image search: [Google]
keanu trapped.jpg
283 KB, 1277x1065
>>55019779
>traps
>>
First for Java
>>
Hime back in the OP!
>>
>>55019779
Batch import script for rtorrent, since I'm moving from an ancient version of uTorrent.
>>
File: 1446861464355.jpg (101 KB, 401x480) Image search: [Google]
1446861464355.jpg
101 KB, 401x480
Giving freelancer a shot.
Also if anyone wants me to make something I'll do it in exchange for 15 bucks or 1 billion ISK in eve online :}
Totally serious.
>>
>>55019844
Make me a sandwich and mail it to my house.
>>
>>55019844
1 billion ISK is like 30 dollars you bitch
>>
give me haskell or give me death!
https://github.com/gmpl/FSharpPlus
https://github.com/gmpl/FSharpPlus/blob/master/FSharpPlus/Samples/Haskell.fsx
>>
I know it's almost completely useless, but I love the concept and look of Befunge. Can someone recommend a good interpreter?
>>
how do i decode a base64 string as binary in C? i need to write it to a file as a test but google doesn't help much for this
>>
>>55019934
I like the way the code looks too, saw befunge fizzbuzz
>>
>>55019951
Which base64? Anyway, the answer is a switch statement and shift operators.
>>
Planning to learn Lua -- which version do I choose, or does it matter?
>>
>>55020168
When in doubt choose the newest version. It most likely doesn't matter, it's not nearly the horrible wasteland that python 2.7 vs 3 is.
>>
Are there people who consider themselves programmers who don't know how to write in C?

I get it if C might seem dated but I honestly don't know if there's a more fundamental language to modern day programming.
>>
>>55020309
Java
>>
>>55020339
I am on a team that mostly works in Java and everyone on it at least learned C in school. Hell at my school, every class pass the introductory level courses were mostly in C. (Hell, my 101 was in C)
>>
>>55019823
Any other options I should add?
usage: rtorrent-add.py [-h] [--utorrent] [--start | --no-start]
[--preserve-priority | --no-preserve-priority]
[--preserve-ratio | --no-preserve-ratio]
[--preserve-location | --no-preserve-location]
[--preserve-all | --no-preserve-all] [--copy]
[--root ROOT]
FILE [FILE ...]

rtorrent import utility with batching & uTorrent resume file support

positional arguments:
FILE one or more files to import

optional arguments:
-h, --help show this help message and exit
--utorrent, --uTorrent
whether the input should be treated as uTorrent resume
files
--start, -s start torrents on import (default)
--no-start, -S do not start torrents on import
--preserve-priority, -p
preserve priority (includes files) if
possible/applicable (default)
--no-preserve-priority, -P
do not preserve priority
--preserve-ratio, -u preserve ratio if possible/applicable (default)
--no-preserve-ratio, -U
do not preserve ratio
--preserve-location, -l
preserve location relative too the download root if
applicable (default)
--no-preserve-location, -L
do not preserve location
--preserve-all, -a same as -pul
--no-preserve-all, -A
same as -PUL
--copy, -c copy torrents to rtorrent sessions directory
--root ROOT, -r ROOT override rtorrent's configured download location
>>
>>55019951

Either use a library, or make some ugly hack that looks like this:

http://pastie.org/10872554

Disclaimer: I have not actually tested the big endian version, and this does not run on middle endian platforms at all.
>>
Algorithms you should know, or you're probably a code monkey
>simplex method
>FFT
>ford-fulkerson
>IDA*
>PCA
>gram-schmidt procedure
>burrows wheeler transform
>eigenvalue centrality measure
>support vector machine
Note that these are all highly useful algorithms and not academic masturbation, yet many programmers don't understand these
>>
>>55020671
>I went to college so I'm not a code monkey
k
>>
>>55020693
(p -> q) != (!p -> !q)
>>
>>55020671
redpill me on these algos
>>
>>55020708
(p <=> q) = (!p <=> !q)
>>
>>55020722
Yeah but that's not what the post said
>>
>>55020708
try learning those by yourself without formal upper education, i.e., paid professors. it's practically impossible, unless your daddy engineer supports you, which is obviously unrealistic to say the least.
>>
>>55020731
p ~> q != p -> q
>>
>>55020309

C is an extremely important language to know because a large portion of our system level infrastructure is built on it. Most language interpreters are written in either C or C++. Libraries for cryptography, graphics, and other computationally intensive tasks, are written in C or C++, and given a wrapper for other languages. Nearly every other programming language has a foreign function interface to talk to C, because if they didn't, they'd basically be screwed out of every popular software library. To put things into perspective, if I want to use .png images in my application, no matter what language I'm using, I'm either using libpng or a library that uses libpng, which means I'm relying on some C code someone else wrote. I'm also relying on zlib, which is also written in C.

To put things more succinctly, all of the building blocks that make up a modern computing system are written in C. To not know C is to not know how any of your tools work.

If you're looking for anything else that might be seriously important, arguably Javascript should be understood at least somewhat by everyone. It's an ugly as shit language, but if C is the language to understand system software, Javascript is the language to understand web software. If it runs on a browser, it's either Javascript or deprecated.
>>
>>55020739
books are better professors
>>
>>55020750
sure, but then, if you aren't learning this stuff in college (or some educational institution), you probably don't even have spare time to do it.
>>
>>55020746
Woah! You truely are OFF the rails tripfriend
>>
>>55020769
I liked his cameo on that erlang movie
>>
>>55020769
and he doesn't even know C
>>
>>55020798
nobody in this thread knows C
>>
>>55020746
All of this just astounds me when I come across someone who doesn't really know C. Thanks for the context on the ecosystem of libraries and tools (which I didn't really consider) that use C, I wasn't even considering that.
>>
File: Capture.png (31 KB, 677x727) Image search: [Google]
Capture.png
31 KB, 677x727
hmm apparently I did something wrong since this has been running for 6 hours

it has to close 30 tags for each entry, but it still shouldn't be taking this long
>>
File: 1439648511159.jpg (116 KB, 469x469) Image search: [Google]
1439648511159.jpg
116 KB, 469x469
>>55020935
I know C
>>
Any good resources on translating languages? Mainly thinking C++ to other languages (probably lua). They have to be equivalent though.
The reason for this is live code reloading/live code editing. Should I go that route or just put constraints on the project and do DLL reloading?

This is intended to be a teaching tool. I'd prefer to have users write C/C++ (might even make some subset of C++ illegal for political reasons). It's quite an ambitious project. I'm just not sure how to deal with this one detail. It'd be nice to be able to have more code inspection (because it's a teaching tool this is pretty important to be helpful) but I can probably organize stuff in the back end to minimize those issues. I don't want to arbitrarily constrain the user though. Stuff like taking a pointer to a global variable would be problematic with DLL code reloading. But it's solvable. Just not easily. So sorry for being distracted but the tldr is:

Should I translate C++ into Lua for code reloading purposes or deal with the issues you face with DLL live code editing?
>>
>>55020983
you're in over your head; I'd recommend picking up a simpler project
>>
>>55020983
>Get the external behaviour of a program
>Implement it
>>
>>55020543
Have a service that deals with RSS torrents neatly.
Also give labeling options. And sequential/first and last piece first options.
>>
>>55021007
>in over your head
Motivate. Sounds more like you're projecting anon. Why is translating code to an intermediate format for ease of use not valid?
>>55021011
I said a lot anon. I'm not sure what you're referring to exactly when you say 'external behavior of a program'. What program specifically? The user? That wouldn't be enough.
>>
>>55020602
That seems overcomplicated; base64 encodes a string of bytes so byte ordering doesn't matter. While an application may care, the encoding doesn't.
http://pastebin.com/9ywRVfzG

>>55021018
Hmm I forgot about RSS, could definitely be something that could say run as a cron with the URLs as the files.
>>
>>55020945

It's one of the reasons I often recommend C as a first language. Aside from the easy syntax, you've got a whole host of stuff you can learn from that makes up all of the software you use on a daily basis. I believe programmers should be encouraged to dig down into shit and find out how it works.

>>55020983

Generally any resource on compiler writing should work... lex, parse, make symbol table, make AST...

Although you're compiling C++, so honestly I'd recommend instead that you look for a good book on how to tie a noose. You may need it.
>>
>>55021055
Well forget that pastebin, I'm too tired to be programming right now.
>>
>>55021055

Figures as much. It's a dirty hack I wrote a while ago originally in C++ anyways.
>>
>>55020602
>>55021055
Welp just finished my own base64 parser as an exercise.
http://pastebin.com/awcALW3Y
>>
>>55021067
>Although you're compiling C++, so honestly I'd recommend instead that you look for a good book on how to tie a noose. You may need it.
>yfw correctly parsing an AST in C++ is literally undecidable

Speaking of compilers and undecidability, I'm working on a language now with a dependent type system where type terms are literally the same language as expressions (so type checking means running an evaluator). I'm going to see how it works out and if it fails miserably. I'm not entirely sure how type equality is going to work, if I should force function purity, and if evaluation should be lazy or eager (I'm trying to get away with eager).
>>
Java
class Foo {...}
class FooBar extends Foo {...}

@MyAnnotation
void doStuff(Foo foo) {...}

void main() {
FooBar fooBar = new FooBar();
doStuff(fooBar);
}


Can I somehow use annotation processor to check what the declared type of foo param (FooBar in this case) during compile time?
In other words, access information about variable the method was called with.
Currently I check this via reflection in runtime every time method is called.
>>
>>55021153
The whole point of Java subclassing is that types are downcasted and can only be determined at runtime.
You could use overloading instead?
>>
I'm working on a nixie tube clock using a Motorola 68HC11 microcontroller. I've programmed the entire thing in assembly because that's what I'm most comfortable with. I'm a EE major so my programming skills are probably sub par compared to most of you /g/entooman. Was hoping someone could take a look and tell me what you think. Is it good? Is it shit? Constructive criticism? I know I should have more comments in my code. Threw it together in a hurry.

http://pastebin.com/YqQZQe4u
>>
>>55021205
I could not churn out 500 lines of assembly, and I'm sure most of /g/ couldn't either, shit. Nice job.
Next time try Forth
>>
>>55021219
Thanks. I should add, it's been tested and it functions properly. Never heard of Forth. I'll look into it. Not sure if it will be of any use on that particular microcontroller. I seriously need to brush up on C. Too many other things going on in my life.
>>
>>55021165
In real app things are more complicated and I can't really use overloading.
I still have to use reflection to call different methods from Foo's subclasses. The only alternative I see is to make Foo aware of all its subclasses and add all their methods to it to overload later.
Currently it works just fine, it's lack of compile time checking that makes me uncomfortable.
>>
Hvis du forstår ikke Haskell, må du slå av datamaskinen din akkurat nå.
>>
>>55021294
Nu blir Pajeet arg igen
>>
what would y'all recommend as a good I intro to OO languages?
my mentor is big on FP, and so I only really know Erlang/haskell/racket/etc atm.
I'd really like to branch into more common languages
>>
>>55021448
Scala
>>
>>55021067
>Although you're compiling C++
>tie a noose
Aye. It's not necessarily my compiler really. The intent is to have a teaching platform where a user has basic library functionality that's kinda fun to use (think scratch, except not scratch the language). What I'm building is essentially an IDE but the presentation is entirely different and is intended to have much more knowledge of where the user is. There's very few IDE's that focus on learning. And most don't even have memory presentation as an integral part of the program. I haven't found an IDE which does basic stuff like annotating the memory window with whatever you have in your watch window/autos window. Just doing that would open up a better understanding of software and discourage the problematic view that execution time doesn't matter.

C/C++ doesn't get all these fancy teaching tools. So I want to make a fancy teaching tool. And I have good ideas to make it appealing.
>>55021205
>I know I should have more commends in my code
Yes. Far more comments if you're just gonna throw it at people who don't even have any idea of what you think is wrong with it.
Frankly looks like you're bragging. As misguided as that is.
>>
What language has the best module/library system?

I've made a fuckton of tools but they're all in separate languages and its hard to make them all work together. So I want to recreate this little ecosystem of mine under 1 umbrella.

Amazingly enough, Javascript is the best one I found... But Node's dumb event loop makes the idea of doing any computation silly.

Does /g/ know the answer
>>
>>55021583
>What language has the best X
>I gave a fuck about performance
C.
>>
>>55021583
Python and JS, easily
>>
>>55021583
Go
>>
>>55021606
I think you mean C++
He said module/library system is important
>>
>>55021624
I think single-h file libraries are OK.

Tbh senpai I'm not quite sure what he meant by module/library system because it sounds too complicated to belong in any reasonable software.
If that's just what you call the languages import/include/using system I still hold C is better than C++.
>>
>>55021624
C++ uses the same "library system" as C, but now the ABI is unstable.
There is seriously no reason to use C++ over anything.
>>
>>55021640
>>55021643

namespaces
>>
>>55021570
Well if you plan on writing software that tries to remotely decipher C++ code you're going to have a really difficult time.
>>
>>55021043
>intermediate format for ease of use
you don't even know what the fuck you're talking about, let alone how you could possibly accomplish it; picking c++ as your source language to be translated speaks volumes about your inexperience
>>
>>55021651
You get to write my_shit::function instead of my_shit_function. Great.
It's totally worth fucking with the ABI just for that.
>>
>>55021677
IDEs
>>
>>55021643

>There is seriously no reason to use C++ over anything.
Templates, constexpr, RAII, namespaces, lambda expressions, real reference types, and if you really want it, it also supports object oriented programming.
>>
>>55021684
retard
>>
>>55021570

Suggestion: instead of compiling C++ to Lua, compile LLVM bytecode to Lua. That way you can just use Clang to handle the C++ parsing part. As a bonus, you get a Rust to Lua compiler for free.
>>
>>55021694
>it also supports object oriented programming
That is a massive downside though.
>>
>>55021707
Supporting it fucking isn't
It doesn't make you use it
>>
>>55021694
>RAII
When will this meme die? It's pointless and is basically a workaround to avoid exception issues.
>templates
Shitty metaprogramming language. Easily replaced by babbys first meta-programming library attempt.
>reference types
Never got the appeal. Maybe if you had a language with super strict anti-aliasing rules.
>OOP
Sounds rather minor. Really I don't think it's positive as a whole. Sure, it's a typing convenience sometimes.
>>55021715
Well you should avoid people who do. So there's your downside.
>>
>>55021715
It bloats the language
>>
>>55021706
Lua doesn't have goto. Even then you'd end up with REALLY ugly resulting Lua.
>>
>>55021715
You will get fags who write libraries in that style.
Also, extra bloat in a language is a bad thing and leads to thousands of subsets of a language that see use. Just look at C++.
>>
>>55021715
>you should write everything by yourself without using libraries written by third parties
that's the problem with C++: everyone picks a different subset of what they consider reasonable to use
>>
>>55021723
>>55021729
>>55021734

>freedom is bad because other people will do what I don't want them to
>>
>>55021606
Solving the linking problem of C is something no build system I know does well.

As in, I can't build a bunch of libraries forming a tree of dependencies and have any of them build and link all those projects. I can't make a change in a library and then build the project that uses it to observe the effects of that change.

One of my projects is actually a Makefile-based system that does just that: it uses a concept of a "workspace" that I invoke the build system in, telling it which project to build. Projects declare other "projects" they depend on, and they get built to satisfy dependencies. All build artifacts end up in a $workspace_prefix/build/{objects,shared-objects,executables} and it automatically adjusts PATH for executables and dynamic linking; it even supports building in tmpfs

And it's still not good enough.

>>55021612
>Python
Why's it's module system good?

>JS

JS would be my choice were it not for the eventloop of Node.js, which makes programming in general impossible. Any long computation will bottleneck the whole thing. There's a workers pull request on GitHub that will add execution as a form of background I/O, it looks promising but it's "on the TODO list for v7".

>>55021622
Why's it's module system good?

>>55021624
>C++
>libraries

Unstable ABI. Virtually useless language for any application which won't also be built in C++ with the exact same compiler down to the version.

>>55021640
I mean developing a set of functions and data structures, packaging those up as a "module", and then importing those in the applications.

This is because my tools are a natural fit for the libraries. I made some functionality and the one off script that used it was actually quite small. Then I needed that exact functionality in another application... In another language, and it was awkward
>>
>>55021751
it's like you have a mental illness >>55021715
>>
>>55021661
That's partly the reason I considered limiting the C++ to a subset. I consider it a bad language. It's just the best option right now.
>>55021672
>picking c++
The point is to teach C++. So that's just a given, it's not a choice.
>speaks for your inexperience
C++ shouldn't be taught? Sounds rather opinionated.
>you don't even know what the fuck you're talking about
Well I know that LUA has good (imo) introspective properties. It's interpreted so it allows for a low iteration time which is a major plus.
>>55021706
I haven't looked into LLVM bytecode much.
Though I've reconsidered. It's probably going to be a mess dealing with the LUA if its translated becuase i can't assume a small change in C++ code produces similar LUA all the time. I probably won't support modifying datatypes for instance. If I were using lua directly that would not be hard to do. So I'm probably just gonna lex the C++ and use that to provide the user with the help they need. Shouldn't have been so dismissive of that other guy. It has some obvious issues.
>>55021724
My limited understanding is that the branching can be represented as switch/if statements. But I just know about it from watching this: https://www.youtube.com/watch?v=FnGCDLhaxKU
>>
>>55021707

You don't have to use it. It's just there if you want it.

>>55021722

>When will this meme die? It's pointless and is basically a workaround to avoid exception issues.
Even if you completely turn off exceptions, RAII is incredibly useful as a means of resource management.

>Shitty metaprogramming language. Easily replaced by babbys first meta-programming library attempt.
Well considering the alternative in C is just macros, ask yourself this: do you really want to use a macro library to get all of your generic programming done?

>Never got the appeal. Maybe if you had a language with super strict anti-aliasing rules.
You can guarantee a pointer will always point to a valid object.

>>55021724

C++ supports goto. Still want to compile it to Lua?
>>
>>55021790
>You don't have to use it
you're an idiot, but then again you don't even know C, so your opinions are irrelevant anyway
>You can guarantee a pointer will always point to a valid object.
no, you can't
>>
>>55021811
>someone who knows fucking nothing about programming talking about how Ruby knows nothing about C
>>
>>55021777
>That's partly the reason I considered limiting the C++ to a subset
This is definitely a good idea. But you still have a bunch of fucked up things like double templates >> vs shift right, templates in general I guess, and the often cited example
x * y(z);
could mean multiple different things based on context. You could try to iron out these fucked up edge cases or you could just parse C instead...

>>55021777
>My limited understanding is that the branching can be represented as switch/if statements.
This is true but it'll take some deciphering of a format that is suited for somewhat arbitrary jumps. It's like, converting something from english, to chinese, back to english. Or maybe english -> chinese -> spanish. Either way it gets kind of fucked by the intermediate step. Also I don't think Lua has "switch" statements.

>>55021790
>RAII is incredibly useful as a means of resource management.
How come Rust doesn't have full-fledged RAII?
I think that RAII _was_ a good thing way back then, but for example in the program I'm writing, by using smart pointers I'm literally better off by not declaring the deconstructor. Implementing some 4 constructors is absolutely absurd and should not be required of any sane individual trying to write code.
>>
>>55021790
>resource management
What is a resource anon?
I've had this discussion for years. Basically when you start asking these questions about what resources are RAII falls apart because it's a concept that has problems at the root.
>You can guarantee a pointer will always point to a valid object.
Pretty sure there's such a thing as reference invalidation.
You can use pointers in the exact same way and have the exact same benefits because it's no different really.
If you want to use references as a way of signifying 'this has been checked as valid'. That's fine. But don't say that it solves anything. It's just a coding convention at that point. Not a language feature. Doing
void foo(int* ptr /*Hey mr programmer, this function should demand of the caller that this pointer is always valid!*/){
/*----*/
}

Has the same effect. Sure, it's fine to have a single symbol for that. But it doesn't guarantee anything.
>>
>>55021839
>If you want to use references as a way of signifying 'this has been checked as valid'. That's fine.
You could literally write code without a type system at all, and just do all of the correctness checking in comments and "in your head", but for some odd reason people don't do that...
>>
>>55021877
I do that. But devils advocate here:
You don't share you head with other people on the project.
>>
>>55021886
I mean I do correctness checking in my head. Not program without a type system at all. Obviously.
>>
Learning my way around Arch. Successfully installed it yesterday as dualboot. Love it
>>
Was in a thread yesterday that asked wheter C was functional or object-oriented, and one poster ( >>55007904 ) wondered if it was possible to do the following in C

let (^) x y = pown y x

let fs = List.map
let f1 = (*) 2
let f2 = (^) 2
let fsf1 = fs f1
let fsf2 = fs f2

printfn "%A" (fsf1 [0; 1; 2; 3])
printfn "%A" (fsf1 [2; 4; 6; 8])
printfn "%A" (fsf2 [0; 1; 2; 3])
printfn "%A" (fsf2 [2; 4; 6; 8])


I've made a solution, and although it has it's limitations, it is pretty generic.

First limitation is of course that you need to manually mind memory, and therefore I pass the output list as an argument to the function instead of allocating and returning it within.

Second limitation is lack of lambdas, I can't declare a function within a function. This is why I had to declare f1, f2 and fs.

Third limitation is that this only works on UNIX machines (tested on OS X and Linux).

Of course, one of the reasons I like C++ over C is templates and what you are able to do real apply functions (instead of hacking it with stdarg or with passing void* pointers around). In C++ you are also able to do operator overloading stuff like boost optional, std::function and std::bind are really handy in addition to state capturing lambdas (closures)

But without further ado, I present my C implementation of the above code :

http://pastebin.com/0hgT1cxG

>pls no bully
>>
There is nothing wrong with references themselves, but C++ references are the most retarded thing in existence.
void my_fn(int &n)
{
// Mutate n in some way
}

and by calling it:
// Elsewhere
int n = 10;
my_fn(n);
assert(n != 10);

It fucking changes state invisibly. Way to make your programs way fucking harder to reason about.
There is NOTHING syntax wise to suggest that n might change.
When you see something like the C & operator, you know that n could probably change.
>>
>>55021940
If it's not declared const, then you must assume it is going to change.
>>
>>55021940
It doesn't have to change state

and as >>55021952 says, you should assume anything that isn't const is mutated
>>
>>55021952
I can easily change things even if they're declared const.
>>
>>55021940
This is also a valid point.
But it's not 'invisible'. The function should be clear in its name. But it puts requirements in the wrong places when there could be static type checking that deals with it.
>>55021952
Which he can't know if it is or not without looking at the function. That's 2 seconds of thinking you didn't spend before writing this post. Think before you post.
>>
>>55021940
I'm really not sure what you are expecting?
>>
>>55021934
>((void (*)(void*, size_t size, size_t nmemb, const void*, void*)) func) ((void*) data, size, nmemb, in, out);
Holy shit just stab my eyes out and call my oedipus I don't need this in my life.

Also I'm sure your code is really neat but I can't look at it

>>55021940
You generally want to make references mutable only when they're definitely going to be mutated, but it is true that it makes the function calls pretty ambiguous.
e.g.
int do_thing (int in_param, int& out_param) {
...
}

int main () {
int input, output;
input = 5;
do_thing(output, input); // oh fuck!
}
>>
>>55021955
You would have to fuck around on purpose to do that though. That's like saying it's a problem that you can do this in C

void _my_fn(int* n);
#define my_fn(n) _my_fn(&n)
>>
>>55021958
>Which he can't know if it is or not without looking at the function.
Why would you call a function without looking at its signature?

How would you even know
1) that the function exists
2) what it does
3) what arguments it takes
4) what it returns (non-const references are assumed to be mutated)

>That's 2 seconds of thinking you didn't spend before writing this post. Think before you post.
See above, shit for brains.
>>
>>55021955
It's going to be easy to see the bug when there are casts like
 .. (const data& dat) {
*((data*) &dat) = haha_fuck_you();
}

void thing (int& real_object_not_null) { }
...
thing(*(int*) nullptr);
>>
>>55021981
So you're saying everyone remembers everything perfectly? No there's plenty of calls you can make without perfect knowledge. Sure, ill advised perhaps.

And yes most people do look at functions in some form while calling. Either via a reference or through their IDE. But there's no guarantees.

Either way, a pointer can be safely assumed to modify where a pass by value wouldn't. It's superfluous.
>>
>>55021981
1) human error
2) human error
3) human error
4) human error
Wow, what if people actually wrote software, not robots!
>>
>>55021968
>Holy shit just stab my eyes out and call my oedipus I don't need this in my life.
Function pointers can be a bit messy to type.

>>55021987
>>55021955

Casting away const with C casts is undefined behaviour, just saying.

const_cast doesn't work on references
>>
>>55022006
So in other words there's completely nothing fucking wrong with references?
>>
>>55022005
>So you're saying everyone remembers everything perfectly?
No, that's why you look at the goddamned signature.

>Either way, a pointer can be safely assumed to modify where a pass by value wouldn't. It's superfluous.
References are not pass by value.........
>>
>>55022019
>No, that's why you look at the goddamned signature.
Yeah, it's called human error. But you're making a compile time error into a runtime error (and worse yet likely a logical error). Does that sound good?
No it's fucking terrible. But sure, use references if you're a demigod or whatever. Just don't expect me to ever want to work with you.
>References are not pass by value.........
Yeah. I completely disregarded references because THEY ARE SUPERFLUOUS.
>>
>>55021934
So C is functional now?
>>
>>55022043
C can be whatever it wants
>>
>>55021934
You can actually do some pretty interesting functional work with Cuda C++

template <typename T>
using uCat = T(*)(T);

template <typename T>
using mCat = T(*)(T, T);

__device__ int square(int a)
{
return a * a;
}

__device__ int mult(int a, int b)
{
return a * b;
}

template <typename T>
__device__ T mult(T a, T b)
{
return a * b;
}

template <typename T, mCat<T> func>
__global__ void biMapKernel(T *a, T *b, T *c, size_t size)
{
size_t i = threadIdx.x + blockIdx.x * blockDim.x;
if (i < size)
c[i] = func(a[i], b[i]);
}

template <typename T, uCat<T> func>
__global__ void MapKernel(T *a, T *c, size_t size)
{
size_t i = threadIdx.x + blockIdx.x * blockDim.x;
if (i < size)
c[i] = func(a[i]);
}

You're basically getting typeclasses for compile time Cuda metaprogramming.
>>
>>55022036
>But you're making a compile time error into a runtime error
That's like saying that a human (mistakenly) typing a & in front of his variable is a runtime error. It's just a non sequitur, anon. References are passed by reference, if the programmer erroneously ASSUMES that it is a value and not a reference, then it is a human error.

>Just don't expect me to ever want to work with you.
I would never work with a C++ programmer that insists on using C-style pointers in a world where const correctness and smart pointers exist.

You are not a good C++ programmer.

Also, how would you deal with stuff like copy constructors if you refuse to use references?

Do you think it is a good idea to do deep copies of every vector and data structure you pass around?

>Yeah. I completely disregarded references because THEY ARE SUPERFLUOUS.
They are not, references have constraints pointers do not have (such as not being nullable and not being able to arithmetics on them), allowing optimisations and code correctness.

It also allows you to pass around composite types without doing deep copies.
>>
>>55022078
And regular C++(11 and newer) for that matter.
>>
>>55022019
>>55021981
>that's why you look at the goddamned signature
If I'm reading over code, I don't want to have to look at the signature for EVERY single function I call.
I want to be able to start at the top of a block, and reasonably know the state of the program by the end of the block, without having to dig through several other files to find out.
State changes are incredibly important, and they should be incredibly obvious.
Your language shouldn't have anything which gets in the way of you being able to reason about your programs.
>>
>>55022092
>State changes are incredibly important, and they should be incredibly obvious.
They are, if it is passed by reference you should assume that IT IS PASSED BY REFERENCE

Jesus....
>>
>>55022089
Definitely. The fact that you can write a generic map over a functor (array on the device) or in this case a bimap over 2 functors using a monoid satisfying typeclass is pretty cool. The cooler part however is that you get the error at compile time if your function doesn't satisfy the type constraint, without thousands of pages of template errors.
biMapKernel<int,mult><<<10,1>>>(d_a, d_b, d_c, 10);
>>
>>55022092
For better or for worse, one of C++'s big design rules seems to be implicit > explicit.
It's great because it allows you to pretend you're more abstract code than you actually are, but the downsides are obvious.

Examples:
- implicit RAII constructors when passing, returning, assigning etc.
- implicit cast constructors, for instance string literals become std::string's due to a non-explicit constructor (you literally have to declare 'explicit', otherwise it implicitly calls the constructor under your fingertips!)
- references require no syntax
- template type inference
- function overload decisions, they get especially complex for operators
I'm sure there are more examples
>>
>>55022128
I would blame it on the committee insisting on being compatible with C, which is why you have implicit constructors and these things.
>>
>>55022100
>if it is passed by reference you should assume that IT IS PASSED BY REFERENCE
But there is no syntactic different between pass-by-reference and pass-by-value. An error in confusing arguments (>>55021968) is literally invisible
>>
I går knullet /g/ en sau fordi den kunne bruke et programmeringsspråk bedre enn /g/
>>
>>55022100
some_function(a);

This is an excerpt of some C++ code. It was written awhile ago, so you can't remember its type signature.
Is a passed by reference or value?
You're going to say "look at the signature".
Ok, you now go to some_file_i_included.h. You look at the type signature.
Fine. However, when you're reading over code, you don't look at a single function call. You will be looking at hundreds, possibly thousands. Do you really think that it's acceptable to go to some different file (or seek the current file) hundreds of times to just be able to reason about your programs?

Reasoning about your programs should be as easy as possible. Don't use shitty meme languages that get in your way.

>>55022128
implicit is horrible most of the time, especially when it comes to knowing about the program's state.
>>
>>55022128
you can do implicit and explicit
>>
>>55022011
You should pretty up your function pointer types by using
using[/using] which is a sexier typedef.
>>
>>55022143
>But there is no syntactic different between pass-by-reference and pass-by-value.
There is, there is a fucking ampersand in the code signature.

>An error in confusing arguments (>>55021968) is literally invisible
In C you can pass an integer as any pointer type and it will merely generate a warning, C++ references have additional constraints (one being that it must be of the same type).

In what world is passing C-style raw pointers considered """"superior"""" in terms of code correctness and generating compile-time errors?
>>
>>55022147
If you have a proper development environment then you should be able to see the type just by highlighting the function.
>>
>>55022147
>implicit is horrible most of the time, especially when it comes to knowing about the program's state.
I think implicit was a fad when C++ was designed but people are waking up and realizing that it's shit.
For example a lot of newer languages are doing away with weak integer -> float promotions in arithmetic, that were a thing in C and Java, and especially JavaScript (the horror!).

>>55022150
But the C++ language clearly favors implicit because everything is implicit by default.

>>55022163
>There is, there is a fucking ampersand in the code signature.
We was talking about C++. I know C.
>>
>>55022147
Implicit works well when there is precisely one behaviour, e.g. typeclasses. In many other cases it is a nightmare, though.
>>
>>55022175 (Me)
>We was talking about C++. I know C.
Fuck I misread you. Having to lookup function signatures for every call is not convenient, period. Have you ever tried to decipher a C++ standard library header?
>>
>>55022175
>We was talking about C++.
Yes, in C++ a reference is declared with AN AMPERSAND.

void foo(int& /* <--- a fucking ampersand */ a);


>>55022147
>This is an excerpt of some C++ code. It was written awhile ago, so you can't remember its type signature.
Do you have a habit of fucking around with code you don't know?

Why would you even modify the call to some_function if you no longer remember what some_function does.

Your scenario is bad.

>Fine. However, when you're reading over code, you don't look at a single function call. You will be looking at hundreds, possibly thousands. Do you really think that it's acceptable to go to some different file (or seek the current file) hundreds of times to just be able to reason about your programs?
It's like you actually believe that gtags/ctags and cscope doesn't exist.
>>
>>55022163
>it will merely generate a warning
>merely
A warning is a huge fucking red flag and will get any programmer's attention. If you let your code compile with warnings, you've failed as a programmer and should probably kill yourself.

>In what world is passing C-style raw pointers considered """"superior"""" in terms of code correctness and generating compile-time errors?
I honestly like Rust's system the best.
my_fn(&var); // Immutable reference
my_fn2(&mut var); // Mutable reference
>>
>>55022086
>You are not a good C++ programmer.
No I'm a (relatively to you at least) good programmer.
>copy constructors
These are things you shouldn't use. They're far worse. Anything that hides code complexity like that is insane to use if you care about performance (and if you don't go use a language that's better than C/C++).
>such as not being nullable
Except they can. Have you even programmed with references?
>Do you think it is a good idea to do deep copies of every vector and data structure you pass around?
No but you pass something to a function understanding the potential. With a reference human error can slip in.

If I were to support a reference syntax it'd have to have a syntax like
void foo(int& ref){

}
int main(){
int i=5;
foo([RefSymbol]i);
return 0;
}

or whatever. And generate an error when you don't pass that. It's necessary.

Regardless even in the function I don't like them because with pointers at any instance you know the pointer is modifying something that's potentially outside the function. Which is important to know. Const references are important to use for that reason. But that's a solution to a newly introduced issue I hope you see. Another point of error.
>That's like saying that a human (mistakenly) typing a & in front of his variable is a runtime error.
It's not the same to have two different potential meanings for the same codesnippet and have a typo.
>>55022175
I think everyone had the woes but nobody dared complain because of how programming discussions go.
There's a 'right solution' according to people and the only options that are considered are the ones available. I think this:
https://www.reddit.com/r/programming/comments/2gwkkp/ideas_about_a_new_programming_language_for_games/
'Best post' is quite telling. It's making assumptions and is conservative for unmotivated reasons.
>>
>>55022196
>Do you have a habit of fucking around with code you don't know?
Yes. Don't you do this?
>Why would you even modify the call to some_function if you no longer remember what some_function does.
I never said anything about modifying it. It's about understanding and reasoning about it.
>>
>>55022201
>A warning is a huge fucking red flag and will get any programmer's attention. If you let your code compile with warnings, you've failed as a programmer and should probably kill yourself.
The Linux kernel spits out literally hundreds of warnings when it compiles, due to the compiler improving over time (and stuff that the early GCC didn't detect is now detected).

You are ignorant if you think that everyone is working on FizzBuzz-sized projects and that you are the sole developer.

>I honestly like Rust's system the best.
I agree with you, but I still don't find it a reason to dismiss C++ references all together. They are an improvement over raw-pointers when it comes to code correctness.
>>
>>55022217
>They are an improvement over raw-pointers when it comes to code correctness
As I keep fucking saying. It hides information at the point where the function is called.
It's the same reason that operator overloads and exceptions are shit.
>>
>>55022217
You've clearly never worked on a large codebase before. Anything submitted to code review that produces a warning is an instant fail.
>>
>>55022209
>No I'm a (relatively to you at least) good programmer.
Apparently not

>These are things you shouldn't use
Opinions dismissed

>Except they can. Have you even programmed with references?
Not nullable does not mean that they can't be null (due to programmer error).

But you cannot do this
int a = 42;
int& n = a;
n = NULL;


>No but you pass something to a function understanding the potential. With a reference human error can slip in.
With raw pointers, even more so. You have to be insane if you sincerely think that C style pointers are good when considering correctness.

>If I were to support a reference syntax it'd have to have a syntax like
I would too, but that isn't the case (due to backwards compatibility with C). The lack of such syntax is not, however, an argument to dismiss C++ references all together.

>>55022212
>Yes. Don't you do this?
I don't modify random things I don't understand what does, no.

>I never said anything about modifying it. It's about understanding and reasoning about it.
In order to reason anything at all about some_function, you would have to look at the declaration and the comments describing what it does.
>>
>>55022242
>tfw my code produces warnings because the compiler is wrong
>>
>>55022209
>There's a 'right solution' according to people and the only options that are considered are the ones available. I think this 'Best post' is quite telling. It's making assumptions and is conservative for unmotivated reasons.
Sorry, I'm not entirely sure what you meant by this.
>>
File: review-nightcrawler.jpg (159 KB, 1920x1080) Image search: [Google]
review-nightcrawler.jpg
159 KB, 1920x1080
>>55022256
jej some people man
>>
>>55022242
>You've clearly never worked on a large codebase before.
You can meme all you want. I have a patch acked for inclusion into the Linux kernel (no it does not produce a warning).

>Anything submitted to code review that produces a warning is an instant fail.
That is not synonymous with a code base that is completely free from warnings all together. As I said, the Linux kernel spits out hundreds of compiler warnings. Sure, your aim should be to write warning-free code, but it is sometimes inevitable when working on low-level code.
>>
>>55022232
>As I keep fucking saying. It hides information at the point where the function is called.
Raw pointers allow you to
>write to memory you shouldn't be allowed to
>read from memory you shouldn't be allowed to
>pass integers as if they were pointers and vice versa
>pass NULL to something that should be initialised

References prevent these things


>It's the same reason that operator overloads and exceptions are shit.
You know nothing. Exceptions are good

{
std::lock_guard<std::mutex> lock(mutex);

// .. do stuff

<oh shit something bad happened, exception is thrown and lock is released instead of deadlocking the system>
}
>>
>>55022273
It's true. scalac is a mess.

By the way, does /g/ know why this evaluates to false?

List(1, 2, 3).toSet()
>>
>>55022305
>Exceptions are good
Opinion completely discarded.
>>
reminder that exceptions are the black sheep of monads
>>
>>55022317
How else would you guarantee that a lock is free'd on memory corruption in a mission critical system?
>>
>>55022319
Exceptions are just an implicit global specialized ErrorT. There's nothingwrong with them.
>>
>>55022335
I think that was his points. They are just a specialised implementation of monads.
>>
>>55022305
>References prevent these things
This is only true at compile-time. It's still possible to have a NULL reference at runtime.
>>
>>55022328
By using goto.
>>
File: 1422139984182.jpg (57 KB, 800x600) Image search: [Google]
1422139984182.jpg
57 KB, 800x600
>>55021834
>How come Rust doesn't have full-fledged RAII?
ownership semantics in Rust is a normalized RAII.
>>
>>55022343
If you actually read what I wrote, I wrote "passing NULL to something that expects an initialised value"

This is impossible to do with references.

Of course, bugs do happen and programmers make errors. My point is that references are better than raw pointers in terms of code correctness.

>>55022344
Why would you completely discard RAII and manually free things?
>>
>>55022335
There's a few things wrong with an implicit global specialised ErrorT.
>>
>>55021934
This is like porn
>>
>>55022373
Greater control and predictability.

>>55022376
>specialised
Oh, are you a Brit? 'ey up, lad
>>
>>55022253
>But you cannot do this
>
int a = 42;
int& n = a;
n = NULL;

Why would anyone care about that?
Also I don't think you can't assign 0 to a referenced variable for some reason. Maybe you know far more than I do about references but why would a not be allowed to be 0 here?
>due to programmer error
So we're ignoring reality and assuming they're used 'correctly' when they're problematic and should be fixed? Would you ever design references as the C++ committee did? No, they're not sane and I hope you are.

>With raw pointers, even more so.
I'm complaining about references because they don't belong. I'm not saying C pointers are great. They're better because people know to be cautious. Earlier on someone (maybe you) said that references couldn't be invalidated. Which is just bonkers. Would anyone say that about a pointer?
>The lack of such syntax is not, however, an argument to dismiss C++ references all together.
Well I say it is simply because it makes them so dangerous. See >>55021790
>You can guarantee a pointer will always point to a valid object.
In reference to the appeal of references. They don't solve this issue and it's a common perception that they do.

They pretty much require additional to be valid in any way. Or you simply don't know and they're too dangerous outside your own hobby projects.

>>55022260
Well, it doesn't make sense unless you read the entire first comment on the reddit page.

Basically the guy is saying 'C++ is good enough, stay with C++'. 'I think he will have problems implementing these things!'. Which I think is just unmotivated garbage. And you see it practically everywhere.

It shouldn't be that way. People should consider things in isolation. Seems odd considering I'm arguing against a 'new' feature. But the problems I have with references is that they're just a pointer type someone wanted to make nice and they really just compromised too much and made it a 'prettier' pointer. Which I don't think was needed.
>>
>>55022402
>Greater control and predictability.
Both are demonstratively false.

If you think goto's have any place in structured programming, don't post here again until you've read SICP
>>
>>55022402
'ey up!

I'm strayan though m80. Spent a winter in Canada living with a bunch of lads from God's Own Country.
>>
>>55022411
>>54996861
VS
>>54998722
FIGHT.
>>
>>55022403
>Also I don't think you can't assign 0 to a referenced variable for some reason. Maybe you know far more than I do about references but why would a not be allowed to be 0 here?
0 is not NULL

C++ don't allow implicit casting of void* like C does. In C++11 and newer you also have nullptr, which is a proper keyword and solve problems that occurred with NULL.

>So we're ignoring reality and assuming they're used 'correctly' when they're problematic and should be fixed? Would you ever design references as the C++ committee did? No, they're not sane and I hope you are.
>could be fixed
They cannot be "fixed", because C++ is backwards compatible with C and changing syntax would break virtually all existing code.

>I'm complaining about references because they don't belong.
They do.

Hell, in Python and in Java, object types are always passed by reference. In C++ it is at least explicitly.

> They're better because people know to be cautious.
>>
>>55022411
Goto is the best way to perform resource management in C. It is a perfectly good language construct. Those who argue for its indiscriminate banning are as foolish as those who use it indiscriminately when other language constructs would be more appropriate.
>>
>>55020971

Post skirt and shaved legs.
>>
>>55022460
>Goto is the best way to perform resource management in C.
C is a portable assembly.

C++ is a modern multiparadigm programming language.

>Those who argue for its indiscriminate banning are as foolish as those who use it indiscriminately when other language constructs would be more appropriate.
Where did I argue that at all? I argued that goto's are not a replacement for exceptions.
>>
>>55022420
Tha's doing God's work, lad. Good on yer
>>
>>55022470
Kill yourself, fag.
>>
File: digouteyes.png (61 KB, 252x221) Image search: [Google]
digouteyes.png
61 KB, 252x221
>>55021934
    ((void (*)(void*, size_t, size_t, const void*, void*)) func) ((void*) data, size, nmemb, in, out);
>>
>>55022481
C++ is a jack of all trades, and master of none. It is seldom the best choice of language any more.
>>
>>55022509
This is why C is so much better than C++.
>>
File: 562.jpg (36 KB, 600x600) Image search: [Google]
562.jpg
36 KB, 600x600
>posting the degenerate homosexual anime image
>>
>>55022521
And still Adobe and Facebook pay people six figures for developing C++ for them full time.
>>
>>55022411
struct triple {
void * a;
void * b;
void * c;
};

struct triple allocate(size_t n) {
struct triple t = { 0 };

t.a = malloc(n);
if (NULL == t.a) goto error;

t.b = malloc(n);
if (NULL == t.b) goto error;

t.c = malloc(n);
if (NULL == t.c) goto error;

error:
free(t.a);
free(t.b);
free(t.c);

return t;


Good way to handle resource acquisition errors in C.
>>
>>55022436
>that C switch

Amazing!
>>
>>55022562
>Good way to handle resource acquisition errors in C.
We were talking about C++, not C.

Also, that is broken.

If the first malloc fails, you will try to free NULL three times which is going to make you have a bad day.
>>
>>55022554
not degenerate
not homosexual
lame meme
>>
File: Microsoft Visual Studio.png (2 KB, 354x62) Image search: [Google]
Microsoft Visual Studio.png
2 KB, 354x62
>>55022446
>They cannot be "fixed", because C++ is backwards compatible with C
So you're saying that if you use a reference in C++ it's still valid C code?
No clearly not. So since you've thrown the idea of backwards compatibility (because you used a C++ reference) you can make them nice.
>>55022446
>C++ don't allow implicit casting of void* like C does.
So you mean that every compiler and in the world defines NULL as a (void*)0. Doubt it.
And it's not what windows does in its API for instance. It'd be silly to assume that. It's not a language standard thing.

I don't see why you'd assume that in the first place. Is it in the C++ standard that NULL is a (void*)0?
>>
>>55022373
#include <stdio.h>

struct ShittyArgument
{
int barf;
};

void scream(ShittyArgument& arg)
{
printf("REEEEEEE%i\n", arg.barf);
}

int main(int argc, char **argv)
{
ShittyArgument *foo = NULL;
scream(*foo);
return 0;
}
>>
>>55022590
free(NULL) is defined to be a NOP and is perfectly safe.
>>
>>55022590
you can safely free null pointers.
>>
>>55022590
>you will try to free NULL three times which is going to make you have a bad day.

Do you even know the standard? free(NULL) is a no-op, are you seriously this fucking retarded or just pretending to be stupid?
>>
>>55022590
>If the first malloc fails, you will try to free NULL three times which is going to make you have a bad day.
Oh yeah a function call that does nothing in an error case 3 times. Sounds nightmarish.
>>
>>55022607
>So you're saying that if you use a reference in C++ it's still valid C code?
No, I'm saying that you can use C++'s concept of default constructors and pass by reference in conjunction with C code.

>So you mean that every compiler and in the world defines NULL as a (void*)0. Doubt it.
That is how it is defined in the standard, so as long as the compiler conforms to the standard, then yes.

>And it's not what windows does in its API for instance
If that's true, then Windows API is invalid C for trying to redefine NULL.

>It'd be silly to assume that. It's not a language standard thing.

It is.

>I don't see why you'd assume that in the first place. Is it in the C++ standard that NULL is a (void*)0?
Yes.


It's even in the C standard, section 6.3.2.3 Pointers

http://www.open-std.org/JTC1/sc22/wg14/www/docs/n1256.pdf
>>
>>55022590
This is why C++ fans think C is dangerous and difficult to use, it's because they don't actually know the language
>>
>>55022590
>>55022617
>>55022628
>>55022632
>>55022638
>>55022662
Best part of this thread, by far.
>>
>>55022662
This is why C fans think C++ is dangerous and difficult to use, it's because they don't actually know the language
>>
>>55022590
>If the first malloc fails, you will try to free NULL three times which is going to make you have a bad day.

Try actually learning C before dismissing good code as broken. You'll learn to love the simplicity of the language and you'll see that even the hated goto has its uses.

By the way, this is also the reason why we don't set pointers to NULL once we're done using them. That's a great way to hide bugs.
>>
THIS IS YOUR THREADLY REMINDER THAT ALL PROGRAMMING LANGUAGES ARE
NOTHING MORE THAN SYNTACTIC SUGAR FOR MACHINE CODE.
>>
>>55022562
>Good way to handle resource acquisition errors in C.
Nice, now do that with a mutex and a database connection instead of a shitty pointer.
>>
>>55022693
this happens in every thread, someone is wrong about C or C++ standard and 20 other anons corrects him
>>
THIS IS YOUR THREADLY REMINDER THAT ALL MACHINE CODE IS JUST A REALLY SHITTY WAY OF DOING COMPUTATION AND WE SHOULD JUST USE TURING MACHINES INSTEAD
>>
File: 1455032554035 copy.png (630 KB, 1280x720) Image search: [Google]
1455032554035 copy.png
630 KB, 1280x720
>>55022662
Is there a better way of removing the last digit in the mantissa of the floating point number?
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
long double deleteDigit(long double fractionalPart)
{
char tempBuff[257];
memset(tempBuff, 0, 257);
sprintf(tempBuff,"%.13Lf",fractionalPart);
char lastDigitTemp[257];
long double lastDigitNumeric = 0;
long double decimalPlaces = 0x9184E72A000;
for(register int x = 14;x > 1;x--,decimalPlaces/=10)
{
if(tempBuff[x]!='0')
{
strcpy(lastDigitTemp,&tempBuff[x]);
lastDigitTemp[1] = '\0';
break;
}
}
lastDigitNumeric = strtold(lastDigitTemp,NULL);
lastDigitNumeric/=decimalPlaces;
fractionalPart-=lastDigitNumeric;
return fractionalPart;
}
void stripDigit(long double *a)
{
long double intPart,fractPart;
fractPart = modfl((*a),&intPart);
(*a) = intPart+deleteDigit(fractPart);
}
int main(void)
{
long double a = 9.1234567890123;
for(register int x = 0;x<13;x++)
{
printf("%.13Lf\n",a);
stripDigit(&a);
}
return 0;
}

>>
>>55022708
But the guy has been such a fucking wanker, so it's all the more sweet.
>>
>>55022708
Nobody on earth knows the C++ standard. C is actually tractable though.
>>
>>55021934
void __end();
void __call(uintptr_t addr, size_t size, size_t nmemb, const void* in, void* out)
{
uintptr_t offs = ((uintptr_t) &__end) - ((uintptr_t) &__call);
uintptr_t func = *((uintptr_t*) (addr + offs));
uintptr_t data = *((uintptr_t*) (addr + offs + sizeof(uintptr_t)));

((void (*)(void*, size_t, size_t, const void*, void*)) func) ((void*) data, size, nmemb, in, out);
}
void __end() {}


void* bind(uintptr_t func, void* data)
{
size_t page_size = getpagesize(); // assumes that the size of __call is less than a page

char* ptr = mmap(NULL, page_size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
if (ptr == NULL)
{
perror("mmap");
return NULL;
}

size_t func_size = ((uintptr_t) &__end) - ((uintptr_t) &__call);
uintptr_t func_addr = (uintptr_t) &__call;

memcpy(ptr, (const void*) func_addr, func_size);
*((uintptr_t*) (ptr + func_size)) = func;
*((uintptr_t*) (ptr + func_size + sizeof(uintptr_t))) = (uintptr_t) data;

return ptr;
}


void call(void* bound, size_t size, size_t nmemb, const void* in, void* out)
{
uintptr_t func = (uintptr_t) bound;
((void (*)(uintptr_t, size_t, size_t, const void*, void*)) func) (func, size, nmemb, in, out);
}


This is quite clever, passing the address of the function to call. Allocating an entire page for this seems a bit excessive though, but I guess it's a work around for mmap needing page-aligned addresses?
>>
>>55022647
>redefine
Well It's literally right there "an intereger constant expression with the value 0".
>it's in the C standard
What I can read in the C standard is that it's perfectly valid to have a #define NULL 0
Null isn't required to be cast to void*.
>No, I'm saying that you can use C++'s concept of default constructors and pass by reference in conjunction with C code.
Can't even parse this garbage because you're talking about constructors for some loony reason. That has nothing to do with this. Hypothetically if we suddenly required C++ to have calls to functions using references be annotated with some symbol. What would break about C?
Nothing to my knowledge. Perhaps you have some idea about what C++ should be that'd break. I don't care. Don't have references in your language if you can't write them properly.
>>55022698
No it's plenty dangerous. And not only that even when it's dangerous it has taken me 20 minutes to compile fairly trivial projects. And that's a fault of the language that greatly impacts how nice it is to write code.
>>55022703
>new programmer makes a comment
Ok, Good. You know what programming languages are. Now go write your own perfect language.
>>55022707
Not him but I don't really see why that'd be much harder. That pattern still looks fine.
>>
>>55022722
Machine code is normally turing complete
>>
>>55022755
>Well It's literally right there "an intereger constant expression with the value 0".
Which was my point. In C, 0 and (void*) 0 are treated equal.

In C++ they're not, except for a few edge cases where it is (because of implicit conversions due to backwards compatibility with C). This is why C++11 introduced nullptr which doesn't equal 0.

Are you having a hard time or what?

>Not him but I don't really see why that'd be much harder. That pattern still looks fine.
Why would you write such code when RAII exists? I swear, you autists like writing a lot of code just for the sake of writing a lot of code.
>>
>>55022755
>Can't even parse this garbage because you're talking about constructors for some loony reason. That has nothing to do with this. Hypothetically if we suddenly required C++ to have calls to functions using references be annotated with some symbol. What would break about C?

struct type
{
int value;
type(int value) : value(value) {}
};

void foo(const type& object)
{
printf("%d\n", object.value);
}

int main()
{
foo(2);
return 0;
}


The above would break.
>>
[<not_portable>]
Reminder that C functions can read their own machine code
>>
>>55022728
if you are talking about removing the last digit in base 10, it is likely that the new number is not representable in floating point.

If all you want is removing last digit in binary, just binary AND it with integer which sets corresponding bit to 0
>>
>>55022728
>memset on statically sized buffer
>register
>Using string operations instead of floating point ones

>last digit in the mantissa
Base 10 or base 2?
>>
>>55022728
Pretty hacky way of doing it, but meh
    float x = 1234;
std::cout << x << std::endl;
int bottom = (int) x;
int top = bottom;
bottom <<= 9;
bottom /= 10;
bottom >>= 9;
bottom *= 10;
top >>= 23;
top <<= 23;
x = top | bottom;

beware of endianness though.
>>
undefined but not uninteresting
void f() {
for (char* cpointer = f;;++cpointer)
printf("%u ", (unsigned int) (*cpointer));
}
>>
File: 1461131877335.png (377 KB, 500x654) Image search: [Google]
1461131877335.png
377 KB, 500x654
Hi guys,

I'm looking to access my sql database that is running on xxamp remotely. How should I set this up? Currently I can connect using the 'local host' url but I presume I can't use something like this remotely? Do I need to find a company to host it or something?

Sorry I'm very new to this and I'm really confused.
>>
>>55022802
>std::cout << x << std::endl;
Absolutely disgusting.
>int
Way to make it not work on almost every input.
>beware of endianness though.
Your code has no problems with endianess, but does rely on the size of int and float.
>>
>>55022804
>undefined
I don't see any undefined behaviour..
>>
>>55022828
Obviously should be uint_32 but whatever. Float is defined as 32 bits if you're working in IEEE754 which you should be.
>>
>>55022844
It is like every other expression is undefined behavior anon.
>>
>>55022844
dereferencing "function memory"
>>
>>55022786
Decimal I get the long double with a variable number of digits in the mantissa and I need to remove the very last digit from it, just the mantissa. I wanted to using floating point operations but the problem is that the double or long double always has trailing digits in the back Like if in my program I get 0.76 it turned out it had .0760000000000512, not 0.760000000000. So I had to use string operations not to botch anything and get exactly what I need.
>>
>>55022707
I don't see why it'd be any different. You simply have to understand the semantics of whatever library you're using.

>>55022769
>Why would you write such code when RAII exists? I swear, you autists like writing a lot of code just for the sake of writing a lot of code.

Because I write C, not C++.
>>
>>55022769
>Are you having a hard time or what?
Anon. I think you're getting a bit defensive here for no reason.
I said
>that's not what windows does in its API
Ok. SO we got that.
>If that's true, then Windows API is invalid C for trying to redefine NULL.
Ok? So You're telling me they're not doing valid C when they're doing #define NULL 0.
You post an image which says that C permits non-void* NULL.
>C++ introduces nullptr
Ok. But what does that have to do with anything? You did
int a=5;
int& n=a;
n=NULL;

And said it was illegal. But I said that NULL could be 0. So I don't see why assigning n (a) to 0 would be illegal. It isn't.
Sure, I'm fairly certain n=nullptr would be illegal in C++. But that's not what you wrote.
>>55022776
>the above would break
You mean the
int main(){
foo(2);
return 0;
}
bit? Why is our old C code calling our new C++ code? That has nothing to do with backwards compatibility in the sense that C++ uses it. If we can't call C functions or compile C functions that's breaking C backwards compatibility.
int cFunction(int *oldPointer, int someInteger){ //This is old C, it compiles
return *oldPointer+someInteger;
}
void cppFunction(int& newFancyReference, int someNewFancyInteger){ //This is C++ it compiles if we're in a C++ compiler
differentAndCoolValue=newFancyReference+someNewFancyInteger;
return differentAndCoolValue;
}
int main(){ //This is C++ it compiles if we're in a C++ compiler
int oldBoringCResult;
int newSuperCoolCppResult;
int a=5;

int& n=a
int* oldPointer=&a;
n=NULL; //just because we can
//n=nullptr; This didn't work though..
newSuperCoolCppResult=cppFunction([ReferenceSymbol]n,5);
oldBoringCResult=cFunction(oldPointer,2);
return 0;
}

It's not that complicated. This would work. But the C++ committee doesn't recognize issues with their language.
>>
>>55022863
>>55022870
I didn't even notice the function name at first. I don't think dereferencing "function memory" is undefined behaviour though.
The assignment isn't valid though, as the standard doesn't guarantee that "normal" pointers and function pointers are compatible (POSIX C does however).
I can't really be bothered skimming through the standard to find out if I'm right or not. It would be implementation defined at best.
>>
I'm having trouble understanding the different bitwise operators in C, can anybody explain them or show me a good resource on them?
>>
>>55022887
>0.760000000000512
>0.760000000000
Right. That would be floating point rounding errors. You can't represent most decimal numbers exactly in floating point.
>>
>>55022930
& - AND
| - OR
^ - XOR
~ - COMPL/NOT
>>
File: 2016-06-11_141209.png (4 KB, 395x99) Image search: [Google]
2016-06-11_141209.png
4 KB, 395x99
>>55022802
Not what I need fampie.
>>
>>55022887
You are doing this wrong. You can't expect your pretty numbers to be representable in floating point. "0.76" will be converted to 0.7600000512 or what ever number closest to 0.76 and representable in floating point.
>>
>>55022930
>>55022942
>> - SHR
<< - SHL

assignment versions for all except ~
>>
>>55022930
& AND is analogous to intersection.
x & y takes the bits of x that also set in y.
It's for checking if certain bits are set.

| OR is analogous to union.
x | y takes all bits set in both x and y.
It's for merging sets of bits into one.

^ XOR is analogous to our use of "or", meaning "either-or".
x ^ y takes all bits that are set in one but not the other.

~ NOT inverts the bit pattern.
~x makes all the zeros ones and all the ones zeros.
>>
File: Figure 7_1.jpg (66 KB, 1600x597) Image search: [Google]
Figure 7_1.jpg
66 KB, 1600x597
>>55022930
>can anybody explain them or show me a good resource on them?
They're the logical operators just operating on an array of bits rather than the entire value.
See pic and >>55022942
I suggest you write it out in a text file and see what happens in your head when you try and do the thing.
like:
0000
and
1010
is
0000

0101
xor
1011
is
1110

1001
or
0100
is
1101

Shifts are a bit harder to visualize this way perhaps but they're fairly intuitive.
0001<<1=0010
0100>>2=0001
1111<<4=0000 (everything gets shifted off on the left).
>>
>>55023017
Also if you consider the integer value with a shift a shift is just a multiply by 2*the number you shift by if you don't overflow the type.
>>
>>55022919
>Why is our old C code calling our new C++ code?
That's how C++ and C interact.

>That has nothing to do with backwards compatibility in the sense that C++ uses it.
That's literally the meaning of backwards compatibility.
>>
>>55023017
set operations are a bad example for bitwise operations
>>
>>55023017
In the last shift example, what happens to the bits that are shifted out of the value? Do they disappear?
>>
>>55023077
yes
>>
>>55023077
they go to valhalla
>>
>>55023077
Yes. The bits go nowhere. There's some fuzzy things about what happens in special cases when you shift right. But the general case is that you just get zeros coming in from the left.
I don't know of any shift left examples where you get anything other than 0's from the right.

But yeah. When you're doing something special with bitshifts I think you should inspect your code thoroughly until you know your platform. Or read up on it.

The shift is just an operation. It's not an actual physical 'shift'. I think that should be noted. It's probably a big reason people have these confusions.
>>55023101
Technical details anon. He only cares about the practical operation I'm sure.
>>
>>55023075
>set operations are a bad example for bitwise operations
I think it's pretty decent.
What do you think is a better example?
>>
>>55023119
Set operations aren't meant for boolean conditionals
if you represent a boolean as a set it's True | False

but False OR True is False OR True
>>
>>55023318
*rather than just True
>>
>>55021934
Portable version, not as neat, but works.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>

struct bound
{
uintptr_t data;
uintptr_t func;
};

void* bind(uintptr_t func, void* data)
{
struct bound* b = malloc(sizeof(struct bound));
b->data = data;
b->func = func;
return b;
}

void call(void* bound, size_t size, size_t nmemb, const void* in, void* out)
{
struct bound* b = (struct bound*) bound;
void (*func)(void*, size_t, size_t, const void*, void*) = (void (*)(void*, size_t, size_t, const void*, void*)) b->func;
func((void*) b->data, size, nmemb, in, out);
}

void map(uintptr_t func, size_t size, size_t nmemb, const void* in, void* out)
{
if (nmemb > 0)
{
((void (*)(size_t, const void*, void*)) func)(size, in, out);

map(func, size, nmemb - 1, ((const char*) in) + size, ((char*) out) + size);
}
}

void f1(size_t size, const void* in, void* out)
{
int* x = (int*) out;
*x = *((const int*) in) * 2;
}

void f2(size_t size, const void* in, void* out)
{
const int* x = (const int*) in;
*((int*) out) = *x * *x;
}

void print(int* list)
{
printf("[%d", list[0]);
for (int i = 1; i < 4; ++i)
{
printf("; %d", list[i]);
}
printf("]\n");
}

int main()
{
int list_lo[] = {0, 1, 2, 3};
int list_hi[] = {2, 4, 6, 8};
int result[4];

void* print_result = bind((uintptr_t) &print, result);

uintptr_t fs = (uintptr_t) &map;
void* fsf1 = bind(fs, (void*) &f1);
void* fsf2 = bind(fs, (void*) &f2);

call(fsf1, sizeof(int), 4, list_lo, result);
call(print_result, 0, 0, NULL, NULL);

call(fsf1, sizeof(int), 4, list_hi, result);
call(print_result, 0, 0, NULL, NULL);

call(fsf2, sizeof(int), 4, list_lo, result);
call(print_result, 0, 0, NULL, NULL);

call(fsf2, sizeof(int), 4, list_hi, result);
call(print_result, 0, 0, NULL, NULL);

return 0;
}
>>
>>55023365
Just remembered that C++ has

void
const void
mutable void
const mutable void
>>
>>55023365
>Portable
You can't portably cast a function pointer to a void pointer in C.
>>
>>55023382
Not him but good catch. I thought I was the only one autistic enough to care about this.

There's an SO question about this IIRC
>>
>>55023382
>>55023391
reminder that a void* pointer can point to any object type

therefore void pointers are an object oriented feature
>>
>>55023382
Happy?
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>

struct bound
{
uintptr_t data;
uintptr_t func;
};

void* bind(uintptr_t func, uintptr_t data)
{
struct bound* b = malloc(sizeof(struct bound));
b->data = data;
b->func = func;
return b;
}

void call(void* bound, size_t size, size_t nmemb, const void* in, void* out)
{
struct bound* b = (struct bound*) bound;
void (*func)(void*, size_t, size_t, const void*, void*) = (void (*)(void*, size_t, size_t, const void*, void*)) b->func;
func((void*) b->data, size, nmemb, in, out);
}

void map(uintptr_t func, size_t size, size_t nmemb, const void* in, void* out)
{
if (nmemb > 0)
{
((void (*)(size_t, const void*, void*)) func)(size, in, out);

map(func, size, nmemb - 1, ((const char*) in) + size, ((char*) out) + size);
}
}

void f1(size_t size, const void* in, void* out)
{
int* x = (int*) out;
*x = *((const int*) in) * 2;
}

void f2(size_t size, const void* in, void* out)
{
const int* x = (const int*) in;
*((int*) out) = *x * *x;
}

void print(int* list)
{
printf("[%d", list[0]);
for (int i = 1; i < 4; ++i)
{
printf("; %d", list[i]);
}
printf("]\n");
}

int main()
{
int list_lo[] = {0, 1, 2, 3};
int list_hi[] = {2, 4, 6, 8};
int result[4];

void* print_result = bind((uintptr_t) &print, result);

uintptr_t fs = (uintptr_t) &map;
void* fsf1 = bind(fs, (uintptr_t) &f1);
void* fsf2 = bind(fs, (uintptr_t) &f2);

call(fsf1, sizeof(int), 4, list_lo, result);
call(print_result, 0, 0, NULL, NULL);

call(fsf1, sizeof(int), 4, list_hi, result);
call(print_result, 0, 0, NULL, NULL);

call(fsf2, sizeof(int), 4, list_lo, result);
call(print_result, 0, 0, NULL, NULL);

call(fsf2, sizeof(int), 4, list_hi, result);
call(print_result, 0, 0, NULL, NULL);

return 0;
}
>>
Rust really looks like a good language

but what i don't get it does it have manual memory management or doesn't it
it seems like some kind of magic to me that it's not manual but there isn't a gc either
>>
>>55023397
Uh yeah. In C, we call memory-occupying things "objects".

A void pointer can't really point to functions because it was once the case that they were stored in different memories.
>>
>>55023413
C confirmed OOP language and therefore shit
>>
>>55023401
You're still doing it though.
For all intents and purposes, a (u)intptr_t should be considered as a void *.
>>
>>55023426
OK
>>
>>55023397
>any object type
The term 'object' has a different meaning in standard C, than what OOP defines an object as.
>any
That doesn't really capture the meaning of a void pointer properly.
It points to an unspecified type.
>>
File: butthole-man.jpg (178 KB, 716x960) Image search: [Google]
butthole-man.jpg
178 KB, 716x960
>>55023427
>For all intents and purposes, a (u)intptr_t should be considered as a void *.
But that's wrong.
>>
ITT: anons fail to grasp shitposting
>>
>>55023455
A (u)intptr_t is an integer type where the following must hold:
void *ptr = something;
intptr_t iptr = (intptr_t)ptr;
void *ptr2 = (void *)iptr;
assert(ptr == ptr2);

It makes absolutely no guarantees about function pointers. In fact, it's an optional type and is not even guaranteed to exist.
>>
>>55023401
>>55021934
What kind of turbo autist would think that C is functional just because you can write some boilerplate crap ?

Fucking autism
>>
>>55023477
C isn't really functional they're just writing functional code in C

It's fucking hideous in C
>>
>>55023475
Yeah yeah yeah, I'm done trying to satisfy everyone. I made the original anon's example in C only to prove a point.
>>
Just learnt python and now trying to figure out how to pull data from websites
>>
File: dafoe-dafuq.gif (2 MB, 350x300) Image search: [Google]
dafoe-dafuq.gif
2 MB, 350x300
>>55023484
>C isn't really functional they're just writing functional code in C
Uhm, so you're saying it isn't functional but it is?
>>
Can /dpt/ explain category theory to me in one post?
>>
File: typing2.png (99 KB, 589x596) Image search: [Google]
typing2.png
99 KB, 589x596
>>55023515
No, but I can explain to you why you shouldn't ask /g/ about it in one post.
Thread replies: 255
Thread images: 20

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.