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

The Archive Base Latest Questions

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

int *ptr = malloc(sizeof(char)); *ptr = 100000; printf("%d\n", *ptr); // 100000 Shouldn’t that only

  • 0
int *ptr = malloc(sizeof(char));

*ptr = 100000;

printf("%d\n", *ptr); // 100000

Shouldn’t that only allocate enough memory for a char, i.e. 1 byte? Therefore shouldn’t the largest number be 255?

How does it still print 100000?

Update

Thanks for the answers. If it overwrites the next bytes, how does C then know that this number is larger than one byte, and not just look in the first byte?

  • 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-18T00:43:53+00:00Added an answer on May 18, 2026 at 12:43 am

    Because C has no range-checking of memory. It allocates a byte, and then your assignment via the pointer overwrites it and the next three bytes. If you had allocated another bit of memory right after the first malloc, but before the assignment, you might have overwritten part of the heap (depending on how your malloc works).

    This is why pointers can be very dangerous in C.

    The %d in the format statement (plus the type of the variable) tells the compiler you are looking at an int, and accesses all four bytes.

    Note that if you really had assigned the value to a char, e.g. char *ptr; *ptr = 100000;

    then with some compilers (and assuming plain char is treated as signed but default) it would have printed out -96, not 255 (or 127). This is because the compiler doesn’t automatically limit the value to the highest value that can fit (127 in a signed char, 255 in an unsigned char), but instead it just overflows. Most compilers will complain that you are trying to assign a constant value that overflows the variable.

    The reason it is -96, is that 100000 % 256 is 160, but as a signed char it is output as -(256-160).

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

Sidebar

Related Questions

Example int *ptr; *ptr = 1000; can I catch memory access violation exception using
See this example! int main( int argc, char ** argv ) { int *ptr
int x; printf(hello %n World\n, &x); printf(%d\n, x);
I learned that memset(ptr, 0, nbytes) is really fast, but is there a faster
#include<iostream> #include<conio.h> using namespace std; int main() { int x = 65; int *ptr
Hi I'm writing some test stuff to see the amount of memory that allocated
I was using GetWindowLong like this: [DllImport(user32.dll)] private static extern IntPtr GetWindowLong(IntPtr hWnd, int
void foo(void **Pointer); int main () { int *IntPtr; foo(&((void*)IntPtr)); } Why do I
When working with window handles, is it good enough to use the plain IntPtr
when i run this code : CONADefinitions.CONAPI_FOLDER_INFO2 FolderInfo; int iResult = 0; IntPtr Buffer

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.