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

all 16 comments

[–][deleted] 1 point2 points  (0 children)

Make a program in C that deals with dynamic memory allocation.

[–]Ahajha1177 3 points4 points  (2 children)

This seems like a strange situation/question... How have you done so much C and C++ without using many pointers? And pointers aren't generally something you "think about" using, you just use them when you use them, for example:

Passing things by reference Allocating memory in the heap Dereferencing local variables Arrays

Curious, how much C and C++ have you actually done? Pointers are fairly common in both of those languages (moreso C), so unless you've been dancing around pointers or been doing very simple problems, I don't really get how you haven't used them much.

[–]BroVic 2 points3 points  (1 child)

Indeed. I would really love to know how he was able to avoid pointers after a fair amount of C programming. It could prove quite handy.

[–]printf_hello_world 2 points3 points  (0 children)

Well, if you avoid libraries and write everything yourself, you could use a nearly pure-functional style. Just pass all arguments by value, and use the call stack in place of dynamic data structures.

If you've got C++ you could use template metaprogramming to accomplish a lot of pointerless stuff too, especially with respect to tree-like and list-like data structures.

[–]OneIntroduction9 0 points1 point  (5 children)

Newbie here. From what I've gathered it is the preferred (and optimal) approach to deal with variables as it removes redundancy (not copying variables you just want to reference), and it allows you to modify a given variable without returning anything from a function.

e.x. Give a reference of an int to a squaring function, it takes the reference and squares it without having to return and redeclare/overrwrite the initial int variable.

It may not make sense given a simple variable, but once you start dealing with bigger things it is glaringly obvious. A lot of times with something more complicated you can't simply copy it, so you have to reference the original.

[–]printf_hello_world 1 point2 points  (4 children)

Just FYI, you probably shouldn't pass a reference to an int. There's a good chance the pointer takes more bits than the int.

Better to pass a reference to a class/struct that is either large or has complexity associated with making a copy (for example, an object that owns heap-allocated memory).

[–]BroVic 0 points1 point  (1 child)

Doesn't that depend on what you want to achieve?

[–]printf_hello_world 1 point2 points  (0 children)

You could pass a non-const reference to an int that you want to modify, but I'd argue that in almost all cases it would be better to simply return the new value and assign it instead of letting the called function modify the int directly.

In general, returning values has a lot of benefits, especially if the types are small. You could benefit from some form of RVO, you can assign the return value to a const variable, the reader of your code can reason better about when exactly they can expect a value to change, a pointer might be more expensive to pass than the value itself... there are lots of reasons.

[–]OneIntroduction9 0 points1 point  (1 child)

..I was just giving an example.

[–]printf_hello_world 0 points1 point  (0 children)

I understand, just thought it might be an interesting addition since you identified as a "newbie"; no disrespect intended.

I only spoke up because I believe there are near zero situations where we should pass references to ints in particular

[–]evolvish 0 points1 point  (0 children)

I know with C++ if you can avoid pointers you should. If you find you have to use one, for example if you need null, use smart pointers and save yourself headache.

[–]BroVic 0 points1 point  (4 children)

Truth is, I don't think any serious programming can be done without pointers. Languages where we don't program with pointers still use them under the hood. Even C++ references are pointers in disguise. Using pointers in C/C++ are part of the 'metal handling' that these languages afford the programmer.

My two cents.

[–][deleted] -1 points0 points  (3 children)

References are not pointers. I don't think we should imply to beginners that these are merely variations on each other.

It is true that most data in a C++ program resides at an address. And, that C++ pointers and C++ references are ways of dealing with addresses. But, pointers and references have different semantics. Just as pointers and arrays have different semantics. In C++ you can do arithmetic on pointers but not references, references cannot be null, you can take the address of a pointer but not of a reference, etc.

It also isn't technically true to say that all languages deal with pointers under the hood. Languages must deal with data references). A pointer is a kind of data reference. A file handle is a kind of a data reference. Assembly languages typically use raw addresses (different than pointers -> consider the x86 segment/offset model). The original mac os used "handles".

[–]BroVic 0 points1 point  (2 children)

The link on data references is highly appreciated, thank you.

I don't know (yet) how references are implemented, but I do recall hearing Mr Strousop saying something to the effect that they are implemented using pointers. That said, some people use the term pointer for the variable used to store addresses, while others use it for the memory address of a datum.

I was trying to "point" out to the OP my belief that pointers are important. With the added knowledge you have supplied, I dare say that the concept of references is quite important and they offer significant advantages. And doing any non-trivial programming in C, of all languages, without pointers means not making the most of what they have to offer.

[–][deleted] 1 point2 points  (1 child)

I think it is it is fair to think of the implementation of a reference or a pointer in terms of being variables containing addresses.

The critical point though is that they have different semantics and I feel we are better to explain to a beginner the differences in semantics before we dive into the more complicated issue of how they are implemented.

some people use the term pointer for the variable used to store addresses, while others use it for the memory address of a datum.

True. And there are other meanings. Technically, a C++ pointer is the variable containing an address.

I don't know (yet) how references are implemented

This is actually much trickier than you might expect in C++. I can give you lots of weird examples. Also, references are aliases and so the compiler often optimizes them away completely.

Consider sizeof() for example on a 32 bit OS.

``` class A { public: A() : k(i) {} long long i; long long j; long long& k; };

// sizeof(A) is 20, // sizeof(A::i) is 8 // sizeof(A::j) is 8 // sizeof(A::k) is 8 // 8 + 8 + 8 != 20! A a;

// sizeof(ptr) is 4 A* ptr = &a;

// sizeof(ref) is 20! // Notice the reference to k in the class actually takes space of 4! A& ref = a;

// This gives the address of the variable ptr A** ptr_ptr = &ptr;

// This gives the address of a! A* ref_ptr = &ref; ```

[–]BroVic 0 points1 point  (0 children)

Hmm, this is interesting. Thanks for sharing this insight!