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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:22:51+00:00 2026-05-13T08:22:51+00:00

I am using HDF5 to read a string into a char* allocated by new[]

  • 0

I am using HDF5 to read a string into a char* allocated by new[]. I then use a string::assign() call to copy this data to where I actually want it. I then call delete[] on that char*. This is showing up as the source of a memory leak using totalview. It shows mangled calls in stdlibc++ under delete[] to replace_safe, mutate, create, then malloc. What is going on, and is this really a memory leak? I have set GLIBCXX_FORCE_NEW=1 here as well.

Here is example code that duplicates the situation. Note that valgrind shows no leaks and if I don’t put a breakpoint before the cout call, there is no leak found by totalview.

#include <string>
#include <iostream>
#include <cstdlib>

int main()
{
    std::string str;

    int len = strlen(getenv("PATH"));
    char* x = new char[len + 1];
    strcpy(x, getenv("PATH"));
    x[len] = '\0';

    str.assign(x);

    delete[] x;

    std::cout << str << std::endl;
}
  • 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-13T08:22:51+00:00Added an answer on May 13, 2026 at 8:22 am

    It should be fine:
    But I would suggest using std::vector rather than newing an array of char:

    std::vector<char>  x(len+1);
    strcpy(&x[0], getenv("PATH"));
    

    The reason I would do this is that the method assign() can potentially throw an exception. As such the delete may not be called and thus you could leak in the presence of an exception. Using the vector you grantee the memory is cleaned up because of RAII.

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

Sidebar

Related Questions

So I've been trying to read this particular .mat file into R. I don't
I have daily stock data as an HDF5 file created using PyTables. I would
I am new to PyTables, and am looking at using it to process data
Using a rails console I create a Model record, then retrieve using a new
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using CI for the first time and i'm smashing my head with this seemingly
Using import datetime in python, is it possible to take a formatted time/date string
Or should I use a different hammer to fix this problem. I've got a
Using linq2sql I'm trying to take the string in txtOilChange and update the oilChange
I've just discovered the HDF5 format and I'm considering using it to store 3D

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.