Why can't I use add pointer instead of malloc

I was wondering why I should use malloc to dynamically create memories, while I already have a pointer to manipulate memories freely. So I created this code, but this code corrupts by saying "the stack around the variable" a "has been corrupted"

Can someone please explain why this code is not working?

#include <stdio.h>

int main(void)
{
    int a = 1;
    int * arr1 = &a;
    *(arr1 + 1) = 2;
    printf("%d %d\n", *arr1, *(arr1+1));
}

      

+3


source to share


4 answers


Just because you can do pointer arithmetic at some arbitrary address doesn't mean you should. C gives you a lot of flexibility to do what you want, but that also means it trusts you to do the right thing, which it doesn't.

What you are trying to do is write to the memory address sizeof(int)

bytes by address a

. This memory address is not part a

, and writing to it causes undefined behavior .

As far as what's going on in your particular case, local variables are usually stored on the stack in most hosted implementations. Therefore, if you write out of bounds of a variable, you will most likely rewrite the adjacent variable or return address for that function. Given that your program is crashing, you are probably doing the latter.

Here's an illustration of overwriting other variables:

#include <stdio.h>

int main(void)
{
    int before = 0;
    int a = 1;
    int after = 0;
    int * arr1 = &a;
    *(arr1 + 1) = 2;
    printf("%d %d\n", *arr1, *(arr1+1));
    printf("before=%d, after=%d\n", before, after);
}

      



In this example, I took your example and added a before and after variable a

. When I run this, I get the following output:

1 2
before=2, after=0

      

You can see that it is before

now set to 2, although it is not explicitly set. This means that it appears on the stack right after a

. So by writing one past a

, you end up writing “26”.

To reiterate, this behavior is undefined. The compiler can arrange variables on the stack in any order. If you, for example, added an extra call printf

or compiled with different optimization settings, you might get different results. I recompiled the above code with -O1

and running it resulted in a kernel dump.

So if you need a certain amount of memory and you don't have a place explicitly allocated for it, you need to make a call malloc

to get the memory you want. Then you can read and write this block freely.

+4


source


You don't know what an implementation can store in &a + 1

. This can be important information needed to maintain a sane operating environment. Although you don't know what it can be used for, you modify it. So anything can happen.



Don't write to memory that doesn't belong to you!

+5


source


You are using int * arr1 = &a;

to request a pointer that points to. And arr1

assigned by the system. This way the method will be safe to request a pointer.

And for your other case use *(arr1 + 1) = 2

to ask for a pointer (arr1 + 1)

to specify an integer value. You might not know if it is being deployed by (arr1 + 1)

other programs or other use. This is not a safe memory address. Thus, the reason why the program will be damaged.

0


source


You have assigned an address to a variable to a a

pointer arr1

. arr1 is declared as an integer pointer arr1 + 1 increments the address by 4 (sizeof (int)), i.e. if arr1 is 0x00000003 arr1 + 1 is 0x00000007

*(arr1 + 1) = 2;

      

becomes invalid as its extra memory is allocated for a, writing invalid memory, which causes your program to crash

0


source







All Articles