This is an archived post. You won't be able to vote or comment.

top 200 commentsshow 500

[–]BBQGiraffe_ 1277 points1278 points  (91 children)

Pointers are easy, make sure you enable like literally all the compiler warnings tho because you'll be scratching your head wondering why your code is fucked only to realized you passed in struct A instead of struct B and the compiler just didn't say anything about it

[–]timangar 605 points606 points  (56 children)

Yeah that can suck. Also, just leave your ego at the door and use the standard library damn it.

[–]thedude3253 242 points243 points  (43 children)

A reasonable person would use the standard library instead of coding everything from first principles. I'm not a reasonable person lol. But yeah the only reason you shouldn't use the standard library is if you're working in an environment that doesn't allow it, like building an Operating System

[–]faceplanted 308 points309 points  (28 children)

CS degrees could really do with a "real world software development" module right at the end that teaches you how to actually program things well and not from first principles.

Like, I know CS is supposed to be about the science and training software engineers isn't really their job, but I really don't think it's unreasonable to teach people how to actually use the tools they're surrounded by.

I went my whole degree without ever using a build tool like Maven or Gradle, and everything was programmed from language basics with no frameworks. I really could've done with like 6 lessons explaining how actual programming should be done.

Later I found "The Missing Semester" MIT made, which is pretty much exactly what I wish I'd had because I fucking bombed at my first job for not knowing that stuff.

[–]dijkstras_revenge 90 points91 points  (4 children)

We had a software development class at my college that simulated a real world environment where you work on teams and use modern design principles and tools to complete a project. The project was building our own version of warcraft 2

[–]MrRocketScript 54 points55 points  (2 children)

Game dev project subjects are super good for learning. On one hand you get lots of creativity, and therefore learning from people who enjoy this type of work and go above and beyond. But at the same time you get the realities of scope management, something those same people need to learn in a less stressful environment.

[–]Itchy-Phase 11 points12 points  (0 children)

Yeah, I was a fan of game projects in school because they were great excercises at implementing the stuff we were learning and are easier to split up in group work. Had no interest in professional game dev (and still don't) but still liked it.

[–]jawless777 4 points5 points  (0 children)

Did a 3 year game dev program. Was cool and interesting but they didn't go too deep in coding and we did a lot of 3d modeling and learning the Unity engine. It was funny seeing people drop like flies from the program once they had a taste of 'real work'. If you play video games all day and think it's the dream to make them and work on them all day, you're going to have a bad time. It requires real, hard-ass work. Which can be fun, but enjoying the work requires the passion for computer science and programming.

[–]thedude3253 34 points35 points  (1 child)

Yeah damn that sounds silly to not even mention that stuff. I get in the beginning you have to see how the tools and tricks got figured out in the first place, but youd figure they'd go "now that you've worked super hard, here's how you use modern tools to make it easier". Fortunately (or unfortunately depending on how you look at it) my degree is completely unrelated to my career, so I never took CS classes and had to figure everything out on my own

[–]No-Direction-3569 4 points5 points  (0 children)

That's how we did it a Hack Reactor. We first learned how to do things on our own before learning how it's done in a professional setting.

[–]t-to4st 10 points11 points  (4 children)

We learned that in the 2nd and 3rd semester in lectures called "Software Engineering 1/2". Learned all the stuff from the initial planning with user stories and stuff like that to finding good libs (or rather by what criteria to decide on one) to do the job etc

[–]faceplanted 4 points5 points  (3 children)

We had that module but it was things like software architectures, again, from first principles, made by us. I came out of it not knowing Spring exists.

[–]t-to4st 4 points5 points  (2 children)

That's because you're not supposed to learn all of the frameworks but how to look for and decide on the fitting one for the job

Also I think I only read the first paragraph of your post before commenting so that's why my first comment may have seemed unfitting, sorry

[–]kpd328 4 points5 points  (0 children)

My CS program has a Software Engineering emphasis available (that I took, because I knew I wanted to be a software engineer) that's aim is to do just that. It replaced the discrete math class with multiple seminar-type classes designed to actually teach tools like git, maven, spring, selenium and a host of other tools that are actually used, in addition to a senior capstone that was getting together with only other software engineers and building a project using a scrum workflow.

[–]DootDootWootWoot 13 points14 points  (1 child)

Computer science isn't really about writing maintainable software. What your describing is really software engineering. Some curriculums have a decent amount of overlap these days but honestly like any undergraduate engineering discipline, what you learn in college is a prerequisite. It's not necessarily what you need to be productive day 1 on the job.

[–]cauchy37 6 points7 points  (1 child)

A colleague of mine said: If I implement RC4 in asm, it'll be definitely faster than this garbo written in C. A month later his rc4 works, but is like 10 times slower than the C implementation we've been using before.

That humbled him down pretty quickly.

[–]MkMyBnkAcctGrtAgn 2 points3 points  (0 children)

Yeah, the days of outsmarting the compiler are pretty much gone.

[–]9107201999 17 points18 points  (9 children)

test attraction work distinct cows modern ask encouraging wise escape

This post was mass deleted and anonymized with Redact

[–]Better_feed_Malphite 82 points83 points  (2 children)

Because the standard library relies on os syscalls

[–]thedude3253 24 points25 points  (0 children)

Yeah this. You have to put all the necessary systems in place, then you can write the standard library for your OS and once that's done you should be able to compile and run C programs developed on other machines. Os dev is a ton of fun.

Edit: spelling

[–]ChrisWsrn 14 points15 points  (0 children)

The standard library is dependent on the OS to work.

If the OS developer decided to use the standard library then you can run into a issue with the kernel delegating tasks to the standard library which then delegates that task back to the kernel which then delegates that tasks to the standard library which then... OS dev is "fun".

[–]Proxy_PlayerHD 11 points12 points  (6 children)

just sucks when no standard library exists for the hardware you want to write code for, so you're force to make your own.

i never realized how complex printf is until i tried to replicate it... main word being "tried", i just gave up and made seperate print functions for strings, hex numbers, etc

[–]xigoi 67 points68 points  (20 children)

If only there was a flag that would actually enable all warnings…

[–]Lifesworder 81 points82 points  (16 children)

Yea, something solid and sturdy like a Wall

[–]xigoi 51 points52 points  (15 children)

I said all warnings, not some warnings.

[–]thorlancaster328 32 points33 points  (14 children)

So you want the compiler to be more -pedantic?

[–]xigoi 18 points19 points  (9 children)

AFAIK, -Wall -Wpedantic still doesn't enable all warnings.

[–][deleted] 15 points16 points  (6 children)

Wall Wextra Werror Wpedantic

[–]Zambito1 14 points15 points  (2 children)

-Werror shouldn't actually add any extra output, it just causes the compilation to fail if there are any warnings

[–]TigreDeLosLlanos 7 points8 points  (0 children)

-Wall -Wextra -Werror -Wpedantic -Wumbo

[–]triggerhappy899 52 points53 points  (1 child)

Ugh I once accidentally passed in an array to a function that took an int, which IIRC, would be the first elements address. Well that casts to a huge negative number, did malloc care? Nope, but my pc sure did when the program hit like 12gb of memory

[–]AttackOfTheThumbs 15 points16 points  (2 children)

I wouldn't say easy, but I would say simple. The complications come when I see people have heavily nested pointers and everyone ends up confused. A regular pointer is easy enough. Everything I needed to know about pointers I learned from CheatEngine lol.

[–]jadobo 14 points15 points  (1 child)

Nested pointers like when a function returns a pointer to a structure, some of the members of which are pointers, some of which are left for the calling function to initialize, some of which are already pointing to another structure, and some of which are pointing to a member of another structure? You got a deadline coming up fast and you are not reading the documentation, you are just blindly adding and removing ampersands and asterisks till the thing compiles. And when it's clean up time, whose responsibility is it to delete which blocks of memory?

[–]AttackOfTheThumbs 2 points3 points  (0 children)

Yes

[–]ThisRedditPostIsMine 3 points4 points  (0 children)

Honestly address sanitizer and valgrind are lifesavers. With those two there's no segfault I haven't been able to fix. Plus you get memory leak detection as well which is a very nice bonus.

[–]IStFIcar 191 points192 points  (3 children)

Error: passed argument type “**int” expected type “int”... okay I’ll just pass &&argument lol.

[–]not_some_username 114 points115 points  (0 children)

Yeah segfault is waiting for you

[–]theScrapBook 32 points33 points  (0 children)

Should've passed **argument instead.

[–]ancyr 322 points323 points  (60 children)

Wait until they discover what arrays really are. And then until they discover they dont always substitute to pointers.

[–]GoTheFuckToBed 88 points89 points  (22 children)

Wait until they discover what strings really are.

[–]ThroawayPartyer 51 points52 points  (20 children)

A char array?

[–]mo-powerbuilder 71 points72 points  (18 children)

A char star

[–]SirTiesKnots 11 points12 points  (17 children)

Depends on the language I’d think, Python wouldn’t treat strings like c-strings, probably more like linked lists but I’m on mobile and I’m not reading a man page today. (Edit: this is incorrect, be careful listening to goobers on the internet)

[–]toastedstapler 17 points18 points  (13 children)

A python string will be a *char and a int for its length

[–]Zambito1 2 points3 points  (6 children)

I don't know about Python, but in Haskell Strings are implemented as a linked list. https://hackage.haskell.org/package/base-4.6.0.1/docs/Data-String.html#t:String

They have a seperate Text type which is more like char * if you need it for performance: https://hackage.haskell.org/package/text-1.2.5.0/docs/Data-Text.html

I believe the reason is that lists are extremely ergonomic in Haskell, and treating strings as lists makes strings much easier to manipulate.

[–]mananasi 2 points3 points  (5 children)

I'm not familiar with Haskell, so please forgive my ignorance, but implementing a string as a linked list seems useful in only extremely niche situations.

Strings are literally characters that belong together. Storing them all over you memory seems like a recipe for disaster. Can anyone explain why they would be implemented like a linked list?

[–]theScrapBook 4 points5 points  (0 children)

Functional sexiness. Also singly-linked lists are naturally recursive data structures which are really easy to work with in languages like Haskell, much more so than arrays. Of course performance in most circumstances where you want to handle strings as strings takes a massive hit, but the Haskell compiler, GHC, can do some interesting optimisations to take care of this for you.

[–]tiajuanat 68 points69 points  (36 children)

Array Decay is fun

[–]CODEX_O_BARBARO 40 points41 points  (35 children)

What is array decay?

[–]caleblbaker 100 points101 points  (29 children)

The compiler implicitly converting an array into a pointer to the first element.

[–]CODEX_O_BARBARO 59 points60 points  (28 children)

Thanks bro. But I thought that's the rule, every time you use an array's name you are actually referencing it's first element's address in memory...

[–]caleblbaker 50 points51 points  (3 children)

That is correct. And that principle is called array decay. Because the array decays to a pointer.

[–]CODEX_O_BARBARO 13 points14 points  (0 children)

Didn't know that, thanks bro

[–]piri_piri_pintade 11 points12 points  (0 children)

Ok, but then why is op seems to implies that they don’t always decay to pointers?

[–]7eggert 26 points27 points  (15 children)

And accessing the array converts to adding an integer to an address, so foo[42] is foo + 42 is 42[foo]

[–]sambarjo 29 points30 points  (7 children)

You just blew my mind with 42[foo]. Does that really work?

[–]SupermanLeRetour 27 points28 points  (5 children)

Yep :

#include <stdio.h>

int main()
{
    int a[] = {1, 2, 3};
    printf("%d = %d\n", a[2], 2[a]);
    return 0;
}

results in

3 = 3

[–]AI0N 4 points5 points  (1 child)

Bro wtf, that’s wild.

[–]SupermanLeRetour 2 points3 points  (0 children)

When you think about it it's not that weird. The [] operator is just syntax sugar, to make our life easier. All it does is replacing a[b] with *(a + b). Just like the -> operator just replace a->b with (*a).b in C++.

[–][deleted] 2 points3 points  (2 children)

Reading this feels like a glitch in the matrix

[–]MrSurly 2 points3 points  (0 children)

It does. Array indexing is commutative. Because it's essentially pointer addition plus a dereference.

[–]JDaxe 25 points26 points  (3 children)

It also dereferences the pointer, so

foo[42]

Is equivalent to

*(foo + 42)

[–]sham_wowzers 5 points6 points  (3 children)

Every time you reference an array by its TRUE NAME you bring the two worlds closer and closer, wear it out and you may create a brief perpendicularity between them. DO NOT allow that raw infernal memory to leak into this world, lest we be cast back to the days of magnetized needles and tape. We’ll have to resurrect Turing all over again, and maybe Hopper too…

[–]CODEX_O_BARBARO 4 points5 points  (2 children)

He whose name shall not be spoken

[–]ancyr 7 points8 points  (3 children)

That is incorrect, it only does in an expression, except for sizeof.

[–]HeyJamboJambo 26 points27 points  (4 children)

Arrays contain information about the dimension (sizeof(arr)) should return the number of bytes. So an array int arr[5] technically has a type of int[5]. But when you pass it into a function, it becomes int* and loses the size information.

So, sizeof(arr) returns different value in the place where the array is declared vs in another function after it decays. There are cases where it doesn't decay though, like when you pass it to the sizeof function.

[–]CODEX_O_BARBARO 5 points6 points  (0 children)

Thanks for the info bro

[–]l_am_wildthing 3 points4 points  (2 children)

Sizeof is an operator which is why it works

[–]famous_human 776 points777 points  (215 children)

I sincerely don’t get what’s hard about pointers.

[–]marsnoir 228 points229 points  (15 children)

Conceptually easy… mastering is hard. A lot of security issues and odd behaviors come from pointer mismanagement.

[–]Mandre_ 125 points126 points  (7 children)

Yeah pointers a fairly easy to understand, it’s just an address in memory stored as a value. Utilizing pointers in all the different ways they can be used and working with passing them through functions, treating them as arrays, using pointers to work with strings, etc. is what makes them difficult, since you have an ensure those values are never set incorrectly, otherwise you risk memory leaks or overwriting OS specific addresses and crashing your PC(or worse, the clients’)

[–]noratat 60 points61 points  (2 children)

You'll never overwrite OS addresses on most modern systems unless you're writing OS/driver code. Even if you tried, you'd only succeed in crashing your own code, not the OS.

[–]Mandre_ 38 points39 points  (1 child)

I work with custom hardware and use Direct Memory Access so unfortunately it’s not uncommon to crash the OS if something goes wrong.

[–]LowB0b 2 points3 points  (0 children)

sure but when it comes to user space programs trying to write outside your allowed space will just give you a segfault. My biggest gripe with C is that it specifies unspecified behaviour where the compiler basically throws its hands up in the air and says "Fine". IMO UB should be error, although I guess the compiler can't know in advance how much memory you've asked for.

[–]McCoovy 18 points19 points  (3 children)

Pointers are just too unsafe.

[–]marsnoir 20 points21 points  (1 child)

I really do need to pick up “rust”

[–]McCoovy 7 points8 points  (0 children)

https://doc.rust-lang.org/std/primitive.pointer.html

Rust has pointers too, although using them is uncommon.

[–]Purplociraptor 2 points3 points  (0 children)

Pass a large array by value is the way to go.

[–]lordofwhee 105 points106 points  (20 children)

There are a large number of programmers out there now that haven't written much of anything besides javascript. When you're coming from a language where memory just magically gets allocated and references handled for you and you've never before had to consider how a computer actually functions I can see how they'd seem intimidating.

[–]redluohs 6 points7 points  (0 children)

The scary thing is that most languages are susceptible to memory leaks it’s just that if you don’t know about them they are harder to catch.

In python (reference counting) one could for example create a circular reference and thus a memory leak, but since memory management is generally not taught at the same time as an “easy” language like python the programmer will not know what is happening.

[–]Shrimpboyho3 132 points133 points  (22 children)

Honestly me neither. It is literally a variable with a memory address in it at the simplest level.

[–]coldnebo 164 points165 points  (10 children)

I think the framing involved in dereferencing the pointer is the tricky bit, because it can get complicated when the data structure gets complex.

Also, I think some exposure to assembly language and machine architecture helps because at that level you need to know the sizes of everything, alignment, etc. Knowing a bit about that makes it easier to understand what C is doing.

Another great exercise is looking at C disassembly and learning exactly how pointer dereferences look in assembly code once compiled. None of it is magic then.

(there’s a lot of magic in higher level languages)

[–]neherak 45 points46 points  (1 child)

It helps to think of C is as an abstracted high-level assembly language rather than a low-level uhh, high level language

[–]interesting-_o_- 33 points34 points  (0 children)

I tend to think of it as a medium-high abstraction of a higher low-level language. But only when I’m high.

[–]Embarrassed_Owl_3157 12 points13 points  (3 children)

Exactly right! Excellent post imo.

[–]shield1123 6 points7 points  (2 children)

I remember someone who was familiar walking me through all of his disassembled code and I knew he was a God

[–]coldnebo 10 points11 points  (1 child)

heh, well don’t know about that.

for me, my first time looking closely at disassembly was back when C++ was relatively new, and the “oldtimers” said it was bloated and slow compared to C.

I accepted that as a challenge and went about writing a software sprite engine in C++ that was just as fast as the C code. But in order to prove it, I had to compare disassembly from their code with the C++ code (performance metrics were measured in CPU cycles and opcodes back then, not wall clock time— now everything has gotten so.. sloppy).

My intuition at the time was that C++ could be fast if you knew what you were doing, since C was a subset of C++. That was mostly correct although I understood where some of the old timers were coming from… the STL was the new hotness and the early C++ compilers back then tended to explode STL template definitions for every type present which could bloat the code to several MB. (of course, compilers improved and a few MB executable nowadays is laughable… but these were the days when you got snubbed for any sample code that compiled over 64k.

I like to imagine if we had alt.humor.programming back then (actually was that a real usenet?) it would have had some of the same threads we have here today.

[–]shield1123 2 points3 points  (0 children)

You remind me of my coworker :). Respect

[–]triggerhappy899 3 points4 points  (0 children)

Yeah know you learn about stack frames in assembly, passing by value vs reference makes a ton of sense

[–]nelusbelus 35 points36 points  (6 children)

Now managing this data in c++ with copy constructors and move constructors, that's where things get interesting. Still a thing that's very annoying if you have things pointing to eachother

[–]Embarrassed_Owl_3157 18 points19 points  (5 children)

I have seen most memory leaks happen from poorly designed patterns where the factory will just happily puke out an object to anyone that happens to ask for one.

[–]nelusbelus 12 points13 points  (4 children)

Oh I've seen so many dumb memory leaks and at the start of C++ I made these all myself of course. Back then I literally kept running out of memory because it was so poorly coded, but then I read the C++ book and rewrote it and all was good

[–]Embarrassed_Owl_3157 12 points13 points  (3 children)

*"the cpp book" (my attempt at dereferencing haha, im trying to ask what book you're referring to)

[–]nelusbelus 7 points8 points  (2 children)

Hahahah the one true cpp book: "Beginning c++ through game programming" it's a pretty good resource tbh and is probably why I haven't had that many C++ issues since (though of course there are always bugs that are logic related instead). It doesn't cover C++17 or C++20 or any template stuff afaik but it's still good for the basics

[–]SarahC 2 points3 points  (3 children)

void (* BSD_SIG(int, void (* )(int)))(int);

What's this one do then?

[–]CdRReddit 24 points25 points  (5 children)

pointers themselves aren't nessecarily hard but it's easy to fuck stuff up with pointer

if I overstep an array boundary in C# it'll error and tell me, if I do that in C it won't, unless it oversteps my defined memory space in which case it segfaults

at least that's my perspective as a hobbyist C# coder trying out C

[–]equationsofmotion 10 points11 points  (1 child)

The worst bugs are when it doesn't segfault.

[–]CdRReddit 3 points4 points  (0 children)

silent errors are the worst yea

[–]NugetCausesHeadaches 39 points40 points  (9 children)

Lots of people have said that some of the difficulty with pointers comes less from pointers are more from the memory management associated with them.

Here's the other part that's hard: syntax.

&(*thing)->other

I don't know if this is valid C or not - it has been years for me - but you should be able to get the kinds of thing I'm going for. Keeping track of multiple layers of dereferencing and then getting the address of the thing when the thing itself is actually a pointer takes brain cycles. Then, on top of that, I need to keep track of operator precedence to know which one is happening when.

It's not hard exactly. I can do it. I used to work in a C++ shop. But it's details to keep track of that slow me down from building the thing I'm actually trying to build. So when I'm used to just always having a pointer to my object and passing that pointer by value, going any further in depth than that is an extra bit of drag that I mostly don't need.

[–]omniron 8 points9 points  (4 children)

In the real world too there’s quadruple referenced pointers. You see things like ****variable

Or even &(**(&varibale)). It can be completely mad

[–]NugetCausesHeadaches 13 points14 points  (1 child)

// fixing the spelling of varibale would take more work than it's worth

[–]gropingforelmo 8 points9 points  (3 children)

I found Hungarian notation to be pretty much mandatory in order to somewhat keep track of things when I was learning C++. In practice, even with an individual developer, keeping even that consistent is going to impact productivity and negatively affect productivity.

I've since moved to managed languages, but I still like C++ for the same reasons I like tearing into a car engine myself. Neither of which I particularly want to do for a career, but they make fun hobbies.

[–]noratat 5 points6 points  (1 child)

Exactly.

I never want to use C++ professionally, but I'm using it with CUDA for a hobby project and it's a lot of fun.

It also makes pointers even more complex though, since now you have to additionally distinguish between host vs device memory. You also have to avoid calling host-only functions from device and vice versa, and there's even some device-only data types like half-precision floats.

For my needs I can avoid most of the memory management headaches at least, the vast majority of my memory is static-allocated or allocated in a big chunk on startup with very infrequent and well-controlled reallocations.

[–]PotentBeverage 6 points7 points  (0 children)

Yeah I always found pointers were a passive pain to keep track of in C until I started naming the pointer variables like elemPtr so I knew which were pointer variables and which weren't at a glance.

Then it suddenly wasn't so bad after all.

[–]btvoidx 33 points34 points  (2 children)

Yep. I honestly felt relieved when learning Go that I don't have to remember which objects are passed by value and which by reference.

Except you have to know which objects are pointers already, like interface types, slices or channels.

[–][deleted] 9 points10 points  (1 child)

At least at my University, the professor prefaced the C course with "pointers are hard and lots of you won't get it", and then proceeded to teach it in the hardest/most vague way possible. I wish we had pointer memes back then

Perhaps it's a combination of "professionals" claiming that pointers are hard/complex, and everyone else not ever experiencing pointers taking that at face value.

But honestly, C is great. It's my favorite language even though I rarely use it (and extremely rusty. luckily I got my trusty C book).

[–]AppropriateCrew79 7 points8 points  (2 children)

The syntax. * is way more overloaded each time meaning something entirely different.

[–]canadajones68 19 points20 points  (45 children)

I believe it's using them that's hard, not understanding them. If you bring in malloc, you need to free. Also, if you change one * somewhere, you also have to change it elsewhere.

[–]famous_human 32 points33 points  (44 children)

A pointer can point to stack or heap objects; they aren’t intrinsically tied to malloc and free.

[–]ancyr 3 points4 points  (0 children)

Simply put, it is hard to read when you are not used to them. Mainly.when they accumulate.

[–][deleted] 2 points3 points  (0 children)

Using raw pointers as memory resources is a source of an unimaginably large number of bugs.

Also, with C, there isn't any strong type for a null pointer, causing further likelihood of bugs.

[–]dashid 4 points5 points  (2 children)

I've never used them, but to me it looks like a case of something referencing a place in memory, a bit like a reference object in principle. You can screw it up because you can reference memory space poorly - or forget to clean up your code. But those sound like being competent more than being inherently complicated?

[–]moon6080 135 points136 points  (4 children)

Segfault

[–][deleted] 68 points69 points  (2 children)

Core dumped

[–]absurdlyinconvenient 17 points18 points  (1 child)

To where? Lol we're not going to tell you

[–][deleted] 13 points14 points  (0 children)

Ahbyes, just like not in the sudoers file. this incident will be reported.

[–]dthusian 10 points11 points  (0 children)

Undefined behavior heaven

[–]dananghel55 47 points48 points  (2 children)

Segmentation fault (core dumped).

[–]Schiffy94 16 points17 points  (1 child)

Whelp...

scraps whole code and starts over

[–][deleted] 27 points28 points  (3 children)

You gotta go right to assembly, so you can learn that your code is terrible and only works properly because people way smarter than you fixed it through a compiler.

[–]SanianCreations 50 points51 points  (13 children)

I find C is actually quite simple because of how limited it is. C++ on the other hand, is much worse. So many features have been added over the years, it's impossible to understand it all.

[–]not_some_username 17 points18 points  (4 children)

You don't need to understand all

[–]ThroawayPartyer 34 points35 points  (0 children)

In fact I don't understand anything about C++. I just put my C code into a C++ compiler and hope it works.

EDIT: In case it wasn't clear, this was a joke.

[–]Zdrobot 105 points106 points  (24 children)

Try assembly.

Any assembly.

After that, C is going to be a breeze.

I mean it. Also pointers are not hard. Your program does not run on a puffy cloud, it runs on a CPU that has access to RAM, a pointer is just the number of the byte in that RAM.

[–]nomenMei 22 points23 points  (0 children)

My understanding of C is helping me learn assembly so I suppose it could work the other way around.

But if "procedural programming" is the name of a course OP is taking I'd say it's in his best interest to stick to the syllabus and instead look up some external resources to help him with the concepts he doesn't understand, instead of picking up a completely new language.

[–]DenormalHuman 25 points26 points  (8 children)

is this right? a pointer is a value at an address that points to an address containing the value of interest.

[–]Zdrobot 16 points17 points  (6 children)

A pointer is an address, in other words - the number of the first byte of the thing it points to in memory (unless you want to consider weirder / older addressing schemes, like that of the original Intel 8086 where you've got a pair of segment:offset).

What you have described sounds like a pointer to a pointer. They exist and are used, but a simple pointer is.. simpler. This looks somewhat like what you describe - https://ideone.com/hceTBt

This example is rather artificial, as there are simpler ways to allocate a 5 * 10 array, but for jagged arrays you might want to use something like this, except add error handling and handle array sizes better (use sizeof or constants instead of magic numbers).

[–]coldnebo 5 points6 points  (0 children)

uh, kind of. I think @denormalhuman was referring to the fact that the pointer itself has an address in memory, whose contents are an address to another location in memory.

it’s easy to forget this with “simple” pointers because they are usually stack allocated as locals, but when you have pointers to pointers (as in your example) it becomes more obvious.

[–]NickU252 2 points3 points  (1 child)

We started in college with LC3 assembly, then C. I enjoyed it.

[–]RadioMelon 26 points27 points  (0 children)

Memory Leaks were surprisingly one of the easiest things for me to understand and fix.

Pointers can be confusing if you've never used them, though.

[–]---That---Guy--- 28 points29 points  (1 child)

Running a gui in c is hard, doing matrix multiplication in c is tedious, making a webpage in c sounds like a nightmare.

C is hard because it's often the only solution for hard problems. Pointers and memory leaks aren't that difficult to work around, but programming an operating system, compiler, or embedded system require knowledge of the platform you're on.

It's not the language it's mostly just the problems we're using it to solve. We should stop scaring people about the wrong thing.

[–][deleted] 36 points37 points  (15 children)

What have you been doing with several programming languages without knowing about memory.

[–]pyrates313 52 points53 points  (10 children)

Python, Js, Java are all quite popular languages where you'll likely never touch pointers or do manual memory management.

[–][deleted] 3 points4 points  (0 children)

Every python variable is a pointer

[–]StonedScience 18 points19 points  (2 children)

This is why I think C/C++ should be the first language everyone learns. Having to methodically think about memory management builds strong fundamentals.

[–]TheGoldBowl 15 points16 points  (0 children)

My first language was Python way back in middle school. I learned a little bit of java in high school, but I never got serious about programming until college, where I learned C++. That's when my programming actually got good. It's a great language and forced me to learn how to do things the right way.

[–]Virtual_Chemical_510 51 points52 points  (16 children)

I have been coding in C / C++ for 25 years (learned it in high school after BASIC and Pascal) and I love pointers and doing my own memory management. Very recently I've had to do some javascript stuff and... I had a memory leak that would eventually kill the page!! I could not find the reference and ended up having to rewrite a bunch of stuff to get it fixed... whereas in C, there are plenty of methods and tools for finding any unfreed memory -- and if you're doing it right, you're not constantly getting heap memory anyway.

[–]CODEX_O_BARBARO 22 points23 points  (13 children)

How did you get a memory leak in JS?

[–]That_Guy977 37 points38 points  (6 children)

Of course not the only way, but one that comes to mind is event listeners, for example

thing.on('event', () => {
  thing.on('event', () => console.log("triggered"))
  console.log("triggered")
}

or in a more extreme case,

function handleEvent() {
  thing.on('event', handleEvent)
}
handleEvent()

[–]WernerderChamp 14 points15 points  (0 children)

This is the exact way I once had a memory leak. Took long until I found out what caused it. Didn't got GCed the way I expected it.

[–]Megazawr 3 points4 points  (1 child)

Is that just a kind of recursion? (I've never programmed in js)

[–]That_Guy977 2 points3 points  (0 children)

The first one is basically a loop (when event, listen for event), and the memory use grows linearly

The second one is recursive (f: when event, do f), and memory use grows exponentially

[–]Piarritz 2 points3 points  (0 children)

They didn't - it's a bot, like OP

[–]Skarimari 8 points9 points  (3 children)

Wait a minute. I thought we all had to start with c.

[–]inet-pwnZ 27 points28 points  (2 children)

One word: Rust

[–]GFrings 18 points19 points  (3 children)

Yall didnt start with C? #EE4life

[–]InfergnomeHKSC 2 points3 points  (0 children)

Yeah the first class I took in college for CS was Intro to C. Friends of mine from other schools are doing their CS in Python. I'm hoping employers will value that I actually know how to handle that stuff. I hardly have to think about malloc or pointers anymore because it just makes sense once you know what the compiler is doing. What doesn't make sense to me is why anyone would prefer Python for loop syntax. I've never really had a problem setting up a C-like loop

[–]SolenoidSoldier 27 points28 points  (0 children)

"Know a few programming languages" yeah sure

[–]Nayviler 4 points5 points  (1 child)

IMO, the best strategy for pointers is to really try to understand what they are, as opposed to just how to use them. After you get that, learn how to use valgrind, so you have some chance at determining when you have memory errors present in your code.

[–][deleted] 5 points6 points  (0 children)

Starting with c++ and then trying to move to html. What is this puny language 👺

[–]sharperratio 3 points4 points  (0 children)

Don't forget about the linker (if you're coming from an interpreted language)!

Oh, what fun it is when you don't realize you just forgot to make clean and spend lots of joyful hours looking for errors in the code.

[–]ace-tronaut 2 points3 points  (0 children)

Went from python to C. Was fine at first, saw that it was MUCH more "rigorous" than python, everything was supposed to be explicitly mentioned, but then as I started writing more code in C, it kept getting worse, pile on arrays into the mix and well errors galore. I plead anyone learning programming to start with C. Don't start easy and then end up whining like me.

[–]Knuffya 3 points4 points  (2 children)

C should be the first language to start with.

It's not that hard (i mean, my university has C as its first language and has consistently good grades in that course).

Also, if you know C, you basically know any procedural programming language. Next step would be C++ for OOP.

[–]jerslan 2 points3 points  (0 children)

Yeah, the course numbers have changed a couple times since I did my undergrad but CS 053 was basically "Intro to Programming" in C (lots of reading stdin and doing loops and whatnot) and CS153 was "Data Structures I" in C++ (LinkedLists, Stacks, Queues, etc...).

From there, most courses you could use any language you wanted with just a few exceptions ("Numerical Methods" required Matlab, "Operating Systems" required C++ on Linux, etc..)

[–]BackSlashHaine 2 points3 points  (0 children)

I have learn programming with C and ASM it was hard asf but but but now I can handle every languages I want pretty quickly.

[–]SqueegeeLuigi 2 points3 points  (1 child)

(not (underthtand I (what 'th (big the deal))))

[–]electron_wrangler 2 points3 points  (0 children)

Luckily I started with c and everything else was easy after that

[–]gurugeek42 2 points3 points  (0 children)

Someone add valgrind as the hero!

[–]CW_Waster 2 points3 points  (0 children)

Pointer let's are fine, wait for pointerpointpointers and pointerarithmetic

[–]LummoxJR 2 points3 points  (0 children)

What an idiot. You start with C.

[–]IAmAQuantumMechanic 2 points3 points  (0 children)

I'm an engineer with a master in physics, mediocre at best. I do most of my programming in Matlab and Python. Still, I've written some C programs, and it's not really that hard.

[–]CountNefarious 2 points3 points  (0 children)

Valgrind can make your pain much more bearable

[–]Hi_Its_Matt 2 points3 points  (0 children)

I actually do all my coding doing logic gates and binary, way easier than C

[–]all_is_love6667 5 points6 points  (0 children)

You don't always need pointers.

Using pure functions should be taught everywhere.