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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:21:05+00:00 2026-06-12T13:21:05+00:00

I have a function that allocated a buffer for the size of a file

  • 0

I have a function that allocated a buffer for the size of a file with

char *buffer = new char[size_of_file];

The i loop over the buffer and copy some of the pointers into a subbuffer to work with smaller units of it.

char *subbuffer = new char[size+1];

for (int i =0; i < size; i++) {
  subbuffer[i] = (buffer + cursor)[i];
}

Next I call a function and pass it this subbuffer, and arbitrary cursor for a location in the subbuffer, and the size of text to be abstracted.

  wchar_t* FileReader::getStringForSizeAndCursor(int32_t size, int cursor, char *buffer) {

  int wlen = size/2;

  #if MARKUP_SIZEOFWCHAR == 4 // sizeof(wchar_t) == 4
  uint32_t *dest = new uint32_t[wlen+1];
  #else
  uint16_t *dest = new uint16_t[wlen+1];
  #endif

  char *bcpy = new char[size];
  memcpy(bcpy, (buffer + cursor), size+2);



  unsigned char *ptr = (unsigned char *)bcpy; //need to be careful not to read outside the buffer


  for(int i=0; i<wlen; i++) {
      dest[i] = (ptr[0] << 8) + ptr[1];
      ptr += 2;
  }
  //cout << "size:: " << size << " wlen:: " << wlen << " c:: " << c << "\n";

  dest[wlen] = ('\0' << 8) + '\0';
  return (wchar_t *)dest;
}

I store this in a value as the property of a struct whilst looping through the file.

My issue seems to be when I free subbuffer, and start reading the title properties of my structs by looping over an array of struct pointers, my app segfaults. GDB tells me it finished normally though, but a bunch of records that I cout are missing.

I suspect this has to do with function scope of something. I thought the memcpy in getStringForSizeAndCursor would fix the segfault since it’s copying bytes outside of subbuffer before I free. Right now I would expect those to then be cleaned up by my struct deconstructor, but either things are deconstructing before I expect or some memory is still pointing to the original subbuffer, if I let subbuffer leak I get back the data I expected, but this is not a solution.

  • 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-12T13:21:06+00:00Added an answer on June 12, 2026 at 1:21 pm

    The only definite error I can see in your question’s code is the too small allocation of bcpy, where you allocate a buffer of size size and promptly copy size+2 bytes to the buffer. Since you’re not using the extra 2 bytes in the code, just drop the +2 in the copy.

    Besides that, I can only see one suspicious thing, you’re doing;

    char *subbuffer = new char[size+1];
    

    and copying size bytes to the buffer. The allocation hints that you’re allocating extra memory for a zero termination, but either it shouldn’t be there at all (no +1) or you should allocate 2 bytes (since your function hints to a double byte character set. Either way, I can’t see you zero terminating it, so use of it as a zero terminated string will probably break.

    @Grizzly in the comments has a point too, allocating and handling memory for strings and wstrings is probably something you could “offload” to the STL with good results.

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

Sidebar

Related Questions

In Cython, say I have a C function that returns a large buffer allocated
I have a function that have a char* as parameter to receive some informations
I have to write a function that fills a char* buffer for an assigned
In my Java code I have function that gets file from the client in
I have some code that I had to write to replace a function that
I have a function that scans a file and returns the number of the
Inside my Controller i have function that runs after user clicks on item, which
So I have function that formats a date to coerce to given enum DateType{CURRENT,
I have function Start() that is fired on ready. When I click on .ExampleClick
I have a function that returns two values in a list. Both values need

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.