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

  • Home
  • SEARCH
  • 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 4272632
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:36:14+00:00 2026-05-21T07:36:14+00:00

I try to wrote code that read data from stdin: size_t bufSize = 1024;

  • 0

I try to wrote code that read data from stdin:

size_t bufSize = 1024;
unsigned char *msg = NULL;
size_t msgBytes = 0;
size_t inputMsgBufCount = 0;
unsigned char inputBuffer[bufSize];
size_t bytesRead = 0;
unsigned char *tmp = NULL;

if ((msg = (unsigned char *)malloc(sizeof(unsigned char) * bufSize)) == NULL)
    exit(EXIT_FAILURE);
bytesRead = fread(msg, sizeof(unsigned char) * bufSize, 1, stdin);
inputMsgBufCount++;

while (bytesRead) {
    printf("iteration: %lu\n", inputMsgBufCount);
    if ( (tmp = (unsigned char *)realloc(msg, (inputMsgBufCount * bufSize) + bufSize)) != NULL ) {
         printf("reallocated\n");
        msg = tmp;
        inputMsgBufCount++;
    }
    else {
        printf("Ran out of memory\n");
        free(msg);
    }
    bytesRead = fread(inputBuffer, sizeof(unsigned char) * bufSize, 1, stdin);
    memmove((msg + (inputMsgBufCount * bufSize)), inputBuffer, bufSize);
}

free(msg);

msgBytes = (inputMsgBufCount * bufSize);

gettimeofday(&end, NULL);
printf("%10.6lf [MB/s]\n", (msgBytes / (1<<20)) / ( (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) * 1.0e-6f ));

But after run it like this:
~# dd if=/dev/zero bs=1024 count=8 | ./test
I have this error:

iteration: 1
reallocated
iteration: 2
reallocated
iteration: 3
reallocated
iteration: 4
reallocated
iteration: 5
reallocated
iteration: 6
reallocated
iteration: 7
test(11450) malloc: *** error for object 0x100804008: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
Abort trap

Can anyone help me please.

  • 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-21T07:36:14+00:00Added an answer on May 21, 2026 at 7:36 am

    inputMsgBufCount is supposed to be incremented after you copy the data in your msg buffer. You do it right on your first read, but for everything else, you increment it immediately after you realloc. If you follow the values for it, it is 1 as you enter the loop. You reallocate to 2*bufSize, and increment inputMsgBufCount, which makes it 2. Then you read the data, and copy it to msg+2*bufSize. This is corrupting your buffer. You should have copied to msg+bufSize. Simply delay incrementing the variable until after you copy your data.

    On another note, you can safely use memcpy() to copy data. msg and inputBuffer will never overlap. Actually you should probably get rid of inputBuffer altogether, and read directly into msg at the right offset.

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

Sidebar

Related Questions

I have some SerialPort code that constantly needs to read data from a serial
Possible Duplicate: Convert some code from C++ to C I've got some code that
I'm trying to read binary data from a specific offset. I write the data
What I am trying to do is to read in data from a socket
I want to read status information that an application provides via shared memory. I
Working on an application to parse robots.txt. I wrote myself a method that pulled
I've a requirement that I need to read an excel sheet programmatically using asp.net/C#
This is a code that will attempt to record an audio sample : but
I am attempting to use mechanize to veiw and collect data from various websites.
I'm cleaning up some pretty complicated code that I didn't write and I'm looking

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.