C was a mistake

C was a mistake

Why does that language sometimes do chaotic shit like this?

pastebin.com/ww2waR3n

I have two arrays with 2 elements and a function that combines two arrays and outputs an array (also 2 elements).

Yet when I print out the value of x, the value of w gets printed instead. WTF ZUCKERBERG

Other urls found in this thread:

opensource.googleblog.com/2017/01/grumpy-go-running-python.html
twitter.com/SFWRedditGifs

Your function is returning pointers to a single static-lifetime array that is mutated by each call to it. The latter call overwrites the values from the previous one.

Exactly this. Returning pointers to variables declared inside functions is generally a terrible idea too, even if they're not static. I could be wrong since it's actually been a while since I used C, but if you return a pointer to something which lives on the stack you have no fucking clue what happens to that shit once you actually use the stack some more.

He's also passing a double pointer to his function, because he's taking the memory address of an array.

Fucking learn C before you bitch about it, op. Start with k&r

>int main()
>{

opinion into the trash

> returns a pointer to a static
> not knowing what that implies

C gives you the kind of power few other languages can give you. In the hands of a good programmer, the ability to return a pointer to a static can be quite useful and efficient. But you abused that power, because you didn't understand the responsibilities that come with it.

Somehow I knew even before I clicked that this would be some kind of allocation/lifetime misunderstanding. Yup, it was.

Let better languages become more mature.

I'll let Rust and Golang to fight it out

use rust or vala then.

C is for retards who will be responsible for some major CVE 2-3 years down the line.

what?

Rust and Go are completely different.

One is actually a modern language with advanced syntactic features and design and the other is some abortion written for dipshit "C tier" Pajeets who don't know shit about good design.

>static double result[2];

>return result;

never code again

> complains about style
> it was literally K&R style

Every year, the IQ of Sup Forums just inches down a little bit more.

The IQ is approaching the asymptote of Sup Forums.

Just learn Go... it has already replaced C

>One is actually a modern language with advanced syntactic features and design and the other is some abortion

Which is which?

can someone ID those speakers

What the fuck have you been smoking?

when would returning a pointer to a static be useful?

Just trollen... Nobody actually thinks that

>if you return a pointer to something which lives on the stack you have no fucking clue what happens to that shit once you actually use the stack some more

That is correct. Returning a pointer to a static is potentially safe, if the caller understand how to use the pointer responsibly. Returning a pointer to a local is never safe and is always wrong.

GARBAGE COLLECTION

D R O P P E D
R R
O O
P P
P P
E E
D R O P P E D

Go is the pajeet one.

It's literally a step backwards in language design. I have no fucking clue why anyone would want to use that abortion.

Do you think that no garbage collection means that suddenly malloc() and free() don't actually take any time? Taking care of memory manually and naively actually has worse performance than even a trivial tracing garbage collector.

When will Rust have a good introductory book?
When will Rust get nice library bindings like C/C++

opensource.googleblog.com/2017/01/grumpy-go-running-python.html
>its j-just a m-meme! Nobody will u-use it!

RAII doesn't suddenly free you from allocation and freeing overhead, it just makes it more predictable.

And it's not as fast as garbage collection.

>our language is so shit we use it as a compiler target

You don't have to heap allocate everything unless your language is fucking retarded.

nigger what are you doing in my living room?

That's a memory leak. You can't use a pointer to a variable that's out of scope. You have no guarantee that it will even exist.

Where am I doing that exactly, and to what variable pls

That's not how a memory leak works and the variable is static so it will exist anyways. Wrong on both counts.

include

>oh, hello honey :3, this is Jamel, your new father

Or just make the function take a third "result" parameter and have the caller provide the output array via that.

OP HERE

OK PLS

what to change so my program works? I removed "static" from the function and now it's even worse

That's my point, abuse of heap allocation is the reason why most garbage collected languages are slower than C/C++, not the use of GC itself.

But then you take languages like Go that have structs as value types and no real "classes", and they blow JVM languages out of the water, performance-wise.

>static
WHAT ARE YOU DOING MAN

Is that fucking Hannibal?

Are you retarded? Whether it's faster or not depends in a lot of stuff, which is why people are still arguing about that.
people.cs.umass(DOT)edu/~emery/pubs/gcvsmalloc.pdf
This paper explains that GC would need 5 times the memory available to be as fast or slightly faster

what's wrong with static explain pls

>putting main at the bottom of your code
trash

Fucking harder daddy,

>when would returning a pointer to a static be useful?

It's perfectly fine when it's a pointer to const. For example:

const double *identity_matrix()
{
static const double id[] = {1.0, 0.0, 0.0, 1.0};
return id;
}


It's also fine when the pointer points to a singleton resource, of which there is only one instance of -- and (crucially) all the callers are aware of that fact. Like for example, the standard library function tmpnam(NULL) returns a pointer to a static that contains the name of a temporary file. Provided the user knows that the name is valid only immediately after the call, it's good because reusing the same buffer space is an efficient design. That kind of efficiency can be important in code that has hard real-time requirements, like interrupt handlers in embedded systems. But I tend to stay away from it in higher-level code because I never quite know the IQ of the programmer who's going to end up maintaining the code.

>I removed "static" from the function and now it's even worse
Your function is now returning an address to its temporary working space that is no longer valid after the function call finishes. Yes, it's worse.

Make it either explicitly allocate some memory for the result and return a pointer to that, or try >structs as value types
Respect for Go slightly increased.

Thanks. I kind of figured Rust was superior to Go, but I know so little about both.

when you want to initialize or allocate some data only in the first call
if no call occurs nothing is allocated
singletons are a use case

also a lot of standard C functions use static data (time_t to struct tm conversion functions like localtime and gmtime, some string functions like strtok)

you should be sure that you know what you're doing before using this as the initialized data is global and not thread safe (unless you mutex the access to it)

Sure looks like him

Humans races =/= dog breeds. Equating them would be kinda dumb.

That's actually a pointer-to-array, which is slightly different from a pointer-to-pointer. The type is double (*)[2], and you can do some interesting stuff with sizeof or pointer arithmetic to find the difference.

double y[2];
double *x = &y;


I'm not sure if this is allowed, since it implicitly converts &y, which is a double (*)[2], into a double *. If it is allowed, it probably does the same thing as
double y[2];
double *x = y;

since both y and &y point to the same place.

TLDR: Yeah, C is terrible.

they're both shit

C/C++ dev here. Rust honestly looks like a little step in the right direction.

This is coming from a person who says C# and Java has NO place in this world

it doesn't matter whether rust is half-decent or not when it's being developed by SJW diversity feminazi nigger cucks

Do you think your lonely autistic pol rambling has any meaningful effect outside your chamber of virginity?

saved for future shitposting on Sup Forumslution

Things you should not return:

1. Pointers to local memory
2. Pointers to static memory

Things you should return:

1. Pointers to dynamically allocated memory
2. Actual values (i.e. a struct containing an array)
3. A status code or nothing, while passing in a pointer to a buffer to be written to.

That is a really convincing Real Doll.

How long until they can talk and have a full motion range mouth and tongue?

>Uses Windows, Mac OS X, Linux, or another OS.
>Doesn't realize that without C, none of these would exist.

kek

Fuck you, I return pointers to static memory buffers all the time.
If you want thread safety, write a fucking strdup and copy my const char arrays yourself.

>sent from my PDP-11

Eh, it's pretty similar just different levels of extremity.

Since stuff like sizeof() is part of preprocessing, you can't sizeof an array passed to a function, as that could change at runtime.

you can sizeof() stack arrays

tfw no hannibal gf

Rust is too complicated.
Compiler scream all the time
I say shut up compiler
He no shut

>shoes on the sofa
Let the nigger have her.

>Returning pointers to variables declared inside functions is generally a terrible idea too
No it itsn't if it's declared in the heap.

Since x points to results just like w, they both refer to the same values -3 and 6.
Use
double *result = calloc(2, sizeof(double));

to get needed behavior.

Pretty sure you all got trolled

cuck

Only in the same scope they're declared; doesn't work for arrays passed to a finction

OP here, I modified the function to a void and removed the static and now it works thx all

C wasn't a mistake, you are

this tbqh

Where can I get me this shirt

get out Moreg

Love this kind of topics, kids cant into C, this is tragic, OP probably javascript kiddie who cant even understand scoping

GET OUT MOREG REEEEEEEEEE

...

You're dead nigga!

You can pass pointers of automatic variables only when you're using recursion.

If you use a separate function to initialize a structure or an array you have to malloc it first

takarodj te russian spy or i will buy a nigger to cuck u

Except even if you remove the static keyword you'll get the same behavior.

>these are the same people who will argue with you in another thread
This is why I don't take anyone here seriously
because I can't help but imagine them as this retard OP who can't even into basic C, talking shit about things he knows nothing about
After this he will probably open an amd vs nvidia thread with fake benchmarks

get rekt autist shitter

gcc -Wall -Werror
won't compile because you are returning pointer to stack-allocated variable

OP wanted to test us.
OP thinks /g is full of retards like him
OP uses "static" secretly
OP if faggot.

c is the white mans language.

first learn about the stack nigger, you have to malloc when you return memory from a function.

Fuck off disgusting nigga

OP here, it's true, I just trolled you all, and you proved to me that you are retards, stay mad faggots.

When will Rust get out of beta?

without c there would not even be a pdp-11

>he fell for the fast GC meme

If you think garbage collection is slow, stop using dogshit garbage collected languages that heap-allocate a billion reference-counted objects.

What's wrong with Java? You can even run Alpaca Lang on it via the Erjang VM.

When will Half Life 3 be released?

The sad part about C is, even in the 60s and 70s there were far less error-prone languages. "Worse is better" is the ONLY reason C became so widespread. It was easier to implement and that's all.

Sizeof isn't part of preprocessing (at least, it can't be done SOLELY at preprocessing in a C99 complaint compiler). It accurately returns the size of VLAs if used in the scope they're declared, and that can't be done at compile time. Doing the calculation at compile-time for other types is however a common optimization.

"Recursion" is misleading, since many will take it to mean a function calling itself, or otherwise forming a cycle in the graph of function calls. But declaring variables in main(), and passing them via pointer to sub-functions called from main, is common and valid, though it's not what comes to mind when people hear "recursion". Basically the problem only exists when you try to dereference a pointer that points to something on a dead stack frame.

My criticism isn't of the C language, it's of using K&R style in 2017.

>If you think garbage collection is slow, stop using garbage collection
FTFY.

Not them, but it's slow (JVM start up and sending your personal data to the NSA), doesn't support operator overloading (making custom numerical types a nightmare to work with), forces you to use OOP, and uses excessive memory.

About two weeks before ReactOS/HURD becomes usable.

Like what, FORTRAN? COBOL? BASIC? None of those are good systems languages, the area in which C excels. UNIX was made by programmers, for programmers, so it made sense to use the same language for the OS and applications. Issues only began later on, when nobody really standardized on a good, high-level, general-purpose application language.

Nothing wrong with Java.

That is, if you're a total cuck.

C invented systems programming. Before that there was only assembly.

>language that is a perfect assembly abstraction
>blame language because you don't know how computers work

Op pls

You can always tell when someone's favorite language is C.
It makes their code unbearable because they always try to write C-like code in whatever language they're in, even down to the autistic mannerisms like 3 letter variables and const correctness.

real programmers don't need const