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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:22:37+00:00 2026-05-24T11:22:37+00:00

I have a block of memory with elements of fixed size, say 100 bytes,

  • 0

I have a block of memory with elements of fixed size, say 100 bytes, put into it one after another, all with the same fixed length, so memory looks like this

<element1(100 bytes)><element2(100 bytes)><element3(100 bytes)>...

In some situations I need to determine whether all bytes of a certain element are set to the 0-byte because that has a special meaning (I didn’t say it was a good idea, but that is the situation I am in).

The question is, how do I do that efficiently. Further: is there a simple function to do it. For setting bytes to zero I can used memset or bzero, but I don’t know of any function for checking for zero.

At the moment I am using a loop for the check

char *elementStart = memoryBlock + elementNr*fixedElementSize;
bool special = true;
for ( size_t curByteNr=0; curByteNr<fixedElementSize; ++curByteNr )
{
  special &= (*(elementStart+curByteNr)) == 0;
}

Of course, I could loop with a bigger offset and check several bytes at once with a mword or some other suited bigger type. And I guess that would be rather efficient, but I would like to know whether there is a function to take that burden from me.

Suggested functions:

  • !memcmp (compareBlock, myBlock, fixedElementSize)
  • 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-24T11:22:38+00:00Added an answer on May 24, 2026 at 11:22 am

    The obvious portable, high efficiency method is:

    char testblock [fixedElementSize];
    memset (testblock, 0, sizeof testblock);
    
    if (!memcmp (testblock, memoryBlock + elementNr*fixedElementSize, fixedElementSize)
       // block is all zero
    else  // a byte is non-zero
    

    The library function memcmp() in most implementations will use the largest, most efficient unit size it can for the majority of comparisons.

    For more efficiency, don’t set testblock at runtime:

    static const char testblock [100];
    

    By definition, static variables are automatically initialized to zero unless there is an initializer.

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

Sidebar

Related Questions

I have a memory block that is divided into a series of location that
Basically, I have a block of memory allocated using malloc that I want to
I have a memory block (opaque), that I want to store in a Blob
Let's say I have a vector of N elements, but up to n elements
For an assignment, I have to allocate a contiguous block of memory for a
I have a structure which contains some elements, i free the memory of this
I have a system that returns a void* to a memory block. This memory
I have a block of product images we received from a customer. Each product
I have a block of code that works and I wanted to ask what
I have a block of JSP code that needs to be used in several

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.