[Linux]

I have had a hard time understanding pointers beyond a simple array step through and modifying one address at a time. When I get into pointers to C-structures, C++-objects and classes my head starts to hurt. And that's just to name a few. I find the C syntax *++p for instance to be very confusing, I would expect the syntax to look like this -> ++*p. Why on earth would the pointer symbol * be pushed back and be seperated from it's pointer name with the increment operator in the middle? Anyway, here's what I'm wanting to know. I've recently read that C++ 11 makes dealing with pointers a lot easier, if that's the case does the current version of g++ support that aspect of the new C++ 11 standard? Or better yet, what version of g++ supports it if any?

I also recently ordered a book called 'Understanding and Using C Pointers' from amazon and now I'm wondering if I really need that book if C++ 11 and g++ have a better way. Thanks.

Why on earth would the pointer symbol * be pushed back and be seperated from it's pointer name with the increment operator in the middle?

The * is not part of the pointer name, it's part of the type when you declare the pointer, like int* p; which means that p is a pointer to an int. But when you later do *p, the * symbol is a dereference operator, which means that *p fetches the value stored at the address pointed to by p.

So, to understand the expression *++p, you have to read it inside out. The ++p increments the pointer p (that means, it moves its address one increment forward, the number of bytes that this increment represents depends on the size of the thing that the pointer points to, like if p is a int*, then the increment is of sizeof(int)) and then it returns the final value of p after the increment. In other words, the expression returns the address that is one increment after the original address that p pointed to. And finally, the * dereferences that address, as if you did *(++p) or p += 1; *p.

I've recently read that C++ 11 makes dealing with pointers a lot easier, if that's the case does the current version of g++ support that aspect of the new C++ 11 standard?

Well, you are always going to have to understand pointers. But C++11 provides library components that help you manage the memory that pointers typically point to (dynamically allocated memory). These components are called smart-pointers. The std::unique_ptr allows you to create a pointer to dynamically allocated memory, wrap it inside that unique-pointer and whenever the unique-pointer goes out of scope (e.g., when you leave a function, or when the object that contains it is destroyed), the memory will be automatically deallocated, which means you don't have to do it manually (as in "old style" C++ or as in C). The std::shared_ptr is another smart-pointer that works similarly to the unique-pointer except that it keeps a reference count on the number of shared-pointers that point to the same object, and only when all of them have been destroyed, it will deallocate the memory (and destroy) the object it points to (i.e., it allows you to have multiple parts of the program "share" a pointer to the same object).

And to answer your question, latest versions of GCC support smart-pointers, along with most C++11 features. There is a complete table here of what features of C++11 GCC supports. The shared-pointer has been supported for many years (because it doesn't require the core language features of C++11, meaning that it can work on C++98/03 (older C++ standards)), and it has been used for years (it's part of modern C++ tradition since the early 2000s). The unique-pointer is a later addition, because it needs a core language feature called "move-semantics" (or "rvalue-references") which has only been added since C++11.

So, according to that table, smart-pointers are supported in GCC since version 4.3, but I would recommend at least version 4.6 (from experience).

Also, to turn on C++11, you will need to use the -std=c++11 option, or -std=c++0x (for pre-2011 versions of GCC that implemented the earlier drafts of C++11, which were referred to as C++0x).

These days, most compilers from less than 2 years ago, roughly, support nearly everything of the C++11 standard and standard library. Even the Microsoft compiler is not too bad since late 2013.

I also recently ordered a book called 'Understanding and Using C Pointers' from amazon and now I'm wondering if I really need that book if C++ 11 and g++ have a better way.

It is never a bad thing to understand pointers, even when you program in high-level languages that attempt to hide them (like Java/C#). They are a fundamental part of computer programming, regardless of the language. The only danger with learning the old ways of manually tinkering with pointers (as in C) is that you might miss out of the great mechanism (such as RAII and smart-pointers) in C++ that take much of that burden away. Just keep in mind that in real-life C++ programming, dealing directly with "raw" pointers is not the norm, but the exception, which usually only shows up in some implementation details (which are local to one function or source file, and often hidden away). So, if your goal is to learn modern C++ practices, just make sure you understand "raw" pointers (and manual memory management), but don't dwell on it, move on to more modern and safe constructs.

And if you are going to pick a book to learn C++, make sure to pick a very recent one. C++ books are not, in general, "timeless" (except for a few advanced ones), they have an expiration date of about 5 years, because modern C++ practices have evolved a lot in the last decade or two. Some sure bets for books on modern C++ are: "The C++ Programming Language: 4th Ed." by Bjarne Stroustrup (2013), "C++ Primer: 5th Ed." by Lippman, Lajoie and Moo (2012), and "Effective Modern C++" by Scott Meyers (2014) (although that one is more for people already proficient with pre-C++11 versions of C++).

When you declare a pointer, you use the type *name convention to say, make a pointer of this type called name. After that, the compiler knows that name is a pointer, so you don't have to keep reminding it that name is a pointer by adding a splat (*). In the main body of the function, *name is a dereference, which means to get the value that name is pointing to.

The difference between *++p and ++*p is all about the order that the operators * (dereference) and ++ (pre-increment) are applied. In *++p, the pointer is incremented and then the value in the new location is returned. In ++*p, the value at the pointer location is collected and that gets incremented.

// Example code to demonstrate the
// difference between *++p and ++*p.

#include <iostream>

int main(void)
{
    int k[3] = {17, -3, 21};    // indexes are 0 to 2.
    // declare and initialize the pointer p
    // to look at the beginning of the array k.
    int *p = k; 

    // The next line will take the 17 from the 
    // beginning of the array, and increment 
    // it, changing the 17 to 18.
    std::cout << ++*p << std::endl;

    // The next line will change the pointer 
    // to look at the next element, then get
    // the value there, -3.
    std::cout << *++p << std::endl;

    return 0;
}

I hope this helps you understand the difference between these two operations.

Thanks guys. mike_2000_17 one more thing, I thought gcc was for C and g++ was for C++ ?

GCC: GNU Compiler Collection
So, GCC is an all-encompassing term for a collection of compilers, it is true that the gcc / gcc.exe program is mainly a C compiler (also does C++, but you have to make a few contortions to make it work) and the g++ / g++.exe program is for C++ code. Most people just say "GCC", just like people say "MSVC" (short for "MicroSoft Visual C++") instead of "cl.exe".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.