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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:39:20+00:00 2026-05-20T20:39:20+00:00

Here I have a function that creates a string, assigns it to a string

  • 0

Here I have a function that creates a string, assigns it to a string pointer, and returns it. I tried returning a regular string and it worked fine. But then when when I integrated the pointers and de-referenced them, my program crashed. When I tried to debug it, this is the message I got:

Unhandled exception at 0x00024cbf in Assignment 2.exe: 0xC0000005: Access violation reading location 0xcccccce4.

Here’s my code:

string* Recipe::getCookingTime()
// @intput: none
// @output: cooking time as a string
{
    string temp;
    string displayHrs;
    string displayMins;
    if( cookingTime_->numHours < 10 ) 
        displayHrs = intToString(0) + intToString(cookingTime_->numHours );
    else 
        displayHrs = intToString(cookingTime_->numHours );
    if( cookingTime_->numMinutes < 10 ) 
        displayMins = intToString(0) + intToString(cookingTime_->numMinutes);
    else 
        displayMins = intToString(cookingTime_->numMinutes);

    temp = "The time to cook the recipe is " + displayHrs + ":" + displayMins;
    *cTime_ = temp;
    return cTime_;
}
  • 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-20T20:39:21+00:00Added an answer on May 20, 2026 at 8:39 pm

    The problem is that you are dereferencing the cTime_ variable without actually allocating the memory first. I am not sure if that is a global variable or a member variable but you need to use the “new” operator first to allocate it’s memory. So you return the pointer to (address of) this variable back to the caller of the function, but as soon as this function exits, it is deleting the “temp” variable and therefore, the pointer you returned will be pointing at invalid memory.

    The solution would be to use the “new” operator:

    string* Recipe::getCookingTime()
    // @intput: none
    // @output: cooking time as a string
    {
        string displayHrs;
        string displayMins;
        if( cookingTime_->numHours < 10 ) 
            displayHrs = intToString(0) + intToString(cookingTime_->numHours );
        else 
            displayHrs = intToString(cookingTime_->numHours );
        if( cookingTime_->numMinutes < 10 ) 
            displayMins = intToString(0) + intToString(cookingTime_->numMinutes);
        else 
            displayMins = intToString(cookingTime_->numMinutes);
    
        if( NULL == cTime_ )
        {
            cTime_ = new string();
        }
    
        *cTime_ = "The time to cook the recipe is " + displayHrs + ":" + displayMins;
        return cTime_;
    }
    

    However, I have to warn you that this is not good design because here you are allocating memory and require that the call knows they have to free it when they are done with it. A preferable way to do it would be to have the caller allocate the variable and then pass in the pointer:

    bool Recipe::getCookingTime( string* str )
    // @intput: none
    // @output: cooking time as a string
    {
        if( NULL == str )
        {
            // Received invalid pointer
            return false;
        }
        string displayHrs;
        string displayMins;
        if( cookingTime_->numHours < 10 ) 
            displayHrs = intToString(0) + intToString(cookingTime_->numHours );
        else 
            displayHrs = intToString(cookingTime_->numHours );
        if( cookingTime_->numMinutes < 10 ) 
            displayMins = intToString(0) + intToString(cookingTime_->numMinutes);
        else 
            displayMins = intToString(cookingTime_->numMinutes);
    
        *str = "The time to cook the recipe is " + displayHrs + ":" + displayMins;
        return true;
    }
    

    Then when the caller wants to use the function they can do this:

    cTime_ = new string();
    getCookingTime( cTime_ );
    

    Summary
    The important thing to remember here is that you must allocate memory the a pointer is referencing before trying to assign to it. Also, it is generally bad design to allocate memory (using the new operator) within a function and not explicitly delete it. Whoever allocates memory should almost always be the one to free it

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

Sidebar

Related Questions

I have a PHP function that returns an array. This is the output of
I have working C++ code using swig which creates a struct, passes it to
I have an old application developed in Delphi 2006 that I now needed to
I am attempting to create a generic mapping function that takes the values of
I have identical code on my production and local *AMP servers. I've created a
I'm trying to make sure that I don't leave any loose ends open in
I'm having hard time understanding the following C# code. This code was taken from
I am parsing a text (css) file using fscanf. The basic goal is simple;
I managed to create a new line for each only with : $content1= hot(britney)
I am creating a login page for my web application. I want to create

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.