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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:33:10+00:00 2026-06-12T07:33:10+00:00

I want to know if it is ok to free() a pointer cast to

  • 0

I want to know if it is ok to free() a pointer cast to another type.
For instance if I do this:

char *p = malloc (sizeof (int));
int *q = (int *)p;
free (q);

I get no warning on gcc (-Wall).

On linux, the man pages on free says it is illegal to call free on a pointer that was not returned by malloc(), calloc() or realloc(). But what happens if the pointer was cast to another type in between?

I ask this because I read that the C standard does not require different pointer types (e.g. int* and char*) to have the same size, and I fail to understand how this is possible since they both need to be convertible to a void* in order to call the malloc/free functions.

Is the above code legal?

  • 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-12T07:33:11+00:00Added an answer on June 12, 2026 at 7:33 am

    It’s probably safe, but it’s not absolutely guaranteed to be safe.

    On most modern systems, all pointers (at least all object pointers) have the same representation, and converting from one pointer type to another just reinterprets the bits that make up the representation. But the C standard doesn’t guarantee this.

    char *p = malloc (sizeof (int));
    

    This gives you a char* pointer to sizeof (int) bytes of data (assuming malloc() succeeds.)

    int *q = (int *)p;
    

    This converts the char* pointer to an int* pointer. Since int is bigger than char, an int* pointer could require less information to indicate what it points to. For example, on a word-oriented machine, an int* might point just point to a word, while a char* has to contain a word pointer and an offset that indicates which byte within the word it points to. (I’ve actually worked on a system, the Cray T90, that worked like this.) So a conversion from char* to int* can actually lose information.

    free (q);
    

    Since free() takes an argument of type void*, the argument q is implicitly converted from int* to void*. There is no guarantee in the language standard that converting a char* pointer to int*, and then converting the result to void*, gives you the same result as converting a char* directly to a void*.

    On the other hand, since malloc() always returns a pointer that’s correctly aligned to point to any type, even on a system where int* and char* have different representations, it’s unlikely to cause problems in this particular case.

    So your code is practically certain to work correctly on any system you’re likely to be using, and very very likely to work correctly even on exotic systems you’ve probably never seen.

    Still, I advise writing code that you can easily demonstrate is correct, by saving the original pointer value (of type char*) and passing it to free(). If it takes several paragraphs of text to demonstrate that your code is almost certainly safe, simplifying your assumptions is likely to save you effort in the long run. If something else goes wrong in your program (trust me, something will), it’s good to have one less possible source of error to worry about.

    A bigger potential problem with your code is that you don’t check whether malloc() succeeded. You don’t do anything that would fail if it doesn’t (both the conversion and the free() call are ok with null pointers), but if you refer to the memory you allocated you could be in trouble.

    UPDATE:

    You asked whether your code is legal; you didn’t ask whether it’s the best way to do what you’re doing.

    malloc() returns a void* result, which can be implicitly converted to any pointer-to-object type by an assignment. free() takes a void* argument; any pointer-to-object type argument that you pass to it will be implicitly converted to void*. This round-trip conversion (void* to something_else* to void*) is safe. Unless you’re doing some kind of type-punning (interpreting the same chunk of data as two different types), there’s no need for any casts.

    Rather than:

    char *p = malloc (sizeof (int));
    int *q = (int *)p;
    free (q);
    

    you can just write:

    int *p = malloc(sizeof *p);
    ...
    free(p);
    

    Note the use of sizeof *p in the argument to malloc(). This gives you the size of whatever p points to without having to refer to its type explicitly. It avoids the problem of accidentally using the wrong type:

    double *oops = malloc(sizeof (int));
    

    which the compiler likely won’t warn you about.

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

Sidebar

Related Questions

I want to know how malloc and free work. int main() { unsigned char
I want to know if there is any free tool to test the performance
i want know if is possible, to get a specific element value of a
want to know why String behaves like value type while using ==. String s1
I want to know if a pointer points to a piece of memory allocated
Do you know a free good analyzer for IIS 7 Logfiles? I want to
Possible Duplicate: Why Free crashes when called twice I just want to know what
I want to know a free C/C++ library that can load and decode MIDI
I want to know if there is a free environment to develop in PL/pgSQL.
I want to know is it possible to get new permission(say INTERNET permission) which

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.