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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:09:04+00:00 2026-05-25T15:09:04+00:00

In this code I present below I am having a problem. It manifests as

  • 0

In this code I present below I am having a problem. It manifests as a bad_alloc exception, and this is because the argument passed to reader, the CompressedBufferReader, is a bad string.

class FileReader { 
    FILE *file; 
protected:
    unsigned char *data; // local copy
    long size;
public:
    FileReader(const char *filename);
    ~FileReader();
    unsigned long getSize();
    unsigned char *getFileData();
};

class CompressedBufferReader {
    unsigned char *buffer;
    unsigned long len;
public:
    CompressedBufferReader(unsigned char *);
    ~CompressedBufferReader();
    unsigned char *getBuffer();
    unsigned long getLength();
};
CompressedBufferReader::CompressedBufferReader(unsigned char *srcCompressed) {
    len = 0; buffer = 0;
    len = GetDecompressedBufferSize(srcCompressed);
    buffer = new unsigned char[len]; if (!buffer) throw std::runtime_error("Cannot allocate!");
    WriteDecompressedBuffer(buffer, len, srcCompressed);
}
CompressedBufferReader::~CompressedBufferReader() {
    delete[] buffer;
}
unsigned char *CompressedBufferReader::getBuffer() {return buffer;}
unsigned long CompressedBufferReader::getLength() {return len;}

// similar interface to FileReader. Does not inherit because it does not benefit from doing so.
class CompressedFileReader {
    CompressedBufferReader reader;
public:
    CompressedFileReader(const char *filename);
    unsigned char *getFileData();
    unsigned long getSize();
};

CompressedFileReader::CompressedFileReader(const char *filename) : reader(FileReader(filename).getFileData()){} // this line is causing the problem
unsigned char *CompressedFileReader::getFileData() { return reader.getBuffer(); }
unsigned long CompressedFileReader::getSize() { return reader.getLength(); }

To be more specific, it seems like the FileReader which I create anonymously becomes deallocated before its data contents can be passed to the constructor of reader, a CompressedBufferReader.

the problem is that I cannot write CompressedFileReader‘s constructor in a way that allows me to properly instantiate a FileReader, because I intend to use CompressedBufferReader‘s constructor and that means I must call it before the body of the constructor. Bit of a catch-22. How is this issue resolved?

  • 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-25T15:09:05+00:00Added an answer on May 25, 2026 at 3:09 pm

    I don’t see a problem in the way you use the constructors, which should cause the bad_alloc (though the code does seem clumsy). Let us look at the execution of the line that is causing the problem –

    CompressedFileReader::CompressedFileReader(const char *filename) : 
       reader(FileReader(filename).getFileData()){}
    

    The following steps take place:

    1. A temporal FileReader is created. Its constructor is called with filename, which is a const char*.
    2. The constructor does unknown things, because we don’t have its code. It should, I assume, read the file into an allocated buffer, stored at the data member of the FileReader.
    3. getFileData() is called on the temp FileReader, returning the value of data, I assume, which is an unsigned char *.
    4. reader, which is a CompressedBufferReader, is constructed using the unsigned char *.
    5. The temporal FileReader is destructed.

    So, the problem is not with the order of the constructions, or the lifespan of the temporary FileReader. There are a few unknown which you should look into:

    1. Does the constructor of FileReader create a valid buffer, and stores it in data?
    2. Does getFileData() return the buffer created?
    3. Does GetDecompressedBufferSize() return the correct value for a valid buffer?
    4. Does the exception happen to be thrown from WriteDecompressedBuffer, whose code we don’t have?

    Lastly, you might want to simplify your code. Constructions like that aren’t very readable. And, of course, the use of standard container like vectors would make it safer.

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

Sidebar

Related Questions

I am using this code to check that array is present in the HashMap
I am having some trouble with a piece of code below: Input: li is
I have this code below. How can I set a cell value to =
I could swear this was working yesterday. Now however the code below destroys the
This code in JS gives me a popup saying i think null is a
This code is from Prototype.js . I've looked at probably 20 different tutorials, and
this code always returns 0 in PHP 5.2.5 for microseconds: <?php $dt = new
This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
This code always works, even in different browsers: function fooCheck() { alert(internalFoo()); // We
This code does not seem to compile, I just need to write something to

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.