-2

The major problem I find at the moment is with "initializing pointers" .

In Pascal, all you had to do is declare NEW(POINTER). In C++, I am lost as - correct me if i am wrong - you have to declare a "dummy" variable in order to initiate a pointer.

Let me put in this way: I want to build a class that will be chained by pointers. In Pascal, all I had to do is add the class a pointer, I called it next, of the same kind that it is nested in.

How do I it in C++?

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
  • 1st rule of thumb: You don't need raw pointers in c++, specifically not for dynamically allocated memory. – πάντα ῥεῖ Feb 06 '21 at 01:15
  • You can declare a pointer without initializing it: `int * pointer;`. You can also initialize a pointer at declaration: `int a; int * pointer_to_a = &a; int pointer = nullptr;` – Thomas Matthews Feb 06 '21 at 01:30
  • 1
    Recommendation: [get some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). C++ is a complicated language and a guided tour through the fundamentals will greatly speed up your learning. – user4581301 Feb 06 '21 at 01:31
  • I also did Pascal (more than) 25 years ago. Pointers in C++ and Pascal are very similar. Main difference would be that C++ has pointer arithmetic as well. But any pointer concept you have in Pascal you also have in C++ albeit with different syntax. It strikes me that you might only have a partial knowledge of pointers in C++, this dummy variable idea is mistaken. – john Feb 06 '21 at 01:58
  • *"you have to declare a 'dummy' variable in order to initiate a pointer"* -- Could you give an example of what you mean by this? Your information is wrong, and as a result, I cannot guess at your intended meaning. (Knowing where you are coming from allows more useful answers.) – JaMiT Feb 06 '21 at 02:25

1 Answers1

6

Pointers work exactly the same way in C++ as they do in Pascal. The only difference is in how you allocate and free the things being pointed at.

For instance, you can declare a local variable and point to it, eg:

int i = 12345;
int *p = &i;
// use p as needed...

This would be equivalent to this in Pascal:

var
  i: Integer;
  p: ^Integer;
begin
  i := 12345;
  p := @i;
  // use p as needed...
end;

Or, you can use new and delete to work with variables allocated dynamically, eg:

someType *data = new someType;
// use data as needed ...
delete data;

Which is equivalent to this in Pascal:

type
  someType = record
    ...
  end;

var
  data: ^someType;
begin
  New(data);
  // use data as needed ...
  Dispose(data);
end;

However, modern C++ code should strive to avoid using new/delete directly as much as possible. Use smart pointers instead (std::unique_ptr and std::shared_ptr, via std::make_unique() and std::make_shared()), which free memory automatically when they go out of scope and get destroyed. This helps make code safer and more self-documenting as to the ownership semantics of pointed-at data. Use raw pointers only where needed to access data without transferring ownership of it, eg:

void doSomething(someType *data)
{
    // use data as needed ...
}

{
    auto data = std::make_unique<someType>();
    doSomething(data.get());
} // <-- data is freed automatically here!

The type of class you describe is more formally known as a node/element of a "linked list", which is commonly implemented using new/delete, eg:

#include <iostream>

struct node
{
    int data;
    node* next = nullptr;
};

node *head = nullptr;
node *tail = nullptr;

void addToList(int data)
{
    node **n = (tail) ? &(tail->next) : &head;
    *n = new node{data};
    tail = *n;
}

void clearList()
{
    node *n = head;
    while (n) {
        node *next = n->next;
        delete n;
        n = next;
    }
    head = tail = nullptr;
}

void printList()
{
    node *n = head;
    if (n)
    {
        std::cout << n->data;
        while ((n = n->next) != nullptr) {
            std::cout << ", " << n->data;
        }
    }
    std::cout << std::endl;
}

int main()
{
    addToList(12345);
    addToList(67890);
    printList();
    clearList();
    return 0;
}

A linked-list is not a good example for using smart pointers, though. Yes, the head pointer could be a smart pointer, at least. But you would still have to iterate the list destroying the individual nodes, before the unique_ptr destroys the head node. And you might be tempted to make the next pointers be smart pointers too, but doing that would be a bad idea, as recursive destructor calls can blow up the call stack when destroying large linked-lists. Iterative loops are more suitable for destroying linked-lists, and having next be a smart pointer is not a good fit for that use-case.

But, in any case, C++ has built-in standard classes for double-linked lists (std::list) and single-linked lists (std::forward_list), which handle the linking pointers for you, so you can focus more on your class data and less on how the class instances are linked together, eg:

#include <iostream>
#include <list>

std::list<int> l;

void printList()
{
    auto iter = l.begin();
    if (iter != l.end())
    {
        std::cout << *iter;
        while (++iter != l.end()) {
            std::cout << ", " << *iter;
        }
    }
    std::cout << std::endl;
}

int main()
{
    l.push_back(12345);
    l.push_back(67890);
    printList();
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thx alot Remy! I will study your answer shortly! – Chaim Chait Feb 06 '21 at 05:05
  • Thx but I am still struggling.....I can't figure the syntax of how to do it with classes. I want to build a linked list of classes of the simple kind with the fields string, int and "NEXT" which will be a pointer to the next linked object/class.... Remy? Anyone? Thx in advance. – Chaim Chait Feb 06 '21 at 17:32
  • @ChaimChait in C++, a `struct` and a `class` are basically the same thing, the only real difference between them is the *default* visibility of their members (public vs private, respectively). So the code I presented works for classes, too. If there is something specific you are struggling with, I suggest you update your question to explain exactly what you are having trouble with. – Remy Lebeau Feb 06 '21 at 17:36