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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:10:27+00:00 2026-06-05T10:10:27+00:00

The code below attempts to save a data stream to a file using fwrite.

  • 0

The code below attempts to save a data stream to a file using fwrite. The first example using malloc works but with the second example the data stream is %70 corrupted. Can someone explain to me why the second example is corrupted and how I can remedy it?

short int fwBuffer[1000000];
// short int *fwBuffer[1000000];
unsigned long fwSize[1000000];

// Not Working *********

if (dataFlow) {
       size =  sizeof(short int)*length*inchannels;
        short int tmpbuffer[length*inchannels];
        int count = 0;
        for (count = 0; count < length*inchannels; count++)
        {
            tmpbuffer[count] = (short int) (inbuffer[count]);
        }

        memcpy(&fwBuffer[saveBufferCount], tmpbuffer, sizeof(tmpbuffer));
        fwSize[saveBufferCount] = size;

        saveBufferCount++;
        totalSize += size;
    }
// Working ***********

if (dataFlow) { 
    size =  sizeof(short int)*length*inchannels;
    short int *tmpbuffer = (short int*)malloc(size);

    int count = 0;
    for (count = 0; count < length*inchannels; count++)
    {
        tmpbuffer[count] = (short int) (inbuffer[count]); 
    }

    fwBuffer[saveBufferCount] = tmpbuffer;
    fwSize[saveBufferCount] = size;

    saveBufferCount++;
    totalSize += size;
}


// Write to file ***********

    for (int i = 0; i < saveBufferCount; i++) {
        if (isRecording && outFile != NULL) { 
  //        fwrite(fwBuffer[i], 1, fwSize[i],outFile);
            fwrite(&fwBuffer[i], 1, fwSize[i],outFile);
            if (fwBuffer[i] != NULL) {
  //           free(fwBuffer[i]);
            }
            fwBuffer[i] = NULL;
        }
    }       
  • 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-05T10:10:29+00:00Added an answer on June 5, 2026 at 10:10 am

    You initialize your size as

    size = sizeof(short int) * length * inchannels;
    

    then you declare an array of size

    short int tmpbuffer[size];
    

    This is already highly suspect. Why did you include sizeof(short int) into the size and then declare an array of short int elements with that size? The byte size of your array in this case is

    sizeof(short int) * sizeof(short int) * length * inchannels
    

    i.e. the sizeof(short int) is factored in twice.

    Later you initialize only length * inchannels elements of the array, which is not entire array, for the reasons described above. But the memcpy that follows still copies the entire array

    memcpy(&fwBuffer[saveBufferCount], &tmpbuffer, sizeof (tmpbuffer));
    

    (Tail portion of the copied data is garbage). I’d suspect that you are copying sizeof(short int) times more data than was intended. The recipient memory overflows and gets corrupted.

    The version based on malloc does not suffer from this problem since malloc-ed memory size is specified in bytes, not in short int-s.

    If you want to simulate the malloc behavior in the upper version of the code, you need to declare your tmpbuffer as an array of char elements, not of short int elements.

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

Sidebar

Related Questions

The code listed below attempts to update a row in the database, but throws
I am using PHP 5.3.6 I have the following code below. Everything works fine
Below is the code I am using and the error happens when I attempt
Code below is used to save PostgreSql database backup from browser in Apache Mono
The code below working for only first div. I can't display other divs when
My question has to do with facetting. In my example code below, I look
I am using WebTechNick's file upload plugin to save images on my CakePHP site.
The below code attempts to illustrate what I want. I basically want two instances
Code below is working well as long as I have class ClassSameAssembly in same
The code below generates a warning CS3006 Overloaded method MyNamespace.Sample.MyMethod(int[])' differing only in ref

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.