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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:45:19+00:00 2026-05-14T14:45:19+00:00

When I convert char* to an string it gives an bad memory allocation error

  • 0

When I convert char* to an string it gives an bad memory allocation error in ‘new.cpp’ . I used following method to convert char* called ‘strData’ and ‘strOrg’ to string.

   const char* strData = dt.data();
   int length2 = dt.length();
   string s1(strData);

First time it work without any problem. But in the second convertion it gives above error. When I swap the two conversion in the order, it give the error always in the second conversion regardless of the char* I am converting. Whole code is shown in the following.

    mysqlpp::Query query = conn.query("SELECT data,origin from image where id =2");
    mysqlpp::UseQueryResult res = query.use();
    mysqlpp::Row eee= res.fetch_row();
    mysqlpp::Row::reference dt = eee.at(0);
    mysqlpp::Row::reference org = eee.at(1);

    const char* strData = dt.data();
    int length2 = dt.length();
    string s1(strData);
    istringstream is1(s1);  
    char * imgData =  new char;
    is1.read(reinterpret_cast<char *> (imgData), length2);
    delete [] strData;

    const char* strOrg = org.data();
    int length3 = org.length();
    string s2(strOrg);
    istringstream is2(s2);  
    char * imgOrg =  new char;
    is2.read(reinterpret_cast<char *> (imgOrg), length3);
    delete [] strOrg;

This where the error comes from

    void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)
    {       
    void *p;
    while ((p = malloc(size)) == 0)
            if (_callnewh(size) == 0)
            {       // report no memory
            static const std::bad_alloc nomem;
            _RAISE(nomem);
            }

    return (p);
    }

How can I solve this?

  • 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-14T14:45:20+00:00Added an answer on May 14, 2026 at 2:45 pm

    Instead of

    char * imgData = new char;
    is1.read(reinterpret_cast<char *> (imgData), length2);
    

    try

    char * imgData = new char[length2];
    is1.read(reinterpret_cast<char *> (imgData), length2);
    

    When you read data from an istringstream using read, the buffer you provide must have enough space to hold the results!

    If you call new char; you get space for one char. Use new char[n]; to get space for n.

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

Sidebar

Related Questions

Given public String(char*) why we cannot use the following statement? string str = new
I'm trying to use std::ostringstream to convert a number into a string (char *),
How can I convert a Unicode string to a char* or char* const in
What I want to do is to convert a string into const char*. Ideally
I have unsigned char* , want to convert it to std::string . Can you
I was trying to convert a QString to char* type by the following methods,
How can I convert a string in to char * I was using the
Here is a simple function that converts a string to an integer. int str2int(char
how to convert char to LPCTSTR in vc++ I am using MVC. QByteArray qBary;
Can you help me look for a way to convert char into 8x8 binary

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.