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 7190449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:31:05+00:00 2026-05-28T19:31:05+00:00

I have a class Foo defined like this: class Foo { public: Foo(int num);

  • 0

I have a class Foo defined like this:

class Foo
{
  public:
    Foo(int num);
    Foo(const Foo& other);
    ~Foo();

    Foo& operator=(const Foo& other);
    ...

  private:
    string *fooArray;
    void clearMemory();
    ...
}

Foo::Foo(int num)
{
    fooArray = new string[num];
}

Foo::Foo(const Foo& other)
{
    *this = other;
}

Foo::~Foo()
{
    clearMemory();
}

Foo& operator=(const Foo& other)
{
    clearMemory();
    fooArray = new string[other.size]; // size is a private variable
    memcpy(fooArray, other.fooArray, other.size * sizeof(string));
}

void Foo::someOtherFooFuncion(int newNum)
{
    clearMemory(); // crash with Visual Studio, does not crash with g++, but g++ also
                   // crashes when destructor is called
    fooArray = new string[newNum];
}

void Foo::clearMemory()
{
    if(fooArray != NULL)
    {
        delete[] fooArray; // sometimes it crashes here with g++; it always crashes when
                           // compiling in Visual Studio
        fooArray = NULL;
    }
}

As noted in the code comments, it is giving me crashes at times. I have tried following the steps in GDB, I got as far as

destructing Foo:
@0x7fff5fbff9b0
$26 = {
  fooArray = 0x1001009d8, 
  ...
}

And then delete[] fooArray is reached, and all of a sudden

Foo(49858) malloc: *** error for object 0x100100990: pointer being freed was not allocated

Have no idea where the 0x100100990 came from.

I realize the code is very incomplete, but I really don’t know even where to start hunting for the bug right now, and would like some tips as to what possible conditions could cause delete[] errors.

EDIT:

Added c’tor, d’tor, and assignment operator. I am away from PC so code may not be 100% accurate. Assigning values to fooArray and accessing them works just fine though.

Also, I would greatly appreciate a general list of problems that could potentially cause delete[] to throw an error, so that I could at least have some idea of where to look in the code.

EDIT 2:

So I followed Xeo’s advice to use std::uninitialized_copy, now the code works fine and compiles under g++. The destructor works fine in Visual Studio as well, but somehow deleting fooArray in someOtherFooFuncion makes it crash.

Any other ideas?

  • 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-28T19:31:06+00:00Added an answer on May 28, 2026 at 7:31 pm

    Do not use memcpy in C++

    I can’t stress that point enough. When memcpying a class object in C++ (non-POD to be exact), you’re breaking that class’ invariants defined by its constructors, since you are circumventing exactly those. Every time you memcpya a std::string class, you get a new object referring to the same memory as another object. You’ll get a double delete with this, which is causing your crash.

    Use std::uninitialized_copy from <algorithm> like this:

    //                      src_begin             src_end           dest
    std::uninitialized_copy(other.array, other.array + other.size, array);
    

    (untested because I’m writing this from my iPod)

    Or even better, just use std::vector instead of raw memory. You’ll have no need for a destructor and copy ctor / assignment operator anymore.

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

Sidebar

Related Questions

I have a class class foo { public: foo(); foo( int ); private: static
I have an example class defined like below: public class FooBar { void method1(Foo
I have joined models like this: // /foo/lib/model/doctrine/PurchasedItemTable.class.php public function retrievePurchased(Doctrine_Query $q) { $rootAlias
I have a serial of object defined as: public class Foo { public DateTime
I have: class Foo { int a; int b; std::string s; char d; };
I have a spring controller defined like this: @Controller @RequestMapping(/user) class UserController { ...
If I have a parent-child that defines some method .foo() like this: class Parent
I have three different base classes: class BaseA { public: virtual int foo() =
Suppose I have this class hierarchy: class A { public: virtual void foo(Base *b)
I have defined an assembly level attribute class FooAttribute like this: namespace Bar {

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.