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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:48:08+00:00 2026-05-26T10:48:08+00:00

I am relatively new to C programming and having a hard time understanding the

  • 0

I am relatively new to C programming and having a hard time understanding the whole memory allocation issue.

Let’s say, I do:

int *n = malloc(sizeof(char));
// (assuming malloc doesn't return NULL of course)

That provides a Pointer to int, but I didn’t allocate enough memory for an int. Why does it work then? I could even cast it to int explicitly and it wouldn’t bother gcc. I am aware of C compilers being very minimalist, but even if I assign a value to *n, which doesn’t fit in a char, like:

*n = 300;

… and print it out afterwards:

printf("%d", *n);

… it works perfectly fine, although now at the latest I’d expect some error like a segmentation fault.

I mean, sizeof(char) is 1 and sizeof(int) is 4 on my machine. Hence 3 bytes are written to some place in memory which hasn’t been allocated properly.

Does it work just because it doesn’t leave the stack?

Could somebody please point me to a place where I might find enlightenment concerning that stuff?

  • 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-26T10:48:08+00:00Added an answer on May 26, 2026 at 10:48 am

    That provides a Pointer to int, but I didn’t allocate enough memory for an int. Why does it work then?

    The return value from malloc is void*, the language allows this to be implicitly converted to any pointer type, in this case int*. Compilers don’t typically include behavior to check that what you passed to malloc met a specific size requirement, in real-world code that can be very difficult (when non-constant sizes not known at compile time are passed to malloc). As you said, C compiler are usually rather minimalist. There are such things as “static analysis” tools which can analyze code to try to find these bugs, but that’s a whole different class of tool than a compiler.

    … it works perfectly fine, although now at the latest I’d expect some error like a segmentation fault. I mean, sizeof(char) is 1 and sizeof(int) is 4 on my machine. Hence 3 bytes are written to some place in memory which hasn’t been allocated properly.

    Writing beyond the bounds of allocated memory is what is called “undefined behavior”. That means that a compliant compiler can do whatever it wants when that happens. Sometimes it will crash, sometimes it can write over some other variable in your program, sometimes nothing will happen, and sometimes nothing will seem to happen and your program will crash at a later date.

    In this particular case what is happening is that most implementations of malloc allocate a minimum of 16 bytes (or more or less, like 8 or 32) even if you ask for less. So when you overwrite your single allocated byte you’re writing into “extra” memory that was not used for anything. It is highly not recommended that you rely on that behavior in any real program.

    Does it work just because it doesn’t leave the stack?

    The stack has nothing to do with this particular situation.

    Could somebody please point me to a place where I might find enlightenment concerning that stuff?

    Any good C book will have information of this type, take a look here: The Definitive C Book Guide and List

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

Sidebar

Related Questions

I'm relatively new to Python and am having problems programming with Scapy, the Python
I am relatively new to Cocoa programming, and some aspects of memory managing are
long-time listener, first-time caller. I am relatively new to programming and was looking back
I am relatively new to C#, having done most of my previous programming in
I'm still a relatively new programmer, and an issue I keep having in Java
I am relatively new to programming with Qt and had a question. Short version:
I am relatively new to programming. My work basically revolves around data and analysis.
I'm relatively new to programming so excuse me if I get some terms wrong
I really need your help for this. I am relatively new to programming and
Being relatively new to functional programming, I expend lots of energy wondering is this

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.