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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:14:14+00:00 2026-06-13T18:14:14+00:00

I have an array named A that contains random floats, with its size anywhere

  • 0

I have an array named A that contains random floats, with its size anywhere in the range from [10000, 1000000]. This array is a randomly generated argument to my function, and in order to operate on it I am trying to preemptively append/pad it with 10000 zeros in an efficient manner. I am worried that appending A’s allocated memory will corrupt the heap, so instead I allocate new memory, memcopy A, and memset 10000 trailing floats in the new Array to 0.0.

void *ArrayPadder(float *A){
    int pad = 10000;
    float *Apadded = (float*)malloc((sizeof(A)+pad)*sizeof(float));
    memcpy(Apadded, A, sizeof(A));
    memset(Apadded+sizeof(A), 0.0, pad);
    return Apadded;
}

Can anyone suggest a more efficient way of accomplishing this?

EDIT: Apologies for the delay but I added some clarification. The reason I can’t just pre-allocate the correct memory space (510000 floats) is because the array is actually of random size, containing random floats. I chose 500000 in an attempt to simplify the question, which is now fixed.

  • 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-13T18:14:15+00:00Added an answer on June 13, 2026 at 6:14 pm

    Not really a more efficient way, but a more accurate way:

    memset(Apadded+sizeof(A), 0, pad * sizeof(float));
    

    as if the size of float is 4, your code only initializes the first pad / sizeof(float) = 10000 / 4 = 2500 elements

    Note that I used 0 and not 0.0 as the second parameter, as memset takes an int and sets its (low byte) value to all of the bytes.

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

Sidebar

Related Questions

I have this method to save a mutable array named myWallet that contains instances
So, I have an array that contains X objects, all named by dates and
I have an array list named newSymptomList which contains a list of Symptom id's
I have an array which contains sets of three similar named items; however, sometimes
I have a large array in PHP. It contains strings that are split into
I have a php file that contains an array $myArray . <?php $myArray =
I have a form that contains and form of which code goes like this
I have table in database named ads, this table contains data about each ad.
I have an array that has keys and values. For eg: Array ( [name]
I have mutable string array named arrayout. It is having 3 element .Now I

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.