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.
Nathan Cruz
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.
Isaac King
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
Anthony Bell
>int main() >{
opinion into the trash
Elijah Morales
> 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.
Gabriel Brown
Let better languages become more mature.
I'll let Rust and Golang to fight it out
Colton Watson
use rust or vala then.
C is for retards who will be responsible for some major CVE 2-3 years down the line.
James Robinson
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.
Brayden Allen
>static double result[2];
>return result;
never code again
Luis Mitchell
> 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.
Dominic Hughes
Just learn Go... it has already replaced C
James Smith
>One is actually a modern language with advanced syntactic features and design and the other is some abortion
Which is which?
Asher Williams
can someone ID those speakers
Ayden Nguyen
What the fuck have you been smoking?
Jose Sanchez
when would returning a pointer to a static be useful?
Cooper Clark
Just trollen... Nobody actually thinks that
Dylan Brooks
>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.
Ayden Rogers
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
Gabriel Garcia
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.
Easton Cox
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.
Samuel Wilson
When will Rust have a good introductory book? When will Rust get nice library bindings like C/C++
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.
Josiah Hill
>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.
Nicholas Edwards
nigger what are you doing in my living room?
Elijah Lewis
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.
Noah Robinson
Where am I doing that exactly, and to what variable pls
David Myers
That's not how a memory leak works and the variable is static so it will exist anyways. Wrong on both counts.
Brody Butler
include
Owen Torres
>oh, hello honey :3, this is Jamel, your new father
Logan Gomez
Or just make the function take a third "result" parameter and have the caller provide the output array via that.
Kevin Kelly
OP HERE
OK PLS
what to change so my program works? I removed "static" from the function and now it's even worse
Carson Garcia
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.
Connor Lewis
>static WHAT ARE YOU DOING MAN
Thomas Johnson
Is that fucking Hannibal?
Jaxon Torres
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
Cooper Myers
what's wrong with static explain pls
Camden Mitchell
>putting main at the bottom of your code trash
Jacob James
Fucking harder daddy,
Adam Williams
>when would returning a pointer to a static be useful?
It's perfectly fine when it's a pointer to const. For example:
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.
Christopher Anderson
>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.
Isaiah Gutierrez
Thanks. I kind of figured Rust was superior to Go, but I know so little about both.
Jason Martin
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)
Aiden Morgan
Sure looks like him
Humans races =/= dog breeds. Equating them would be kinda dumb.
Levi Hall
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.
Jacob Kelly
they're both shit
Wyatt Green
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
Jaxon Rodriguez
it doesn't matter whether rust is half-decent or not when it's being developed by SJW diversity feminazi nigger cucks
Jack Flores
Do you think your lonely autistic pol rambling has any meaningful effect outside your chamber of virginity?
Wyatt Rogers
saved for future shitposting on Sup Forumslution
Josiah Robinson
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.
Dominic Brown
That is a really convincing Real Doll.
How long until they can talk and have a full motion range mouth and tongue?
Jacob Wood
>Uses Windows, Mac OS X, Linux, or another OS. >Doesn't realize that without C, none of these would exist.
kek
Adrian White
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.
Benjamin Taylor
>sent from my PDP-11
Charles Stewart
Eh, it's pretty similar just different levels of extremity.
Connor Hughes
Since stuff like sizeof() is part of preprocessing, you can't sizeof an array passed to a function, as that could change at runtime.
Aaron Bennett
you can sizeof() stack arrays
Chase Gutierrez
tfw no hannibal gf
Liam Rogers
Rust is too complicated. Compiler scream all the time I say shut up compiler He no shut
Chase Robinson
>shoes on the sofa Let the nigger have her.
Cooper Perry
>Returning pointers to variables declared inside functions is generally a terrible idea too No it itsn't if it's declared in the heap.
Evan Lewis
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.
Jeremiah Nelson
Pretty sure you all got trolled
Andrew Murphy
cuck
Brandon Gomez
Only in the same scope they're declared; doesn't work for arrays passed to a finction
Owen Anderson
OP here, I modified the function to a void and removed the static and now it works thx all
Isaac Thompson
C wasn't a mistake, you are
Christopher Moore
this tbqh
Levi Brown
Where can I get me this shirt
Nicholas Powell
get out Moreg
Jayden Williams
Love this kind of topics, kids cant into C, this is tragic, OP probably javascript kiddie who cant even understand scoping
Robert Murphy
GET OUT MOREG REEEEEEEEEE
Michael Butler
...
Kevin Williams
You're dead nigga!
Carson Myers
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
Isaiah Diaz
takarodj te russian spy or i will buy a nigger to cuck u
Sebastian Gray
Except even if you remove the static keyword you'll get the same behavior.
Brayden Anderson
>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
Michael Edwards
get rekt autist shitter
Wyatt Hughes
gcc -Wall -Werror won't compile because you are returning pointer to stack-allocated variable
Luis Reed
OP wanted to test us. OP thinks /g is full of retards like him OP uses "static" secretly OP if faggot.
Gavin Diaz
c is the white mans language.
first learn about the stack nigger, you have to malloc when you return memory from a function.
Angel Wright
Fuck off disgusting nigga
William Sanders
OP here, it's true, I just trolled you all, and you proved to me that you are retards, stay mad faggots.
Joseph Turner
When will Rust get out of beta?
Daniel James
without c there would not even be a pdp-11
Luke Sanchez
>he fell for the fast GC meme
James Richardson
If you think garbage collection is slow, stop using dogshit garbage collected languages that heap-allocate a billion reference-counted objects.
Brandon Young
What's wrong with Java? You can even run Alpaca Lang on it via the Erjang VM.
Evan Diaz
When will Half Life 3 be released?
Camden Cox
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.
Caleb Sanchez
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.
Elijah Cooper
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.
Henry Baker
Nothing wrong with Java.
That is, if you're a total cuck.
Alexander Thomas
C invented systems programming. Before that there was only assembly.
Juan Carter
>language that is a perfect assembly abstraction >blame language because you don't know how computers work
Op pls
Noah Torres
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.