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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:53:21+00:00 2026-05-18T01:53:21+00:00

First, this can be a general algorithm for any language, but I’m learning C

  • 0

First, this can be a general algorithm for any language, but I’m learning C and if there is some C specific features, I’d like to know!

I’m writing a function that will allocate enough memory for a given number of bits; into a long long * variable. The number of bits cannot be < 1. I tested the algorithm :

int bits;  // the function argument, checked for value > 0
size_t dataSize;  // the value passed to the malloc function

for (bits = 1; bits<100; bits++) {
   if (bits < sizeof(long long)) {
      dataSize = 1;
   } else {
      dataSize = (bits + (sizeof(long long) - (bits % sizeof(long long)))) / sizeof(long long);
   }

   printf("%d = %d\n", bits, (int) dataSize);
}

It looks ok… but ugly 🙂
Any way to have a more elegant way to achieve this?
Thank you!

  • 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-18T01:53:21+00:00Added an answer on May 18, 2026 at 1:53 am

    Assuming you want to initialize your bit-field to zero, calloc() might be preferable to malloc(); you probably also should use an unsigned type to avoid signed shifts when twiddling bits.

    #include <limits.h>
    
    const size_t BITS_PER_BLOCK = sizeof (long long) * CHAR_BIT;
    size_t count = bits / BITS_PER_BLOCK + !!(bits % BITS_PER_BLOCK);
    unsigned long long *blocks = calloc(count, sizeof *blocks);
    

    The !! is a somewhat hackish way to convert non-zero values to 1, which is common in C and used here to allocate an additional block if the number of bits is not divisible by BITS_PER_BLOCK.

    You could also get the required number of blocks (as – among others – Lars pointed out in the comments to another answer) via

    size_t count = (bits + BITS_PER_BLOCK - 1) / BITS_PER_BLOCK;
    

    I find the former version more readable, but as the latter is also quite common – it’s a special case of a more general rounding algorithm using integer arithmetics – a C programmer should be comfortable with either choice.

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

Sidebar

Related Questions

Okay at first I thought this would be pretty straightforward. But I can't think
Not sure if you can do this, but I want to select the first
I can't possibly be the first person to do this, it seems like it
In code-behind I can do this to select something: // Select item in first
Sorry, this is my first time using this forum. Apparently people can edit my
How can I search first in model (i know how to do this). And
I can't figure this one out. At first let me say that my cache
How can I make this second struct work, when the first struct has a
I can only assume this is a bug. The first assert passes while the
Can anyone explain why this code with the given routes returns the first route?

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.