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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:53:28+00:00 2026-05-17T22:53:28+00:00

I am reading strings in C++ using fread where I am reading and storing

  • 0

I am reading strings in C++ using fread where I am reading and storing shortSiteText in siteNames_. siteNames_ is declared as std::vector<char*> siteNames_; I use siteNames_ in other functions but because shortSiteText is a pointer, when I call the delete command on it, the last entry in siteNames_ is changed. How do I prevent this?

for (unsigned int i = 0; i <= nSites; i ++){
   fread((char *) &shortSiteTextLength, 1, sizeof shortSiteTextLength, baseFile_);
   shortSiteText = new char[shortSiteTextLength];
   fread(shortSiteText,1, shortSiteTextLength,baseFile_);
   siteNames_.push_back(shortSiteText);
}
delete [] shortSiteText;

I tried to use the dereference operator: siteNames_.push_back(*shortSiteText); but that generates a compiler error.

NOTE: I must use fread and char* due to legacy code.

  • 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-17T22:53:29+00:00Added an answer on May 17, 2026 at 10:53 pm

    Let’s zoom on this:

    shortSiteText = new char[shortSiteTextLength];
    siteNames_.push_back(shortSiteText);
    delete [] shortSiteText;
    

    Explanation: The second line just pushes a pointer to the array, not the array itself. The first line then desallocate the array, on which the last element of siteNames still points to; this leads to undefined behavior when you use this element.

    Hack: Remove delete [] shortSiteText

    Real fix: You encounter this problem because you try to manage object ownerships yourself. Don’t ! Here, you can use std::string and still be able to work with legacy code, with the c_str() member function.

    To quote a friend of mine:

    As a general rule, if you are a
    beginner and your code contains the
    word ‘char’, you have a bug.

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

Sidebar

Related Questions

I'm trying to combine List<string> strings using string.Join(,, strings) but everything I'm reading says
I am reading a csv-file with JDBC, using CsvJdbc . Some strings in the
Possible Duplicate: Parsing Dates and Times from Strings using python I'm reading a string
I have a winforms applications which is reading strings using sockets and the sockets
I am reading a file using perl script. This file consists of strings with
I'm using a thread to read Strings from a stream (/dev/tty1) while processing other
A while back I was reading the W3C article on ' Re-using Strings in
I stumbled upon an ugly problem the other day: I'm using XML strings to
I have implemented the TTS support for reading the Text Strings in my Application,
I'm reading a comma-delimited list of strings from a config file. I need to

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.