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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:36:06+00:00 2026-06-12T12:36:06+00:00

I have difficulty understanding this piece of code : Particularly this part: // check

  • 0

I have difficulty understanding this piece of code:

Particularly this part:

     // check that the stuff we wrote has not changed
     if(n[k][0]!=(unsigned char)(n[k]+s[k]+k))
        printf("Error when checking first byte!\n");
     if(s[k]>1 && n[k][s[k]-1]!=(unsigned char)(n[k]-s[k]-k))
        printf("Error when checking last byte!\n");

The whole program tries to mimic the Windows’ malloc and free function.
It must be run on Windows.

Anyone can explain how those 2 ifs work?

Thanks.

  • 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-12T12:36:07+00:00Added an answer on June 12, 2026 at 12:36 pm

    The code makes more sense with a bit more context.

    // used to store pointers to allocated memory
    unsigned char *n[NO_OF_POINTERS]; 
    
    int s[5000]; // used to store sizes when testing
    
    ....
    
    for(i=0;i<NO_OF_ITERATIONS;i++) {
       k=myrand()%NO_OF_POINTERS; // pick a pointer
       if(n[k]) { // if it was allocated then free it
          // check that the stuff we wrote has not changed
          if(n[k][0]!=(unsigned char)(n[k]+s[k]+k))
             printf("Error when checking first byte!\n");
          if(s[k]>1 && n[k][s[k]-1]!=(unsigned char)(n[k]-s[k]-k))
             printf("Error when checking last byte!\n");
          FREE(n[k]);
       }
       size=randomsize(); // pick a random size
                 size=1;
       n[k]=(unsigned char *)MALLOC(size); // do the allocation
       s[k]=size; // remember the size
       n[k][0]=(unsigned char)(n[k]+s[k]+k);  // put some data in the first and
       if(size>1) n[k][size-1]=(unsigned char)(n[k]-s[k]-k); // last byte
    }
    

    The last two lines set the first and the last byte to values using a formula based on the pointer value (n[k]), the size of the allocation (s[k]), and the pointer index (k). There is no meaning to this formula, it’s just a calculation of a value to be stored that will be different for different pointer allocations.

    The if statements you highlighted check that the values of the first (n[k][0]) and last (n[k][s[k]-1]) bytes haven’t changed before freeing the memory. The code is basically a test harness for the VirtualAlloc and VirtualFree functions.

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

Sidebar

Related Questions

I'm not a C++ programmer and have difficulty understanding the explanations given on websites.
I have encounters this abap snippet that I have difficulty to comprehend. call function
I have some jQuery code that is having difficulty running because I'm using it
I am having difficulty in understanding IPC in multiprocess system. I have this system
I'm having some difficulty understanding why the output of the following code is not
i have a little difficulty in understanding how to do some INSERT SELECT. For
I'm having a little difficulty understanding alias_method / alias_method_chain . I have the following
I am told that the configuration below is possible, but have had significant difficulty
I have difficulty in understanding the use of union in C. I have read
I have difficulty understanding how a javascript callback should work. I am using the

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.