Home > Blockchain >  Using SmartPointers with operator ' '
Using SmartPointers with operator ' '

Time:05-25

How can I change the code below to have unique_ptr instead of the traditional pointer?

// vector::data
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector (5);

  int* p = myvector.data();

  *p = 10;
    p;
  *p = 20;
  p[2] = 100;

  std::cout << "myvector contains:";
  for (unsigned i=0; i<myvector.size();   i)
    std::cout << ' ' << myvector[i];
  std::cout << '\n';

  return 0;
}

I tried the code below, but I am having E0349 no operator " " matches these operands

  unique_ptr<int> p {myvector.data()};

  *p = 10;
    p; // <<< ERROR HERE >>>
  

CodePudding user response:

Smart pointers are not meant to manage individual elements of bulk data. The intent purpose of smart pointers is to manage the allocation and lifetime of complex objects.

Container objects like std::vector have iterators for the express purpose to wrap access to their bulk data internal elements without exposing a naked pointer. Use those!

CodePudding user response:

myvector.data(); returns a pointer to the internal representation of the vector. You should not initialise a std::unique_ptr<> with it because the unique_ptr<> will delete the array when it is destructed but so will the vector (when destructed or reallocated [e.g. resized]). The result is undefined.

Secondly and separately there is no valid meaning to incrementing a unique_ptr<> because you should only delete a pointer that was allocated. Passing a pointer to 'within' an allocated object to delete rather than a pointer to it's start also has undefined consequences.

In both cases the behaviour is undefined and in practice usually catastrophic (results in corruption of the heap leading to who-knows-what).

CodePudding user response:

In your code std::vector is responsible for memory management, so feeding here any smart pointer is pointless.

Forcing std::unique_ptr to interact with std::vector will only break things, since both are implemented with assumption they are only objects responsible to manage memory owned by them.

The only whay to introduce smart pointer here, is this, but std::vector solution is better:

#include <iostream>
#include <memory>

int main ()
{
  auto unique = std::make_unique<int[]>(5);

  int* p = unique.get();

  *p = 10;
    p;
  *p = 20;
  p[2] = 100;

  std::cout << "unique_ptr contains:";
  for (unsigned i=0; i<5;   i)
    std::cout << ' ' << unique[i];
  std::cout << '\n';

  return 0;
}

Please remember that smart pointer help to mange memory. Basically smart pointer moves responsibility of releasing memory from developer to compiler and compiler is more reliable then human.

Now since smart pointer is responsible for managing memory it should always point to same location. and -- do not have seance for them. To iterate over c-like arrays you should use raw pointers.

CodePudding user response:

I am having E0349 no operator " " matches these operands

The problem(mentioned error) is that std::unique_ptr has not overloaded operator and hence this operator cannot be used with it which is what the compiler is trying to say in "no operator " " matches these operands".

Note

Also, note that memory in containers might get reallocated and then if you use(like by dereferencing etc) the old pointers that still point to the old memory, you will get undefined behavior.

  • Related