简体   繁体   中英

Is there a better way to initalize pointers in c++?

Below I have a bit of code for implementing Huffman compression. What I was curious about was if I could initialize the left and right pointers without including cstdlib , or rather, if I could initialize an empty memory location for storing the left and right without using malloc.

Also, in my combine function, I do not want to use "NULL" for the string parent node of my left and right, but would rather have an empty string. Will I have to make a new constructor for this? I get an error ( basic_string::_M_construct null not valid ) when I replace "NULL" with nullptr.

#include <string>
#include <cstdlib>

#ifndef PRIORITY_NODE
#define PRIORITY_NODE

namespace Huffman
{
  class PriorityNode
  {
    private:
      std::string key; // The character sequence to compress
      long frequency = 0;  // The frequency of the character sequence
      PriorityNode* left = (PriorityNode*)malloc(sizeof(PriorityNode));
      PriorityNode* right = (PriorityNode*)malloc(sizeof(PriorityNode));
    
    public:
      PriorityNode(std::string k, long f): frequency(f), key(k){};
      std::string getKey() const{ return key;}
      long getFrequency() const{ return frequency;}
      void setLeft(const PriorityNode& left){*this->left = left;}
      void setRight(const PriorityNode& right){*this->right = right;}
      PriorityNode& getLeft() const{ return *left;}
      PriorityNode& getRight() const{ return *right;}
      friend PriorityNode combine(const PriorityNode& lhs, const PriorityNode& rhs)
      {
        long ret_freq = lhs.getFrequency() + rhs.getFrequency();
        PriorityNode ret = PriorityNode("NULL", ret_freq);
        ret.setLeft(lhs);
        ret.setRight(rhs);
        return ret;
      };
  };
}

#endif

So a couple of points.

  1. key is a string, not a pointer. It makes no sense to set it to nullptr , i am guessing "NULL" , is just a stand in for when the key has no value. Instead of this, just use an empty string "" .

  2. You should try to avoid this manual memory management for several reasons. First of all you have no destructor, so your memory that you malloc is never free ed meaning you have a memory leak. Secondly, you allocate memory for the sub nodes, even if you do not need to. I would suggest something more like the following:

     class PriorityNode { private: ... std::shared_ptr<PriorityNode> left, right; // Default constructs to nullptr... friend PriorityNode combine(const std::shared_ptr<PriorityNode> lhs, const std::shared_ptr<PriorityNode> rhs) { PriorityNode ret = PriorityNode("", ret_freq); ret.setLeft(lhs); ret.setRight(rhs); return ret; };

The empty string is "" . You can pass that to the existing constructor. std::string doesn't distinguish empty strings and NULL pointers like C strings. It is not allowed to initialize an std::string with a NULL pointer. If you need a separate null state, use std::optional<std::string> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM