Home > Blockchain >  Copying objects when passing by value - how many copies do I end up with?
Copying objects when passing by value - how many copies do I end up with?

Time:01-03

Let me start off by saying that I'm very new to C currently - i have previously only really worked with python and javascript (quite a lot of exposure to python) and so, now that I'm learning C to expand my knowledge and understanding of lower level programming concepts I wanted to reach out and ask a specific question here.

Of course, with python I have not needed to worry at all about memory allocation, object copies, passing by value/reference, etc. its a whole new world! (very exciting :D)

The way i learn best is to learn by doing, so i have created a relatively simple blockchain proof of concept.

I wont paste in all my code here, as its a fair amount already, but what I want to check is specifically around how copies of objects are handled.

I am not using any dynamic memory allocation either via new/delete or shared_ptr/make_shared thus far in my code, and if i've understood correctly, all of my objects are being created on the stack - as such once the object goes out of scope, this will be automatically destructed (is this right?).

Let me provide an example here and explain what my specific question is. Consider the following code blocks:

void Blockchain::AddBlock(Block bBlock)
{
    if (bBlock.ValidateBlock(_vChain))
    {
        _vChain.push_back(bBlock);
        cout << "Chain Length: " << _vChain.size() << endl;
    }
    else
    {
        throw Exceptions::ValidationError();
    }
}

void Blockchain::AddTransaction(Transaction &tTransaction)
{
    cout << "Adding Transaction to block number " << _vChain.size()   1 << endl;
    bool IsValidated = _ValidateTransaction(tTransaction);
    if (!IsValidated)
    {
        cout << "... not valid" << endl;
    }
    else
    {
        // cout << "Valid";
        int ChainLength = _vTransaction.size();
        if (ChainLength < BlockchainConstants::MAX_BLOCK_SIZE)
        {
            _vTransaction.push_back(tTransaction);
        }
        else
        {
            uint32_t NewIndex = _vChain.size();
            Block NextBlock = Block(NewIndex, _vTransaction);
            NextBlock.sPrevHash = GetLastBlock().GetHash();
            AddBlock(NextBlock);
            _vTransaction.clear();
            _vTransaction.push_back(tTransaction);
        }
    }
}

Again, if i've understood correctly, in the above block of code that i've written, once the transaction pool has hit its max capacity (thus triggering the else condition), I am creating a block object based on the transaction pool and the index, this is the first instance. When i call AddBlock() I am passing the object by value, thus creating a copy of the object. Within AddBlock() method i am validating the block, and again pushing the object back into the vector _vChain (again, creating a copy). My question is, at the end of that whole process, am I left with any more than one object? My assumption is that the first object created gets destructed at the end of the else condition in AddTransaction() and the second copy is destructed at the end of AddBlock(), leaving me with just 1 instance of the object in the vector _vChain.

Is my thinking correct here? Apologies for the long question, I just wanted to make sure i had all of the details here for you guys - let me know if there is anything else you need to know to help me out. Would appreciate any help at all!

Cheers

CodePudding user response:

Your question is a bit unclear since I'm not sure why you'd like to know how many copies are there. Edit your question if you'd like to know more explicit question. I smell XY-problem here.

What you need to know is the variable scope.

I assume _vChain is a class member or global variable somehere so _vChain will have a copy of bBlock if validation is passed in AddBlock(). The input argument will be destructed after leaving the function.

If you want to see the behavior, the most simple way is to place std::cout in both of the constructor and destructor to track the construction and destruction of Block.

Also I suggest to pick a beginning programmer's book in this list.

  •  Tags:  
  • c
  • Related