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

Can someone explain this result to me. The first test succeeds but the second
First check out this code. I seems like it should work for me, but
this is my first question here so I hope I can articulate it well
problem euler #5 i found the solution but i don't know why this first
This is my first post here and I wanted to get some input from
I'm working with Doctrine2 for the first time, but I think this question is
I have some subroutines that I call like this myWrite($fileName, \@data) . myWrite() opens
First of all (in case this is important) I'm using ActiveState's Perl (v5.8.7 built
This is my first experience using the Zend Framework. I am attempting to follow
this is my first question to stackoverflow so here it goes... I use cruise

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.