1

While studying pointers I found that if I allocate memory for a 2 digit int I can give that int a higher value. Can someone explain me why it works like that and how to work around it?

Here is the code:

int *p;

p = (int *) malloc(2*sizeof(int));
p = 123456;
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
zoNNN
  • 21
  • 5

4 Answers4

4

First of all, Please see this discussion on why not to cast the return value of malloc() and family in C..

That said, here, you're just overwriting the pointer returned by malloc(). Don't do that. It will cause memory leak. Also, if you try to dereference this pointer later, you may face undefined behavior, as there is no guarantee that this pointer points to a valid memory location. Accessing invalid memory leads to UB.

Finally, to address

I allocate memory for a 2 digit int [...]

let me tell you, you are allocating memory to hold two integers, not a two digit integer. Also, to store the integer values into the memory area pointed by the pointer, you need to dereference the pointer, like *p and store (assign) the value there, like *p=12345;.

malloc() allocates memory of the size of bytes passed as it's argument. Check the sizeof(int) (and 2*sizeof(int), if you want) to make it more clear.

Regarding this, quoting C11, chapter

void *malloc(size_t size);

The malloc function allocates space for an object whose size is specified by size [...]

So, here, malloc() returns a pointer with the size of two integers (i.e., capable of holding two integer values), not two digits or bytes.

Also, it's always a good practice to check the success of malloc() before using the returned pointer to avoid the possible UB by dereferencing NULL in case of malloc() failure.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    thanks, after reading your answer i have a better understanding of how it works – zoNNN Sep 05 '16 at 20:51
  • @zoNNN You're welcome. You can also [consider accepting any answer that helped you](http://meta.stackexchange.com/q/5234/244062). – Sourav Ghosh Sep 05 '16 at 20:57
  • OK, now may I ask for the reason behind the downvote? – Sourav Ghosh Sep 05 '16 at 21:03
  • if i have : char *n; n = (char *) malloc(21*sizeof(char));*n = "something"; will this work? – zoNNN Sep 05 '16 at 21:19
  • @zoNNN 1) you did not read the very first line of my answer....... 2) `"something"` is a string literal, which gives a pointer (`char *`) to it's first element while used in RHS of assignment, so in that case, you neither need to allocate memory using malloc, nor dereference the pointer. – Sourav Ghosh Sep 05 '16 at 21:22
  • 1
    @zoNNN for a simple example, `int *p; p=malloc(2*sizeof(int)); if (p) { *p = 123456};` is a valid code snippet. – Sourav Ghosh Sep 05 '16 at 21:25
  • how im a overwriting the pointer returned by malloc()? with (char *) before? – zoNNN Sep 05 '16 at 21:26
  • @zoNNN nopes, by _assigning_ to the pointer. Consider a scalar variable, and let';s do `int x; x = 10; x = 20;`, so, aren't you overwriting the `10` there? Is it clear now? – Sourav Ghosh Sep 05 '16 at 21:37
  • @zoNNN When you write `p = 123456;` you are overwriting the pointer `p` itself. If you wrote `*p = 123456;` instead, then you would be overwriting `*p`, which means "the memory that `p` points to". – Jesin Sep 05 '16 at 22:46
2

if i allocate memory for a 2 digit int i can give that int a higher value.

No, you cannot. You have allocated memory for two ints, with full int range. Standard guarantees at least five digits (215-1), but on most modern systems you are safe with eight digits (231-1).

Back to your question, there is no way in C to ensure that your program does not write past the end of the memory that it allocates. It is the responsibility of your program to not do that, including any checks it must perform in order to catch potentially unsafe access.

This is called undefined behavior. The system may crash when you access memory past the end of the allocated area, but it does not have to.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

allocate memory for a 2 digit int

malloc(2*sizeof(int)) allocates memory for two integers, not for a single two-digit integer.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
-1
  1. you do not need the cast in the first place

  2. You are allocated memory for two integers - typically 32 bits each

  3. You are giving a pointer the value 123456

Perhaps reread the book

Ed Heal
  • 59,252
  • 17
  • 87
  • 127