Anyone look at C code and ask "WTF am I looking at"?

Anyone look at C code and ask "WTF am I looking at"?

It only happens with C code for some reason. Am I just retarded or did C rape my mom and made me watch as a kid.

Other urls found in this thread:

pastebin.com/GQPnUi7d
code.metager.de/source/xref/gnu/coreutils/src/yes.c
github.com/freebsd/freebsd/blob/998defe1942c7aa1a2e39bf6a1ae9e61082bb3b1/usr.bin/yes/yes.c
twitter.com/NSFWRedditVideo

Yes. I don't know anything about programming so it's really confusing to me.

Borzoo can be tricky at times. Dont worry too much. Ask on piazza man.

well for one if you've never worked with threads then yeah you're doing to be confused

But I did though user.

I did not take Borzoo, I went to a college in Georgia and graduated a few months ago.

Also it is not just threads. Sometimes C code gets long and it is utterly unreadable for me. I am wondering if some people have the same problem.

Nope, pretty sure it's just you.
Taking notes helps

You probably just don't understand C pointer syntax.
C is actually a pretty clean language overall, but I will agree that there are some particularly ugly/unreadable parts, particularly function pointer syntax.

I find POSIX threads calls and concepts to be hideous. They make me feel dirty.

I actually like Win32 thread APIs much more.

>Win32 thread APIs

Good thing D exists. Far more elegant.

(you)

What font?

fira mono

Actually looks pretty good.
Fuck C is ugly.

ASM > C > C++

everything else is considered coding art

You know, I'd go so far as to say Exceptions exist to state the error and give the location as is, so my catching such exceptions is redundant unless the error is entirely expected and we want to continue running the program. In the case that we want it to stop, we shouldn't catch. So, modified code with a nice change of theme to gruvbox.

Yessir, Fira Mono.

Half or more of C code can be memory manipulation, with the actual computation being done buried somewhere within.

For example, print_message_function() in the OP? Jesus fucking christ, three lines, two of them memory related. C'mon. I dealt with even worse building a compiler my senior year.

For that, you get stupidly fast code and tiny executables. Is that a fair trade-off for readability? Maybe on an embedded system. Anywhere else, and I'd question a person's sanity.

C is easy
C++ is confusing as fuck

Wait, why do you have to cast to a void pointer? That doesn't make sense.

but C is simple and easy to understand. C++ on the other hand...

pthread_create takes a void* argument to pass to the thread function

might be hiding a compiler warning about types?

Is also easy to understand.

The void * cast is completely pointless, as impliciy void pointer conversions are well defined.
The void * cast would only be necessary if you were trying to pass an integer (intptr_t) as the argument.

That's what I feel when looking at assembly.

I mean, I want to get good at C, but reading assembly is painful...

>Half or more of C code can be memory manipulation
Well yeah, C is little more than assembly with variables, simplified loops and some slightly improved syntax.

Good joke.

#include
#include
#include
#include

using namespace std;

mutex cout_lock;

void print_message(const string& str)
{
cout_lock.lock();
cout

Is thousands of orders of magnitude easier to understand.

This is wrong, may deadlock.

C and C++ always looks good and it looks fantastic if you know what your doing. Have a look at some of Linus torvald's code. Its pretty

>not understanding that program

>using namespace std;
no

How can it deadlock?

It's for the sake of the example.

this_thread::get_id() may throw indirectly
even if it didn't you should prefer to use RAII in C++ so use std::lock_guard instead of manually locking/unlocking in the print_message function

>C++ always looks good
have::not::seen_template::?

>this_thread::get_id() may throw indirectly
It may not, it's declared noexcept

>even if it didn't you should prefer to use RAII in C++ so use std::lock_guard instead of manually locking/unlocking in the print_message function
I agree, but this is an example.

>It may not, it's declared noexcept
you're absolutely right, i stand corrected, not sure why I was convinced that it did

well color me surprised, someone on Sup Forums isn't fucking retarded.

That's extremely basic C code, of course it's going to be hard. In the real world, there's just a bunch of functions and pointers you manipulate because everything is encapsulated.

See how I do it with the framework used at my job:

#include
#include

#define NUM_THREADS 1337

void main()
{
dcp pool;

dcp_configure(pool);
dcp_size(pool, NUM_THREADS);
dcp_run(pool, println_stdout, "This is thread @s!", GTM_SELF_ID);

dcp_start(pool, NUM_THREADS);

if (invalid(dcp_checkon(pool, GTM_STATUS_START))) {
iterate_gtm(pool, GTM_STATUS_OP_INVALID, println_stdout, "Thread @s failed to start!", GTM_SELF_ID);
}

dcp_waitall(pool, 10000);
if (invalid(dcp_checkon(pool, GTM_STATUS_END))) {
iterate_gtm(pool, GTM_STATUS_OP_INVALID, println_stdout, "Thread @s failed to close!", GTM_SELF_ID);
dcp_defend(pool);
}
}

That's a lot more readable already, it does the same thing with as many threads as you want.

There are thousands of great C libs out there, use them, there's no reason to reinvent the wheel with every piece of software.

On the scale of C code abominations, this is actually 10/10 readable. The only unnecessarily shitty names are iret1 and 2.

look at some optimised cuda code. That shit is fucking unreadable

This

This is horrible C: pastebin.com/GQPnUi7d

You can't even tell what it's going to output.

Gnu yes: code.metager.de/source/xref/gnu/coreutils/src/yes.c
readability 2/10

BSD yes:github.com/freebsd/freebsd/blob/998defe1942c7aa1a2e39bf6a1ae9e61082bb3b1/usr.bin/yes/yes.c
readybility 10/10

GNU/faggots btfo

it looks like an antiquated way of working with pointers

Listen OP, I was literally you a year ago and fucking HATED how C is "unnecessary overcomplicated and ugly"

The program you posted is basic at best and is actually very easy to learn it, you're probably getting confused by the pointers, which is normal because they really are confusing to a newbie. Have a better example here , which is clean and simple.

The only way to overcome this is to keep throwing yourself at the wall of complexity that you experience as a newbie until it's so burned into your brain that you no longer find it complex. Even reading books/lectures/videos wont work, you really must throw yourself into that dark room of unknown and gain experience by bruteforcing your way into it. If you think it's complex and you're the only brainlet who feels that way, no, everyone felt like that, they just didn't give up and kept bruteforcing their way without having the slightest idea what they were doing until they finally had an idea of what they are doing.

I'm pretty sure it's bad practice not to catch errors when you create threads, because it can fail (there's a limit on the number of threads in the OS and such). Same goes for file creation and reading; you may not have read or write permissions, the disk may be full, file may be too big to store in memory, there may be a limit on the filesize if it's a FAT partition, etc.

What is your opinion on D vs C++

What the sweet monkey fuck is that. I consider myself competent at c, but jeez man.

what do you mean by this? because of the unnecessary explicit casts

This is decompiled code. Right?

>type man pthread_create into terminal
>hit enter
>understand how pthread_create works
is this really that hard?

Pointers are an double edged sword, if you don't know what you're doing they will make your life miserable, if you know what you're doing they extremely powerful.

void* casts are also necessary when passing pointers to variadic functions, like printf, since on some systems, like the Cray 6000, word pointers are a different size than byte pointers.

>commented code
>well named variables
>simple as fuck logic

If you don't know WTF you're looking at in this sample then you should not be a programmer. Period. Either change majors at college or quit your job and find a career more suitable for you.

Having said that: some C code is obscure because C developers sometimes have two bad habits:
* They like to use single letters for variable names.
* They like to use abbreviated function names.

And for a third good reason:
* C solutions will sometimes make use of lower level techniques for performance and/or memory efficiency. Shit that programmers in higher level languages wouldn't think of or wouldn't be able to implement.

Sorry for late reply guys.

It was something I made back in the day for shits and giggles.

No, it's deliberate obscure code that does all sorts of dirty tricks.

>not using RAII

I'm trying to convince people who program C+pthreads to use C++, they can't into RAII

>int g
>void h()

Death to those who insult decent naming conventions!

>Having said that: some C code is obscure because C developers sometimes have two bad habits:
>* They like to use single letters for variable names.
>* They like to use abbreviated function names.
Also:
Putting values and functions into global space, turning a library into a singleton object for no good reason. Like CPython or Yacc/Bison/Lex/Flex

C metaprogramming is even worse:
STACK(int) stack;
CALL_METHOD(STACK(INT), init)(&stack);
CALL_METHOD(STACK(INT), push)(&stack, 5);
printf("%d", CALL_METHOD(STACK(INT), pop)(&stack));

>ITT
>All those college/dropout NEET trying to desperately fit in 'C' domain

D is way more readable with an even better template system, along with a huge number of functional, contract, and concurrency tools. And if you don't want to use the mark-and-sweep GC, various allocators exists, and combined with scope guards, you can always put allocation and deallocation code right next to each other; you have guarantees deallocation will happen because of the scope guards.

No horseshit with hiding the type of an allocation behind std::unique_ptr or std::shared_ptr, which are cluttery enough as it is.

In the OP's C code, when a thread creation fails, we immediately print the error and exit. That's a primitive Exception. If this were a giant web app or something and we didn't want the whole thing to crash down when this thread creation fails, of course we'd catch. But that's not the intent of OP's code.

c is the language of the worst kind of anal retentive bastard who requires obfuscation in all he does, to keep people away from the shitty little empire peoples of that type build within organisation. people who like C love regex. you don't understand this logical miasma .. which shaves a picosecond off operation @ the cost of 6 months down the line of someone trying to maintain or rewrite the shit coherently

By "various allocators", I mean both a standard library experimental allocator, as well as a ton of community-made allocators in various libraries built on C stdlib.malloc, etc.

>C is actually a pretty clean language overall

>If you think it's complex and you're the only brainlet who feels that way, no, everyone felt like that, they just didn't give up and kept bruteforcing their way without having the slightest idea what they were doing until they finally had an idea of what they are doing.
This is literally the only way.

Typical Sup Forums. Can't make any kind of argument, so you just pretend anyone who doesn't share your opinion is an idiot. At least the first guy owned up to having a purely subjective opinion.

you're just a little nigger

C is almost intuitive unless there are tons of defines(see any winapi code). C++ is way worse(templated code is basically write only, see STL sources).

You are missing error message on OPs one.

Basically everyone but the most massive on Wincucks agree that the Win32 API and anything related to it is fucking terrible.

That happens to me with Javascript. For whatever reason the patterns that come from writing Javascript screw with my eyes and are really fucking hard for me to parse and read.

>massive on
massive of*

I know that feel man. Thank god Java exists.

Read the pthread man page and you will be fine. I hope you know how to work with pointers.

If you went to Tech you should know C well. If you went to UGA, well, just find a Tech grad and offer him a blowie. I'm sure you'll get a nice retail gig or something out of it.

C is my main language and yet I usually feel bedazzled whenever I bump into any sufficiently complicated C code.
It's normal, C is not the most readable languge at a glance, you usually need to read carefully

Not really. In fact, C and C++ are basically the only languages where I can always figure out exactly what it's doing. A language I have had that problem in, is Groovy.

>hurr... using a platform depending threading library is only subjectively worse than using a cross platform library that does the same thing

said not a single person ever

>C++ ... the only languages where I can always figure out exactly what it's doing
Are you fucking serious? Have you ever seen any C++?
Invisible references leading to invisible side effects
Arbitrary operator overloading, so you can't trust simple operators.
RAII, leading to all sorts of invisible code being executed.

I write C++ almost exlusively for my personal projects wherever Ruby is not fast enough. I have no idea what the fuck you are even talking about regarding invisible references despite having used the language regularly for 12 years.

Whatever you're doing with references that is causing you problems, it's probably your own fault.

Here is a snippet of C++ code.
The prototype of 'fn' has deliberately been left out, but it will not print anything.
fn is not a macro.
int a = 10;
fn(a);
std::cout

10 right?

How could the function possibly alter a if you didn't pass it a pointer to a?

...

I don't know what you are trying to prove with this example, but if I wanted to answer your question I would have to know what the implementation of fn is. This is something that could be done very quickly and easily and should be done regardless

Because you are a shitty programmer who thinks languages should behave according to your personal preferences and a samefag as well

Invisible references.
The real answer is: "There to no way to tell; you cannot trust C++ code".

>hurrr I can't hover my mouse above the method for a second to see what the types of it's parameters are
Literally retard tier

eh
I learned C with that book but now that I look back at it, I feel ashamed.
And no amount of K&R will shake off that feeling

To be fair, even ignoring the type of a and whether it's passed by reference or value, fn could theoretically act directly on the program's memory space and change the value anyways.

There is no way the program could do that without undefined behaviour, so no it can't.

So now I need to have every single snipped of code I want to read open in some shitty bloated IDE?

That doesn't make sense. How would it locate the variable in memory when all it has is a copy of it?

She picks 2. Choice dereferences to the first int in the array, which is 2.

If you are working on a project that is more than source 1 file and you aren't using an IDE than you are a shitty programmer. Fuck off with your imaginary problems

a is a local var = its on the stack
if you know how big the frame is, you could just step down one frame on the stack

C is great OP's pic is straight forward and OP is retarded.

If you think this is ugly you haven't seen Java EE "business logic".

See You're a fucking idiot.
You shouldn't need a fucking IDE to just be able to READ code. I read code on Sup Forums and sites likes github all of the time.
I like to be able to reason about programs and not have to try remember the exact of prototype of every function at the same time.