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

  • SEARCH
  • Home
  • 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 7399285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:02:28+00:00 2026-05-29T04:02:28+00:00

I need some help with operators new and delete I tried to create a

  • 0

I need some help with operators new and delete

I tried to create a class named big to handle huge numbers

#include <iostream>
using namespace std;

class big
{
protected:
    char *a;
    long lenght,maxlenght;
public:
    big()
    {
        maxlenght=256;
        a=new char[maxlenght];
        memset(a,'\0',maxlenght);
    }
    void read();

    void set_maxlenght(long x);

    long msize();

    long size();

    char* return_big();

    long at(long poz);

    char* operator[](long poz);

    void operator=(big x);

    void modify (long poz,long val);

    void dec (long poz);

    void inc (long poz);

    void operator-(big x);

    int compare(big x);
};




//functions

void write(big x)
{
    char *ptr;
    ptr=x.return_big();
    cout<<ptr;
}


//class big

    void big::read()
    {
        char *buffer;
        buffer=new char[maxlenght];
        memset(buffer,'\0',maxlenght);
        cin>>buffer;
        delete[] a;
        a=new char[strlen(buffer)];
        memset(a,'\0',maxlenght);
        for(long i=0;i<(long)strlen(buffer);i++)
            a[i]=buffer[i];
        lenght=strlen(buffer);
        delete[] buffer;
    }

    void big::set_maxlenght(long x)
    {
        maxlenght=x;
    }

    long big::msize()
    {
        return maxlenght;
    }

    long big::size()
    {
        return lenght;
    }

    char* big::return_big()
    {
        char *x;
        x=a;
        return x;
    }

    long big::at(long poz)
    {
        if(poz>=lenght)
            return -1;
        else
            return this->a[poz];
    }

    char* big::operator[](long poz)
    {
        if(poz>=lenght)
            return NULL;
        else
            return a+poz;
    }

    void big::operator=(big x)
    {
        a=new char[x.size()];
        for(long i=0;i<(long)strlen(x.a);i++)
            this->modify(i,x.at(i));
        this->lenght=strlen(x.a);
    }

    void big::modify (long poz,long val)
    {
        a[poz]=(char)val;
    }

    void big::dec (long poz)
    {
        if(a[poz])
            a[poz]--;
    }

    void big::inc (long poz)
    {
        if(a[poz]<255)
            a[poz]++;
    }

    void big::operator-(big z)
    {
        long n,m,i,j,aux;
        big rez,x,y;
        if(compare(z))
        {
            x=*this;
            y=z;
        }
        else
        {
            y=*this;
            x=z;
        }
        n=x.size();
        m=y.size();
        i=n;
        j=m;
        while(j>=0)
        {
            if(x.at(i)<0)
            {
                x.modify(i-1,x.at(i-1)+x.at(i));
                x.modify(i,0);
            }
            aux=x.at(i)-y.at(j);
            if(aux<0)
            {
                x.dec(i-1);
                rez.modify(i,aux+10);
            }
            else
                rez.modify(i,aux);
        }
        while(i)
        {
            i--;
            if(x.at(i)<0)
            {
                x.modify(i-1,x.at(i-1)+x.at(i));
                x.modify(i,0);
            }
            rez.modify(i,x.at(i));
        }
    }

    int big::compare(big x)
    {
        return strcmp(this->a,x.return_big());
    }

    int main()
    {
        big a,b;
        a.read();
        b.read();
        write(a);
        endl(cout);
        write(b);
        return 0;
    }

The first read is ok but the second has this error when it want to execute a=new char[strlen(buffer)]:

---Windows has triggered an error in MyProject.exe

This may be due to the corruption of the heap, which indicates a bug in MyProject.exe or any DLLs it has loaded.
This may also be due to the user pressing F12 while MyProject.exe has focus.
The output window may have more diagnostic information.---

There are also two buttons Break and Continue.
If I press continue it shows me this:

Unhandled exception at 0x77b8380e in Olimpiada a IX-a.exe: 0xC0000374: A heap has been corrupted.

I’m not very experienced with pointers (6 months). I would appreciate any answers.

  • 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-29T04:02:29+00:00Added an answer on May 29, 2026 at 4:02 am

    Your fault occurs here:

    delete[] a;
    a=new char[strlen(buffer)];
    memset(a,'\0',maxlenght);
    

    i.e. you’ve just resized your a pointer to, potentially, a smaller size, but, your maxlength variable still has the original size and can overwrite memory here. The patch would be:

    delete[] a;
    maxlength = strlen(buffer);
    a=new char[maxlenght];
    memset(a,'\0',maxlenght);
    

    Also, I’ve used your mispelt maxlenght variable name. It should be maxlength.

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

Sidebar

Related Questions

I need help understanding some C++ operator overload statements. The class is declared like
Need some help about with Memcache. I have created a class and want to
I am new to perl I need some help to write my output to
I need some help making this program for class. We are working with g++
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview
Need some help with this problem in implementing with XSLT, I had already implemented
Need some help gathering thoughts on this issue. Our team is moving ahead with
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
Need some help assigning a mouseover event to display some icons that start out

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.