Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5935907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:19:20+00:00 2026-05-22T15:19:20+00:00

I’m working on the class that simply contains a character array and it’s size

  • 0

I’m working on the class that simply contains a character array and it’s size (length in bytes). At the moment, I want to overload ‘+’ operand for this class (to implement concatenation). Constructors work fine. Objects are created and I can see their fields and values in the debugger. I’m stuck at the point where ‘+’ is used (main(line 13)). Code compiles well, without even warnings, but as I run it, my program fails with “invalid pointer message”. And I found where exactly that invalid pointer is. It is in ‘+’ implementation (BufferArray.cpp, line 39). When i call SetBuffer, char array is assigned properly (I saw it’s value ‘qwasd’ in the operator implementation scope), but right at the next line it’s vanished when I call SetSize. I have no idea why.

What is wrong with my setters and how can I implement ‘+’ operand in this case?

Thanks in advance.

Here’s the code I work with:

BufferArray.h:

#include <string.h>
#include <stdio.h>

#ifndef BUFFERARRAY_H
#define BUFFERARRAY_H
class BufferArray {
public:
    BufferArray(char* reservedPlace);
    BufferArray();
    void SetSize(int sz);
    int GetSize();
    void SetBuffer(char* buf);
    char* GetBuffer();
    BufferArray operator+ (BufferArray bArr) const;
    virtual ~BufferArray();
private:
    int size;
    char *buffer;
};

#endif  /* BUFFERARRAY_H */

Implementation is in the next file BufferArray.cpp:

#include "BufferArray.h"

// Constructors.
BufferArray::BufferArray(){
    size = 0;
    strcpy(buffer, "");
}
BufferArray::BufferArray(char* reservedPlace) {
    size = strlen(reservedPlace);
    buffer = reservedPlace;
}

// Getters and setters.
void BufferArray::SetSize(int sz)
{
    size = sz;
}
int BufferArray::GetSize()
{
    return size;
}
void BufferArray::SetBuffer(char* buf)
{
    buffer = buf;
}
char* BufferArray::GetBuffer()
{
    return buffer;
}

// Operator +.
BufferArray BufferArray::operator+ (BufferArray bArr) const
{
    char tempCharArray[strlen(buffer) + strlen(bArr.GetBuffer())];
    strcpy(tempCharArray, buffer);
    strcat(tempCharArray, bArr.GetBuffer());
    BufferArray tempBA;
    tempBA.SetBuffer(tempCharArray);
    tempBA.SetSize(strlen(bArr.GetBuffer()) + strlen(buffer)); // Vanishes buffer field.
    printf("%d",tempBA.GetSize());
    return tempBA;
}

// Destructor.
BufferArray::~BufferArray() {
    // Destroy the pointer.
    delete [] buffer;
}

And the main function:

#include <cstdlib>
#include <iostream>
#include "BufferArray.h"
using namespace std;

int main(int argc, char** argv) {
    BufferArray ba1;
    char tmp1[3] = "qw";
    char tmp2[4] = "asd";
    ba1.SetSize(strlen(tmp1));
    ba1.SetBuffer(tmp1);
    BufferArray ba2(tmp2);
    BufferArray ba3 = ba1 + ba2;           // Runtime error is here.
    cout << ba3.GetBuffer() << endl;
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:19:21+00:00Added an answer on May 22, 2026 at 3:19 pm

    in BufferArray::operator+, tempCharArray is a temporary buffer that will be destroyed when the function completes. There a basically two ways to handle this:

    1/ allocate the temporary buffer with new[] in operator+, that way you’ll make sure the buffer survives the call to operator+ but you’ll either have a memory leak or require the caller to invoke delete[] later on, which is rather clumsy and error-prone

    2/ or better yet, modify setBuffer so it does an internal copy of the buffer and add a call to delete[] in your own destructor :

    BufferArray::~BufferArray() {
        delete[] buffer;
    }
    
    void BufferArray::setBuffer(char *otherBuffer) {
        buffer = new char[strlen(otherBuffer) + 1];
        strcpy(buffer, otherBuffer);
    }
    

    Note that you’ll have to modify the constructor so it also copies the input buffer (otherwise you’ll have an illegal call to delete[] when the object is destroyed), and then you may want to overload the copy-constructor and assignment operator to prevent shallow copy which would result in double-deleting the buffer.

    In actual production code, you’d want to use a managed pointer of some sort to avoid doing the delete yourself (e.g. std::vector or boost::shared_array), but for homework the above solution should be fine.

    On a side note, don’t forget to add +1 when using strlen to determine the size of your buffer 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.