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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:46:10+00:00 2026-05-11T00:46:10+00:00

I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>

  • 0

I have this piece of code

#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>  int main(){     void *a, *b;      a = malloc(16);     b = malloc(16);     printf('\n   block size (for a): %p-%p : %li', b, a, b-a);      a = malloc(1024);     b = malloc(1024);     printf('\n   block size (for a): %p-%p : %li', b, a, b-a);   } 

Shouldn’t this print the last allocated block size (16 or 1024)? It instead prints 24 and 1032, so the amount of memory allocated seems to have 8 extra bytes.

My problem is (before making this test case) that I do malloc() in a function (1024 bytes), and return the allocated result. When checking the block size on the function return I get 516 blocks… and I don’t understand why. I guess this might be the reason for the memory corruption that occurs after doing some processing on the allocated buffers:)

Edit: I’ve seen How can I get the size of an array from a pointer in C? and seems to ask the same thing, sorry for reposting.

I’ve redone my example to my more specific code:

#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>  short int * mallocStuff(long int number, short int base){     short int *array;     int size=1024;      array=(short int*)calloc(1,size);     //array=(short int*)malloc(size);      return array; }  int main(){     short int **translatedArray;      translatedArray=malloc(4*sizeof(short int));      int i;     for(i=0;i<4;i++){         translatedArray[i]=mallocStuff(0,0);          if(i>0)             printf('\n   block size (for a): %p-%p : %i',                 translatedArray[i], translatedArray[i-1], translatedArray[i]-translatedArray[i-1]);     }      return 0; } 

And the output is

   block size (for a): 0x804a420-0x804a018 : 516    block size (for a): 0x804a828-0x804a420 : 516    block size (for a): 0x804ac30-0x804a828 : 516 

According to the above post that is bigger than 1024. Am I wrong?

  • 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. 2026-05-11T00:46:11+00:00Added an answer on May 11, 2026 at 12:46 am

    You have a bug. Instead of:

    translatedArray=malloc(4*sizeof(short int)); 

    You should have

    translatedArray=malloc(4*sizeof(short int*)); 

    Note the missing pointer in your code. I suspect this is where your observed behavior stems from.


    Also notice that 0x804a420 - 0x804a018 = 1032, not 516. The formula translatedArray[i] - translatedArray[i - 1] gives you the number of elements (short ints, or more simply, shorts) in between the two addresses, not the number of bytes.

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

Sidebar

Ask A Question

Stats

  • Questions 95k
  • Answers 95k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer See Using C++ With Objective-C for a detailed list of… May 11, 2026 at 6:58 pm
  • Editorial Team
    Editorial Team added an answer This cannot be done with the WCFTestClient. I am researching… May 11, 2026 at 6:58 pm
  • Editorial Team
    Editorial Team added an answer You can refactor the code to accept a method pointer… May 11, 2026 at 6:58 pm

Related Questions

I'm a beginner programmer and I'm learning my first language, C. I'm learning mostly
My application is suspending on a line of code that appears to have nothing
I'm having some trouble with a particular piece of code, if anyone can enlighten
I have been experimenting with woopra.com A web analytics tool. Which requires a piece

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.