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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:41:31+00:00 2026-05-15T13:41:31+00:00

On occasion, the following code works, which probably means good concept, but poor execution.

  • 0

On occasion, the following code works, which probably means good concept, but poor execution. Since this crashes depending on where the bits fell, this means I am butchering a step along the way. I am interested in finding an elegant way to fill bufferdata with <=4096 bytes from buffer, but admittedly, this is not it.

EDIT: the error I receive is illegal access on bufferdata

unsigned char        buffer[4096] = {0};
char *bufferdata;

bufferdata = (char*)malloc(4096 * sizeof(*bufferdata));
if (! bufferdata)
    return false;

while( ... )
{

    // int nextBlock( voidp _buffer, unsigned _length );
    read=nextBlock( buffer, 4096);

    if( read > 0 )
    {
        memcpy(bufferdata+bufferdatawrite,buffer,read);

        if(read == 4096) {

            // let's go for another chunk
            bufferdata = (char*)realloc(bufferdata, ( bufferdatawrite + ( 4096 * sizeof(*bufferdata)) ) );
            if (! bufferdata) {
                printf("failed to realloc\n");
                return false;
            }

        }

    }
    else if( read<0 )
    {
        printf("error.\n");
        break;
    }
    else {
        printf("done.\n");
        break;
    }
}


free(bufferdata);
  • 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-15T13:41:32+00:00Added an answer on May 15, 2026 at 1:41 pm

    It’s hard to tell where the error is, there’s some code missing here and there.

    if(read == 4096) { looks like a culprit, what if nextBlock, returned 4000 on one iteration, and 97 on the next ? Now you need to store 4097 bytes but you don’t reallocate the buffer to accomodate for it.

    You need to accumulate the bytes, and realloc whenever you pass a 4096 boundary.
    something like:

    #define CHUNK_SIZE 4096
    int total_read = 0;
    int buffer_size = CHUNK_SIZE ;
    char *bufferdata = malloc(CHUNK_SIZE );
    char buffer[CHUNK_SIZE];
    while( ... )
    {
    
        // int nextBlock( voidp _buffer, unsigned _length );
        read=nextBlock( buffer, CHUNK_SIZE );
    
        if( read > 0 )
        {
            total_read += read;
            if(buffer_size < total_read) {
               // let's go for another chunk
                char *tmp_buf;
                tmp_buf= (char*)realloc(bufferdata, buffer_size + CHUNK_SIZE );
                if (! tmp_buf) {
                    free(bufferdata);
                    printf("failed to realloc\n");
                    return false;
                }
                buffer_data = tmp_buf;
                buffer_size += CHUNK_SIZE ;
    
            }
            memcpy(bufferdata+total_read-read,buffer,read);
          }
          ... 
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On occasion, I get the following error in design mode of some user controls
At work we use confluence . On occasion it crashes, resulting in an error
So, I have the following line of code, that does a for loop and
On occasion, my local Rails app loses its connection to MySQL. I get some
On occasion, I find myself wanting to search the text of changelist descriptions in
I have the occasion to produce Drupal web sites using development, staging, and production
On more than one occasion I've been asked to implement rules for password selection
I have a PHP application that will on occasion have to handle URLs where
This is a theoretical question, as I can't imagine any practical uses. I made
I have the following method in a base class that implements System.ComponentModel.INotifyPropertyChanged: protected virtual

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.