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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:54:09+00:00 2026-06-08T00:54:09+00:00

If I initialize a temp variable (say a char *, NOT with new) in

  • 0

If I initialize a temp variable (say a char *, NOT with “new”) in the loop of a function, do I then need to explicitly free it or will it be taken care of when it goes out of scope? The latter is what I’ve been taught… When I run my code through Valgrind I’m getting “15 bytes definitely lost” but they seem to be in areas similar to above…temp variables that go out of scope. These are not things allocated with “new”… Does Valgrind get some of it’s numbers based on what the code looks like or is it actually catching orphaned memory? When Valgrind is used do most people expect to see some small numbers lost or is 0 bytes lost the name of the game? Thanks guys.

==7669== 15 bytes in 1 blocks are definitely lost in loss record 1 of 1                    │        std::cout << "    ...printing newly initia
==7669==    at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memc│lized customer..." << std::endl;                  
heck-x86-linux.so)                                                                         │        if (true)                                 
==7669==    by 0x8049C4B: Customer::copyString(char const*&, char*&) (customer.cpp:539)    │        {                                         
==7669==    by 0x804992A: Customer::getName(char*&) (customer.cpp:379)                     │                const char * name = "bunches, oate
==7669==    by 0x8048C2D: main (main.cpp:78)                                               │s";                                               
==7669==                                                                                   │                Customer cus5(name);              
==7669== LEAK SUMMARY:                                                                     │                cus4 = cus5;                      
==7669==    definitely lost: 15 bytes in 1 blocks                                          │        }                                         
==7669==    indirectly lost: 0 bytes in 0 blocks                                           │                                                  
==7669==      possibly lost: 0 bytes in 0 blocks                                           │        cus4.displayData();                       
==7669==    still reachable: 0 bytes in 0 blocks                                           │        std::cout << std::endl;                   
==7669==         suppressed: 0 bytes in 0 blocks  

Both the “copyString()” method and the “getName()” method only have temp variables…everything else is freed… “main.cpp” has some memory that I haven’t freed but as it’s my testing harness I’m not too worried about cleaning that up at this point. If things are listed by Valgrind (such as the 3 lines: copyString, getName, and main) does that mean they are definitely loosing memory or just that they might be?

This is the “getName()” function:

int Customer::getName(char *& toReturn)
{
        if (name)
        {
                const char * nameCopy = name; //casting
                delete toReturn;
                copyString(nameCopy, toReturn);
                delete nameCopy;
                return 1;
        }
        else
        {
                toReturn = NULL;
                return 0;
        }
}

This is the copyString() function:

int Customer::copyString(const char *& toCopy, char *& set)
{
        //prevent strlen or strcpy from being called on NULL
        if (!toCopy)
                return -1;

        //copy data into new char * and set "set"
        int length = strlen(toCopy) + 1;
        char * new_name = new char[length];
        strcpy(new_name, toCopy);

        delete set; //delete anything pointed to by this
        set = new_name;
        return 1;
}

Also I should add, just to stave off questions, I’m using the cstring library because it is a requirement of the class I’m taking. I’m going above and beyond by profiling my code at this point…just trying to learn as much as possible… Thanks!

  • 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-08T00:54:11+00:00Added an answer on June 8, 2026 at 12:54 am

    First of all, you should try to fix memory leaks, unless you’re certain that your application won’t run for a long time and they won’t cause many problems. However, don’t use this to justify not fixing them. They can cause plenty of problems once your application get’s bigger.

    Second of all, i noticed that you used C++ and C-style strings. It is better to use stl::string, since they already manage the memory, while with char* strings you need to do it all by yourself and mistakes can appear very easy.

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

Sidebar

Related Questions

To initialize an int array with all zeros, do I need to use: int
Objective Initialize a new addition problem for users to solve when they visit /task
Can I initialize an IOLoop.instance and than fork new processes, which uses IOLoop.instance()? Something
var homeView = Backbone.View.extend({ el: $(#main_container), initialize: function(){ _.bindAll(this, 'render'); }, render:function(){ $.get('/home', {},
//initialize the people data list - (void)initializePeopleListFromJson:(CLLocationCoordinate2D)coordinate auraId:(NSString *)auraId { //initialize the NSMutableArray list
When initialize an entity framework context. One is to initialize at class level, such
def initialize(apps, catch=404) @apps = []; @has_app = {} apps.each { |app| add app
I initialize the tweet button at the beginning of my app, after user interaction
I am trying to initialize a vector which has integers 1 to n in
I'm trying to initialize a Datatable with a default search value that the user

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.