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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:28:25+00:00 2026-06-09T17:28:25+00:00

I’m relatively new to C++ memory management, and I’m getting this weird error of

  • 0

I’m relatively new to C++ memory management, and I’m getting this weird error of heap corruption (plus an automatic breakpoint in Visual Studio before it). Here is the offending code:

z_world::z_world(char* name)
{
    unsigned int i, skip;
    char tmp;

    //Load data from file
    std::string* data = loadString(name);

    //Base case if there is no world data
    tiles = NULL;

    w = 0;
    h = 0;

    if(data->length() > 0) {
        //Set up the 'tiles' array
        for(i = 0; i < data->length(); i++) {
            if(data->at(i) == '\n')
                h++;
            if(h == 0)
                w++;
        }
        tiles = new int[data->length()-h];

        //Load Data
        skip = 0;
        for(i = 0; i < data->length(); i++) {
            if(data->at(i) == '\n') {
                skip++;
                printf("\n");
                continue;
            }
            tmp = data->at(i);
            tiles[i+skip] = atoi(&tmp);
            printf("%i ",tiles[i+skip]);
        }
    }
    delete data;
}

Here’s where I load in the string:

std::string* loadString(char* name)
{
    ifstream in(name);
    std::string* input = new string();

    while(in) {
        std::string line;
        getline(in,line);
        input->append(line);
        input->append("\n");
    }

    in.close();

    return input;
}

I get the breakpoint and error inside of “delete data;”, which makes me think that “data” gets deleted somewhere before that, but I can’t find where it would. For reference, this method is to create an object that contains world data for a game in the form of a virtual 2D integer array (for the ID’s of the tiles).

  • 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-06-09T17:28:27+00:00Added an answer on June 9, 2026 at 5:28 pm

    Youre problem is probably here:

    tiles[i+skip] = atoi(&tmp);
    

    Problem 1:
    It should be -skip

    tiles[i - skip] =
    

    Problem 2:
    The atoi() command is being used incorrectly (tmp does not contain a string). But also I don’t think atoi() is the appropriate method. I think what you are looking for is simple assignment. The conversion from char to int is automatic:

     tiles[i - skip] = tmp;
    

    Problem 3:
    You are not using objects correctly. In this situation there is no need to generate dynamic objects and create a mess with dynamic memory management. It would be simpler to just to create automatic objects and pass those back normally:

    std::string* loadString(char* name)
          //   ^  Don't do this.
    
    
    
    std::string loadString(std::string const& name)
    //  ^^^^^^^  return a string by value.
    //           The compiler will handle memory management very well.
    

    In general you should not be passing pointers around. In the few situations where you do need pointers they should be held within a smart pointer object or containers (for multiple objects) so that their lifespan is correctly controlled.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string

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.