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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:43:53+00:00 2026-05-29T09:43:53+00:00

I ve the following memory allocation done int *a; a=malloc(5*sizeof(int)); Is there a way

  • 0

I ve the following memory allocation done
int *a;
a=malloc(5*sizeof(int));
Is there a way to just free say the 5th location of this set?

ie can I do free(a+4)??

Currently this gives a segmentation fault error.

  • 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-29T09:43:54+00:00Added an answer on May 29, 2026 at 9:43 am

    No, you cannot do free(a+4) etc.

    Why? Because the C standard forbids it.
    You must pass the exact pointer returned by malloc() to free() without any pointer arithmetic.

    • http://www.kernel.org/doc/man-pages/online/pages/man3/free.3.html

      The free() function frees the memory space pointed to by ptr, which must have
      been returned by a previous call to malloc(), calloc() or realloc().

      • The Linux man pages are not the C standard itself but practically covers equivalent information on the C library functions.

    Why the standard forbids it? It is because some implementations of malloc() stores header information at the location a-4 (or wherever) and free() internally reads it by pointer arithmetic. If it’s given a shifted pointer like a+4, it will miss the header info and get screwed.
    Cf.

    • Where Do malloc() / free() Store Allocated Sizes and Addresses?

    Incidentally some libc implementations provide non-standard functions which return the size of the allocated memory chunk:

    • Is it possible to find the Memory Allocated to the Pointer, without searching for the malloc statement

    If you’re curious how they’re implemented and look into their source code, you often find that they read the header info at a-4 etc:

    • glibc malloc/malloc.c#l698
    • glibc malloc/malloc.c#l1227

    OK, back to the original question.
    If you want to shorten (or lengthen) the memory chunk you allocated with malloc(), you can use realloc() as @Gustav said. But if you want to remove a few bytes from the middle of your memory chunk, you need manual re-allocation and copying. Actually realloc() might internally do a similar work, so be careful with the returned pointer which may be different from the original one.

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

Sidebar

Related Questions

I often catch myself doing the following (in non-critical components): some_small_struct *ptr=(some_small_struct *) malloc(sizeof(some_small_struct));
this question is just speculative. I have the following implementation in C++: using namespace
This is a memory allocation issue that I've never really understood. void unleashMonkeyFish() {
Consider following codes: #include <stdio.h> #include <malloc.h> void allocateMatrix(int **m, int l, int c)
I have written an application that is leaking memory - I've done the following
I am curious of the memory allocation for the following code. NSString *myString =
I have a question regarding memory allocation order. In the following code I allocate
I've just learnt than you could throw or handle exceptions if memory allocation wasn't
Visual C++ debug runtime library features so-called allocation hooks . Works this way: you
I want to create an allocator which provides memory with the following attributes: cannot

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.