0

Let's say I have a class defined like this:

#pragma once

#include <stdint.h>
#include <stdlib.h>
#include <string.h>

class Buffer
{
public:

  Buffer(uint16_t bytes)
  {
    ptr = malloc(size = bytes);
  }

  ~Buffer()
  {
    free(ptr);
  }

  inline char* str() { return (char*)ptr; }

  inline int length() { return strlen(str()); }

  void* ptr;
  uint16_t size;

};

Obviously the simplest possible buffer.

When I create a variable of that type, the constructor is called and the memory is allocated for data. When the variable goes out of scope, the destructor is called and the memory is freed.

Then I made a function, that writes something to that buffer:

writeSomething(Buffer buffer) // note that the Buffer type is not marked as reference
{
   // writes something.
}

And I have some code that writes something else...

test()
{
  Buffer text(1024);
  writeSomething(text);
  doSomethingElse(text.ptr);
}

All hell breaks loose. When I tested the code with the debugger, I noticed that Buffer destructor is called from a line containing doSomethingElse(text.ptr), that makes the pointer invalid and causes unexpected behavior, like next malloc() allocates the address that was already allocated by the text instance.

Just having a hunch, I changed the writeSomething() function signature to:

writeSomething(Buffer& buffer); // note marking Buffer type as reference

That solved the problem, but I still don't understand why. What happens when the argument is not marked as reference? I thought it was obvious it's treated as reference, since it's not a primitive type. As I see in the debugger, something really weird is happening.

So what does the first signature mean? What is passed as the argument? If it's not the reference to the class instance, then what is it?

Why is the destructor called there?

Harry
  • 4,524
  • 4
  • 42
  • 81
  • Passing by reference (`&`) is a basic c++ issue that is covered in any good c++ book. – wohlstad Nov 08 '22 at 09:41
  • 4
    "Obviously the simplest possible buffer." is quite some understatement if you consider how much care a user must take to not write code that is horribly broken. YOu need to read about the rule of 3/5 https://en.cppreference.com/w/cpp/language/rule_of_three – 463035818_is_not_an_ai Nov 08 '22 at 09:42
  • 1
    does this answer your question? https://stackoverflow.com/q/4172722/4117728 – 463035818_is_not_an_ai Nov 08 '22 at 09:42
  • 2
    `std::vector` is a "simple buffer" – 463035818_is_not_an_ai Nov 08 '22 at 09:43
  • 1
    managing a resource is not as simple as you expceted. In general, things that look innocent might blow off your foot if you do not know what you are doing. And frankly, your `Buffer` is a nice foot-gun, it is not a "simple buffer". It has to be said clear: Your `Buffer` is terribly broken, because as you say it lets break hell loose. And thats no weird thing happening, but just a consequence of ignoring the rule of 3/5 which is the minimum basic you need to consider when managing a resource in a custom class – 463035818_is_not_an_ai Nov 08 '22 at 09:48
  • This question (and similar) either should not be closed as duplicate of "What is a rule of three?" question, since searching for that title assumes the person ALREADY KNOWS THE ANSWER! Either change the title of so called "original", or post the the answer as link. Expect countless of similar questions asked over and over again otherwise. Not my problem, KTHXBYE. – Harry Nov 08 '22 at 10:15
  • there are very different opinions around about what it means to close as duplicate. I dont assume that you need to know the answer already, I assume that if you read the Q&A about the rule of 3 then you have sufficient information such that you can answer your own question easily. Moreover, answering your question here would require to repeat most information which is already available there. Btw SHOUTING is impolite – 463035818_is_not_an_ai Nov 09 '22 at 08:28
  • @463035818_is_not_a_number I didn't mean to be impolite, please excuse me. The answers I got were very helpful. I think I just had to ask duplicate question to learn this thing and I wonder how to direct more askers to that answer with the rule of 3. Maybe it's not possible. Again thanks for answering and explaining the problem. – Harry Nov 10 '22 at 09:11

0 Answers0