Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Search - "function pointer"
-
FUCK! agshdklgdahgisdahl;k!
I just spent 45 FUCKING MINUTES debugging try to figure out WHY THE HELL a function that is supposed to return either a pointer to a valid object OR ZERO if a valid object is not found, was RETURNING FUCKING EIGHT!
Then I saw it... I typed:
nodeList[index];
instead of:
return nodeList[index];
It took me looking at a stack trace and a disassembly of the function to realize this.
Can't wait for this three-day weekend...18 -
I could bitch about XSLT again, as that was certainly painful, but that’s less about learning a skill and more about understanding someone else’s mental diarrhea, so let me pick something else.
My most painful learning experience was probably pointers, but not pointers in the usual sense of `char *ptr` in C and how they’re totally confusing at first. I mean, it was that too, but in addition it was how I had absolutely none of the background needed to understand them, not having any learning material (nor guidance), nor even a typical compiler to tell me what i was doing wrong — and on top of all of that, only being able to run code on a device that would crash/halt/freak out whenever i made a mistake. It was an absolute nightmare.
Here’s the story:
Someone gave me the game RACE for my TI-83 calculator, but it turned out to be an unlocked version, which means I could edit it and see the code. I discovered this later on by accident while trying to play it during class, and when I looked at it, all I saw was incomprehensible garbage. I closed it, and the game no longer worked. Looking back I must have changed something, but then I thought it was just magic. It took me a long time to get curious enough to look at it again.
But in the meantime, I ended up played with these “programs” a little, and made some really simple ones, and later some somewhat complex ones. So the next time I opened RACE again I kind of understood what it was doing.
Moving on, I spent a year learning TI-Basic, and eventually reached the limit of what it could do. Along the way, I learned that all of the really amazing games/utilities that were incredibly fast, had greyscale graphics, lowercase text, no runtime indicator, etc. were written in “Assembly,” so naturally I wanted to use that, too.
I had no idea what it was, but it was the obvious next step for me, so I started teaching myself. It was z80 Assembly, and there was practically no documents, resources, nothing helpful online.
I found the specs, and a few terrible docs and other sources, but with only one year of programming experience, I didn’t really understand what they were telling me. This was before stackoverflow, etc., too, so what little help I found was mostly from forum posts, IRC (mostly got ignored or made fun of), and reading other people’s source when I could find it. And usually that was less than clear.
And here’s where we dive into the specifics. Starting with so little experience, and in TI-Basic of all things, meant I had zero understanding of pointers, memory and addresses, the stack, heap, data structures, interrupts, clocks, etc. I had mastered everything TI-Basic offered, which astoundingly included arrays and matrices (six of each), but it hid everything else except basic logic and flow control. (No, there weren’t even functions; it has labels and goto.) It has 27 numeric variables (A-Z and theta, can store either float or complex numbers), 8 Lists (numeric arrays), 6 matricies (2d numeric arrays), 10 strings, and a few other things like “equations” and literal bitmap pictures.
Soo… I went from knowing only that to learning pointers. And pointer math. And data structures. And pointers to pointers, and the stack, and function calls, and all that goodness. And remember, I was learning and writing all of this in plain Assembly, in notepad (or on paper at school), not in C or C++ with a teacher, a textbook, SO, and an intelligent compiler with its incredibly helpful type checking and warnings. Just raw trial and error. I learned what I could from whatever cryptic sources I could find (and understand) online, and applied it.
But actually using what I learned? If a pointer was wrong, it resulted in unexpected behavior, memory corruption, freezes, etc. I didn’t have a debugger, an emulator, etc. I had notepad, the barebones compiler, and my calculator.
Also, iterating meant changing my code, recompiling, factory resetting my calculator (removing the battery for 30+ sec) because bugs usually froze it or corrupted something, then transferring the new program over, and finally running it. It was soo slowwwww. But I made steady progress.
Painful learning experience? Check.
Pointer hell? Absolutely.4 -
Interesting bug hunt!
Got called in because a co-team had a strange bug and couldn't make sense of it. After a compiler update, things had stopped working.
They had already hunted down the bug to something equivalent to the screenshot and put a breakpoint on the if-statement. The memory window showed the memory content, and it was indeed 42. However, the debugger would still jump over do_stuff(), both in single step and when setting a breakpoint on the function call. Very unusual, but the rest worked.
Looking closer, I noticed that the pointer's content was an odd number, but was supposed to be of type uint32_t *. So I dug out the controller's manual and looked up the instruction set what it would do with a 32 bit load from an unaligned address: the most braindead thing possible, it would just ignore the lowest two address bits. So the actual load happened from a different address, that's why the comparison failed.
I think the debugger fetched the memory content bytewise because that would work for any kind of data structure with only one code path, that's how it bypassed the alignment issue. Nice pitfall!
Investigating further why the pointer was off, it turned out that it pointed into an underlying array of type char. The offset into the array was correctly divisible by 4, but the beginning had no alignment, and a char array doesn't need one. I checked the mapfiles and indeed, the old compiler had put the array to a 4 byte boundary and the new one didn't.
Sure enough, after giving the array a 4 byte alignment directive, the code worked as intended.8 -
Definitely Rust, and a bit Haskell.
Rust has made me much more conscious of data ownership through a program, to the point that any C/C++ function I wrote that takes a pointer nowadays gets a comment on ownership.
I wish it was a bit less pedantic about generics sometimes, which is why I've started working on a "less pedantic rust", where generics are done through multiple dispatch à la Julia, but still monomorphising everything I can. I've only started this week, but I already have a tokenizer and most of the type inference system (an SLD tree) ready. Next up is the borrow checker and parsing the tokenized input to whatever the type inference and borrow checker need to work with, and of course actual code generation...
Haskell is my first FP language, and introduced me to some FP patterns which, turns out, are super useful even with less FP languages. -
I spent 4 hours finding a good way to instanciate golang structures. Came up with a function that returned a value or pointer and I just kept reusing the nomenclature for an entire project. Then my buddy looks over and goes, "Ethan, you're a f****** idiot", and shows me the standard nomenclature. Now I have to refactor my entire project. FML.
(Edit: typo)13 -
LOL that's why I love C!
The function pointer cast for strcmp because qsort expects a compare function with two const void * pointers instead of two const char * pointers, that's just beautiful.
Not to mention the hack to abuse strcmp on a struct - which just works because the first struct member is a string and the rest just gets swapped with memcpy as opaque data.
I guess that wouldn't pass a code review at work. :-)6 -
Writing a brainfuck interpreter is a lot fun: Mine does recursion within loops. I also added functions: strings, you can cheat. Stackdump(!), Exit script(*) , go to first cell (^), go to last cell(?), nulling cell (0) It parses this for example: "retoor" ^[.>]. It will dump string retoor. Explanation: the string moves ptr to sixth place. ^ will reset pointer to first. [] is a loop that executes as long there's data in current cell. The "." prints char of current cell (Number if not alpha etc). ">" moves a cell to right. [.>] will thus print until it moved to an empty cell. To move to first, I could've also used my repeater function by adding times to repeat after command: <6 moves six places to left. .>.>.>.>.>.> is also a way to print six chars. +[,.] works as the Linux program "cat". , is one char keyboard input.
Thanks for listening to my tedtalk8 -
I once interviewed for a role at Bank of America. The interview process started off well enough, the main guy asked some general questions about career history and future goals. Then it was off to the technical interviewers. The first guy was fine. Asked appropriate questions which he clearly understood the answers to.
The next guy up, however, was what I like to call an aggressive moron. After looking at my resume, he said I see you listed C++. To which I said, yes I have about 7 years of experience in it but I've mostly been using python for the past few years so I might be a bit rusty. Great he said, can you write me a function that returns an array?
After I finished he looked at my code, grinned and said that won't work. Your variable is out of scope.
(For non C programmers, returning a local variable that's not passable by value doesn't work because the local var is destroyed once the function exits. Thus I did what you're supposed to do, allocate the memory manually and then returned a pointer to it)
After a quick double take and verifying that my code did work, I asked, um can you explain why that doesn't work as I'm pretty sure it does.
The guy then attempted to explain the concept of variable scope to me. After he finished I said, yes which is why I allocated the memory manually using the new operator, which persists after the function exits.
Einstein then stared really hard at my code for maybe 10 to 15 seconds. Then finally looked up said ok fine, but now you have a memory leak so your code is still wrong.
Considering a memory leak is by definition an application level bug, I just said fine, any more questions?4 -
By Thor (not the god, the dragon), Belial and Thor (the god, this time)...
Just got the sources for the software that runs on the SDR for my project. I think I just found the mother of all legacy code:
The whole behaviour is described in a single, 4000 lines C file. Most of the code is in a giant switch with cases selected from an enumeration with names that don't match their function. All varnames are overly long, yet hopelessly unhelpful. And why three fuck would you use pointer[0].data instead of (*pointer).data or pointer->data like a sane person would !? pointer isn't even an array, so why would you use []?1 -
C/C++ is so fucking unreadable. Who had the cancer idea to use abbreviations for EVERYTHING?? If a word is longer than 3 chars you can be sure those psychologically unstable devs will use a shortcut.
Pointer ==> ptr
test_and_set ==> fucking t_a_s
rollback ==> rb
I don't want to play a shitty word guessing game for every fucking function in your horribly documented api.3 -
Making a function pointer in C and typedef-ing it.
Done wrong. Every single time.
Look up in the internet.
Copy a typdef-d function pointer.
It works.
Check it's syntax.
THE SAME FCKIN SYNTAX AS DONE FIRST!
And that's why I hate function pointers.2 -
The setting is a computer lab on campus. The assignment was due tomorrow and I was just finishing up the code. I was a novice at C and programming in general at that time. I finish the ~250 lines of functions or so but behavior of the simple library isn't right. I'm getting wrong values and I cannot find the source - I hate myself for not testing incrementally. Then, after looking for hours piece by piece while looking at references and StackO, I realized that I improperly dereferenced a pointer, something like *(this) instead of (*this) in a function. I didn't even know that I was making a mistake because I missed one of the relevant lectures. After that I realized that the errors thrown by the compiler weren't all that bad...
-
Sometimes my hatred for code is so.. overwhelming that I think I need a sabbatical or should even stop altogether.
Let's face it. All code sucks. Just on different levels.
Want to go all bare metal? Love low level bit fiddling. Well, have fun searching for concurrency, memory corruption bugs. Still feel confident? Get ulcers from large C/C++ code base already in production, where something in the shared memory, function pointer magic is not totally right?
So you strive for more clean abstractions, fancy the high level stuff? Well, can you make sense of gcc's template error messages, are you ready for the monad, leaving behind the mundane everyday programmers, who still wonders about the scope of x and xs?
Wherever you go. Isn't it a stinking shit pile of entropy, arbitrary human made conventions? You're just getting more familiar with them, so you don't question them, they become your second skin, you become proficient - congrats you're a member of the 1337.7 -
I give up. I am having trouble understanding Go lang *, & stuff.
attached code does print what fmt.Println has, but the function calling: FindByID is throwing invalid memory address or nil pointer when it attempts to use the result of the code.
Can someone please explain where I'm wrong?
Error log blames the line of "if" condition even though fmt does print its output to console16 -
What in the holy hell is a pointer to function and function pointer.I guess they have a purpose, I hate their existence. It's solely because it is complicated13
-
Recoding the malloc is a mess. It seems to be a very good exercise and it is. But you know there are so much mystical issues that happens when you're working with memory.
I just figured out that I got 90% of my free function calls by the "ls" command that was in reality a bad pointer following my own verifications.
So, I don't know why because I just make a normal vérification so except if the "ls" was developer with the ass... -
Cleaning up code warnings in a 3rd party piece of software and found a function that was returning a pointer to a local variable, who wrote this piece of shit?!1
-
So I have an array of length 20 which stores a 64 bit decimal number by digits. The starting address is let's say array64. When I am trying to build up my number in ECX:EBX, I am using EDX as the iterator to access the individual elements of the array (I am loading them into AL)
.LOOP:
....
MOV AL, [array64+EDX]
INC EDX
....
JMP .LOOP
The problem is: somehow EDX cannot get higher than 10, so the program just stops and waits for input, when I try to work with a 12 digit number...
This module is outside the main function, and I thought about some Far Pointer problems, but all of my ideas just failed...
What is wrong here?1 -
Why the pointer of member function so special.
trying make a non-template class that like std::function but simpler:
1. wrap static function pointer.
2. wrap class member function pointer and the class object pointer.
3. WITHOUT ALLOCATE MEMORY.
I can't store member function pointer because the size is undefined?! -
How can a function cause a null pointer exception at runtime when it compiles perfectly and you haven't put it in use yet? Wtf.
-
Rubber ducking your ass in a way, I figure things out as I rant and have to explain my reasoning or lack thereof every other sentence.
So lettuce harvest some more: I did not finish the linker as I initially planned, because I found a dumber way to solve the problem. I'm storing programs as bytecode chunks broken up into segment trees, and this is how we get namespaces, as each segment and value is labeled -- you can very well think of it as a file structure.
Each file proper, that is, every path you pass to the compiler, has it's own segment tree that results from breaking down the code within. We call this a clan, because it's a family of data, structures and procedures. It's a bit stupid not to call it "class", but that would imply each file can have only one class, which is generally good style but still technically not the case, hence the deliberate use of another word.
Anyway, because every clan is already represented as a tree, we can easily have two or more coexist by just parenting them as-is to a common root, enabling the fetching of symbols from one clan to another. We then perform a cannonical walk of the unified tree, push instructions to an assembly queue, and flatten the segmented memory into a single pool onto which we write the assembler's output.
I didn't think this would work, but it does. So how?
The assembly queue uses a highly sophisticated crackhead abstraction of the CVYC clan, or said plainly, clairvoyant code of the "fucked if I thought this would be simple" family. Fundamentally, every element in the queue is -- recursively -- either a fixed value or a function pointer plus arguments. So every instruction takes the form (ins (arg[0],arg[N])) where the instruction and the arguments may themselves be either fixed or indirect fetches that must be solved but in the ~ F U T U R E ~
Thusly, the assembler must be made aware of the fact that it's wearing sunglasses indoors and high on cocaine, so that these pointers -- and the accompanying arguments -- can be solved. However, your hemorroids are great, and sitting may be painful for long, hard times to come, because to even try and do this kind of John Connor solving pinky promises that loop on themselves is slowly reducing my sanity.
But minor time travel paradoxes aside, this allows for all existing symbols to be fetched at the time of assembly no matter where exactly in memory they reside; even if the namespace is mutated, and so the symbol duplicated, we can still modify the original symbol at the time of duplication to re-route fetchers to it's new location. And so the madness begins.
Effectively, our code can see the future, and it is not pleased with your test results. But enough about you being a disappointment to an equally misconstructed institution -- we are vermin of science, now stand still while I smack you with this Bible.
But seriously now, what I'm trying to say is that linking is not required as a separate step as a result of all this unintelligible fuckery; all the information required to access a file is the segment tree itself, so linking is appending trees to a new root, and a tree written to disk is essentially a linkable object file.
Mission accomplished... ? Perhaps.
This very much closes the chapter on *virtual* programs, that is, anything running on the VM. We're still lacking translation to native code, and that's an entirely different topic. Luckily, the language is pretty fucking close to assembler, so the translation may actually not be all that complicated.
But that is a story for another day, kids.
And now, a word from our sponsor:
<ad> Whoa, hold on there, crystal ball. It's clear to any tzaddiq that only prophets can prophecise, but if you are but a lowly goblinoid emperor of rectal pleasure, the simple truths can become very hard to grasp. How can one manage non-intertwining affairs in their professional and private lives while ALSO compulsively juggling nuts?
Enter: Testament, the gapp that will take your gonad-swallowing virtue to the next level. Ever felt like sucking on a hairy ballsack during office hours? We got you covered. With our state of the art cognitive implants, tracking devices and macumbeiras, you will be able to RIP your way into ultimate scrotolingual pleasure in no time!
Utilizing a highly elaborated process that combines illegal substances with the most forbidden schools of blood magic, we are able to [EXTREMELY CENSORED HERETICAL CONTENT] inside of your MATER with pinpoint accuracy! You shall be reformed in a parallel plane of existence, void of all that was your very being, just to suck on nads!
Just insert the ritual blade into your own testicles and let the spectral dance begin. Try Testament TODAY and use my promo code FIRSTBORNSFIRSTNUT for 20% OFF in your purchase of eternal damnation. Big ups to Testament for sponsoring DEEZ rant.3 -
Built the most generic file importer.
So a customer had his SAP system giving us some 5 million barcodes in a csv which we needed to parse. But as there could be different file types and I thought the handling would always include the same steps I made them configurable through function pointers. - Did not want to make it as spooky as the rest of the code base where the function pointers were buried deep in some shared memory configs, which might even change at run time, but rather I statically used the member functions of my class. Just to poke fun on the ugly C++ syntax of member function pointers. I still shudder at the thought some poor soul now has to maintain that code.
(For the actual parsing I actually used a one liner in awk which was churning through the records in one minute which was faster than the SAP guys seemed to be accustomed to.) -
Thank you, .NET Framework, for keeping your GC from destroying my DynamicMethod instance after I've accessed its function pointer!
Unlike another runtime that caused me to waste my weekend hunting a memory corruption bug in a managed language because of a minimal behavioral difference...
/tableflip -
I need to tell you the story of my MOAB (Mother of all bugs).
I need to write some stuff in C (which i am fairly used to) and have a function that allocates memory for a Matrix on the heap. The matrix has a rows and columns property and an associated data array, so it looks like this
struct Matrix{
uint8_t rows;
uint8_t columns;
uint8_t data[];
}
I allocate rows*columns + 2 bytes of memory for it.
I also have a function to zero it out which does something like this
for(int i=0; i < rows*columns;i++){
data[i]=0;}
Let‘s come to the problem:
On my Mac the whole stuff works and passes all tests. We tried the code on a Linux machine and suddenly the code crashed in various places, sometimes a realloc got an invalid pointer, sometimes free got an invalid pointer and basically the code crashed at arbitrary points randomly.
I was confused af because did i really make THAT many errors?
I found out that all errors occured when testing my matrices so i looked more into it and observed it through the debugger.
Eventually i came to the function that zeroes out my matrix and it went unusually high and wondered if my matrix really was that big.
Then i saw it
The matrix wasn‘t initialised yet
It had arbitrary data that was previously in the heap.
It zeroed out a huge chunk of the heap space.
It literally wrote a zero to a shitload of addresses which invalidated many pointer.
You can imagine my facepalm2 -
I'm all spooked out. I just added complicated JS code in a massive block, doing something complicated, using syntax that I wasn't sure about.
Load the page, smugly expecting like 200 errors. None.
Alright...
Run everything... it works.
WTF.
It's all balanced out though, because then python started freaking out with the wackiest motherfucker of an error I've ever seen. (A pointer to a function magically turning into None) -
I've recently had an exam, a C++ exam that was about sorting, pointers, etc... The usual. The exam was about Huffman's
optimization algorithm along with some pointer problems. He asked for a function to find something in a stack, what I did was write down a class that has a constructor, deconstructor, SetSize(), Add(), Remove(), Lenght(), etc.. He didn't give me any points for that, why? Because I didn't write down everything like in his book... I had classmates that literally had phones open with his book, he just watched how they copied code and gave them 10/10 points. But nothing to the guy that wrote down 20 pages of code. YES!! On paper, an IT university that asks you to program on a fucking paper. Good thing that at the very least I passed.
TL;DR
Teacher has book, I refuse to remember code from it/copy from it, I get lower grades than people that literally copied word for word.
Life is really fair. -
Just realized a member function pointer can be a template parameter as non-type, gonna try to use it do no dynamic memory allocation trick with std::function.
-
I don't know what's happening. In passing a char pointer to a function. I'm having issue, so I'm printf'ing the address pointed to by the pointer. Right after I assign it, it contains the right address, but the printf on the next line has it containing a different address. Another printf shows another different value, but all the following printfs show that third value. They're all consecutive printfs with nothing happening in between in the program, and the char* starts it's life as an array. All compiler optimizations are disabled. I don't know what's happening, it's just randomly changing. 😭2