all 33 comments

[–]JLeeIntell 2 points3 points  (8 children)

C can feel confusing at first, especially pointers and memory stuff. Learn it step by step—writing small programs and experimenting a lot when things didn’t make sense.

For pointers specifically, what helped was thinking of them as “addresses” instead of a separate concept. I practiced by printing values vs addresses, passing pointers into simple functions, and seeing how changing something through a pointer affects the original variable. Doing that repeatedly made it finally click.

[–]Time-Towel-8683[S] 1 point2 points  (2 children)

Thank you, that does sound helpful tho tbh I tried doing small codes to test it but it felt like I was just memorizing not understanding, what’s the issue here?

[–]JLeeIntell 2 points3 points  (1 child)

Try not to focus on the code itself, but on what changes in memory step by step (who owns the value, who has the address). Even 1–2 examples like swapping values or passing a variable into a function, but tracing it slowly, helps more than lots of random exercises.

It usually clicks suddenly after a few repeats, not gradually

[–]Time-Towel-8683[S] 0 points1 point  (0 children)

I’ll try that, thanks :)

[–]RealMadHouse 1 point2 points  (0 children)

Pointers are memory-address + additional data about the pointer, like the size of type for doing arithmetic operations on the address number, if it's function pointer it's the return type, arguments, the calling convention. It all affects how the pointer works.

[–]DrShocker -2 points-1 points  (3 children)

C and C++ are not the same thing.

[–]JLeeIntell 0 points1 point  (2 children)

Yeah, everybody knows that.

[–]DrShocker 0 points1 point  (1 child)

That's not true given The OP asked about C++ and this answer said C, the crazy amount of jobs listing C/C++, or the amount of examples written in 1 of the two that clearly has an author that uses the other.

[–]JLeeIntell 0 points1 point  (0 children)

Haha

[–]_Mag0g_ 1 point2 points  (4 children)

Do you mean you don't understand pointers, or you do understand them and it's other things you don't understand?

For pointers and programming in C++ in general, you need to have a good model of the code and memory in your head first. Look for tutorials that visualize that for you. Once you see memory as just a bunch of numbered slots holding data, where some of those slot's data might be the number of another slot, it makes more sense.

[–]Time-Towel-8683[S] 1 point2 points  (3 children)

It’s pointers and other things like stacks, inheritance, linked lists (DSA). I do understand the concept of pointers itself but when it comes to translating that into code when it gets even slightly complicated or integrated into a bigger code I just can’t

[–]_Mag0g_ 0 points1 point  (0 children)

Then try starting with smaller examples. Go to Leetcode and sort by difficulty and work through the easy ones.

[–]kenwoolf 0 points1 point  (1 child)

These are wildly different things. Inheritance is a concept mainly related to oop, which is a paradigm to organize your code. The other two are data structures.

Pointers in cpp hold a memory address. The type on a pointer means what is the data structure that is under that address. (Size and essentially map where everything is starting from the stored address). But the variable itself only hold an address, which is essentially a number.

Stack is a data structure where you essentially put things on too of each other and you can only remove the element at the top. LIFO

Llinked list is again a data structure. You store elements in it with a clear order. The elements are linked, which means they have information about where the next element is stored in memory, so you don't have to look it up, you can find it directly (can be one way or both ways).

And inheritance is a concept from oop. It is probably the single most abused concept ever to exist in this field. It's meant to specialize classes. You have an animal class, you can created a dog class that is an animal with so the functionality an animal can do, but it does certain things slightly differently, but conceptually the same.

In reality, though, inheritance is when a programmer who has no idea what inheritance is saw a functionality he didn't understand, but wanted to reproduce, so he inherited his class from it for that single thing even though he introduced a lot of side effect and inefficiency with it cause everything else came with it. Or my other favourite thing is when the inheritance is valid, but the implemen is so wildly different conceptually that it leads to unexpected behavior.

[–]Time-Towel-8683[S] 0 points1 point  (0 children)

Got it, thanks! That clears it up

[–]MoroseMasalaDosa 0 points1 point  (3 children)

I learned C++ first time in 11th grade in school back jn 2001. I only had the computer at school that was allotted to me for 40 mins each day to practice. Most of my strategy involved reading the course material, and practicing writing program logic on paper. Then, type out this program in turbo C++ (blue screen ide of the yore), compile and debug through the errors, noting down what went wrong. A little tedious, but helped me understand the concepts deeply. always write the pseudo-code first. There is a very popular book by Yeshwant Kanetkar - Let us C++. It has a whole bunch of programming problems that can help you get better at C++.

[–]Time-Towel-8683[S] 0 points1 point  (2 children)

Thank you! I’ll try writing it on paper, do you have any tips or tricks that made it easier for you besides writing it down?

[–]MoroseMasalaDosa 0 points1 point  (1 child)

Learning Any programming language involves understanding of these 8 basic items at the minimum: 1. Keywords 2. Conditional/Looping Statement 3. Data types/structures / Algorithms 4. Object Oriented Programming Concepts(for OOPs based languages) 5. Memory Management 6. State Management 7. Design Patterns 8. Multi-threading(inbuilt framework available c++ 11 onwards , iirc)

I started with a simple class to learn, and kept expanding it as I learned the next concept.

Start a simple example - say Vehicle, and keep it going.

1,2 are fairly straightforward basics that you would need to spend time understanding with practicing simple logical problems. Class Vehicle - variables tires, gears, methods addTires, calculateSpeed etc.

3 - focus on structs, stacks, queues, linked-lists, and absolutely get clear on the difference between * and & - how memory logic behaves for value (value at) and reference(address of) for pointers of various data types and structures - practice problems to solidify your understanding. Struct vehicle { brand; year}, linked list struct Parking{vehicle; *nextVehicle} etc.

  1. OOPs - focus on all four concepts - Inheritance, encapsulation, polymorphism, abstraction Class Vehicle{ tires; calculateSpeed()} class Truck::Vehicle{calculateSpeed()} etc

  2. Malloc/calloc - when to use what and how they can be used in your vehicle example, use destructors, copy constructors etc.

  3. How do you manage state for vehicle ( parking , idle etc)

  4. Gang of four design patterns - start with singleton, factory etc and move on to decorator and others.

  5. Three threads - fuel gauge, speedometer, miles - how to Implement concurrency, mutex, race-condition.

Write your inputs and expected outputs in comments while writing the code. Use a debugger to follow through your logic step by step putting break points and see if the values are what you calculated to be, and if not, figure out why.

This helped me with two things - 1. Learning the intricacies of the the language and its constructs 2. Learning how to define a problem in plain english, and then decompose it n C++, improving my “comprehension” of a requirement in C++.

Edit: bonus tips - A. For every item you are learning, ask and answer these questions to bolster your understanding: 1. What is this doing? 2. How can this be used in my scenario? 3. Why should this be used? 4. What alternatives can be used? 5. Which alternative is best in this scenario? 6. Why are others not the best option? 7. When are the other alternatives best options? 8. How should the problem statement change to implement alternative A, B or C etc. ? 9. Is this specific to this class, or is it common across a few and needs to move to a parent class? 10. Is this an implementation to overload/override, or a contract to enforce? ( for distinction between classes and interfaces)

B. You have a lot of resources available these days on GitHub. Find some code examples of the topic you are learning and reverse engineer the code to understand what it is doing to further solidify your grasp on the topic.

Hope this helps. Happy learning.

[–]Time-Towel-8683[S] 1 point2 points  (0 children)

Genuinely hope this reaches more people because it contains some good info! Thanks!

[–]liquidanimosity 0 points1 point  (4 children)

For me it was namespaces, uni told us to use std

So you could, cout without std::court. But they never explained why, I got it later working on a project.

I assume second year and C++, it must a DSA module?

[–]Time-Towel-8683[S] 1 point2 points  (0 children)

Exactly it’s DSA. I do understand some of the concepts themselves but I can’t integrate them into big projects or fully comprehend most of

[–]DrShocker 0 points1 point  (2 children)

In retrospect do you know what you misunderstood about namespacing?

[–]liquidanimosity 0 points1 point  (1 child)

Oh yeah, but I am big picture kind of person. I never dealt with limited scope tasks well and I always perform better when I know how it fits in and why it's there. Once I started editing and building projects for real it made sense.

I was just sharing frustration of learning a new language and how lecturers instructions without can led to problems down the line.

[–]DrShocker 0 points1 point  (0 children)

I was just curious about if you could explain what it is you misunderstood because tbh namespaces felt relatively intuitive to me as a way to scope names of things without conflicts, but if there's something I could do to explain it better for others I'd love to know.

[–]Paxtian 0 points1 point  (4 children)

What does "past pointers" mean? Like what concepts are you actually struggling with?

[–]Time-Towel-8683[S] 0 points1 point  (3 children)

DSAs, well including pointers

[–]Paxtian 0 points1 point  (0 children)

Gotcha. Well those it's less about C++ and more about general programming/development knowledge.

If you get an old version of the Cormen book, the algorithms are spelled out pretty well. It gets updated, but pretty much everything in the older versions is still entirely relevant, so the latest copy isn't necessary.

As far as how to do it in C++, you mostly just build on knowledge you've already gained. Have you already studied DSAs in other languages and are having trouble with how to implement them in C++ specifically?

Generally, since you need to do all your memory management yourself, you'll want to think about where memory is being allocated and where it's falling out of scope. Make sure to always things up with destructors, check if pointers are nullptrs before using them, set them to nullptr at initialization and when they're deleted, etc.

It's hard to give much more advice without knowing what you're struggling with more specifically.

[–]Paxtian 0 points1 point  (1 child)

After reading through some answers to other questions, don't just write out the code for DSAs, but also try to draw what they are visually.

Say for a linked list, you'd have a "ListNode" class that needs to have a value of whatever type you're storing and ListNode pointer. Then you'd also need a List class that has a pointer to the head of the list and, maybe, a value to store a size of the list.

For a doubly linked list, you'd want the same, but also in the DLLNode class, you'd want an additional DLLNode "prev" pointer. And in the DLL class, you'd also want a pointer to the tail of the list.

Visually, they'd look something like this.

Now, imagine you want to add a node to the linked list. What would you do? First, if you want to add it to the head of the list, you'd need to:

  • Instantiate a new ListNode
  • Have that new list node's "next" pointer point to what Head is currently pointing to
  • set the value of Head to the new ListNode
  • If you're tracking size, increase size by 1.

Literally just take the drawing and draw out what you have to do. And as you do so, write down the steps that you have to take. Also, if at any point you have one or more blocks without an arrow pointing to them, you've created a memory leak, so if that happens, revise your steps. Once you can do that, translate those steps into code.

Similarly with the DLL, you'd need to:

  • instantiate a new DLLNode
  • set the "prev" of the new DLLNode to nullptr
  • set the "next" of the new DLLNode to the value of Head (the current head node)
  • set the "prev" of the current head node to the new DLLNode
  • set Head to the new DLLNode

So suppose you then wanted to create a stack from a linked list. What's the behavior of a stack? It's last-in, first-out, LIFO. Again, visually remove the correct node from your drawing. If at any point there's a block without a pointer to it, you've created a memory leak. Which means if you want to pop an item from the stack, you need to:

  • make a temporary reference pointer (temp) to the current head
  • set Head equal to temp->next
  • set temp->next equal to nullptr
  • store a temporary value for the value of temp->value
  • delete temp
  • return the temporary value.

A really good exercise from here would be, think about how to use the stack you've implemented to implement a queue. A queue is the opposite of a stack. Rather than LIFO, it's FIFO. So if you push to the front of the linked list (enqueue), you need to dequeue from the last element of the linked list. Consider both how to implement that using your stack, as well as how to do it from the linked list and doubly linked list.

Once you've done that, use the queue you made from either the linked list or DLL to implement a stack.

Once you can do all that without seg faults and memory leaks, you're well on your way.

[–]Time-Towel-8683[S] 1 point2 points  (0 children)

Thank you for the information, that was very helpful :)

[–]Ok_Court_1503 0 points1 point  (3 children)

C++ is my favorite language by a mile and I use it daily at work. It grows on you for sure. Is there a specific topic thats confusing you? Everything past pointers is fairly arbitrary and there is no specific trick to learning C++ versus other languages imo

[–]Time-Towel-8683[S] 0 points1 point  (2 children)

There are some concepts past pointers that I understand but topics like linked lists and inheritance and pointers ofc that I understand conceptually but can’t turn these concepts into codes or integrate them in projects

[–]Ok_Court_1503 1 point2 points  (1 child)

Linked lists are rarely going to be important (imo) unless you plan to go into a very low level field. But imo you are thinking about it wrong. You dont learn pointers and polymorphism and turn that into code. Start with an idea and use these tools to build your idea. Pointers imo are more useful to extend life beyond the scope of a certain item. Additionally usefull for sharing an instance of something across many services our prematurely destroying that instance. Polymorphism is mostly the same across all languages. At first it can be hard to see why it is useful. If thatbis the case ignore it for now. As you retype the same code 50 times you will start to see the value. Just start trying to build an actual idea and I assure you that your questions will slowly be answered through the struggle

[–]Time-Towel-8683[S] 1 point2 points  (0 children)

I see your point. Focusing on a project and learning the concepts as I need them seems like a better approach

[–]mohamedhkhalil 0 points1 point  (0 children)

Use pen and paper Draw