Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does MISRA C state that a copy of pointers can cause a memory exception?

MISRA C 2012 directive 4.12 is "Dynamic memory allocation should not be used".

As an example, the document provides this sample of code:

char *p = (char *) malloc(10); char *q;  free(p); q = p; /* Undefined behaviour - value of p is indeterminate */ 

And the document states that:

Although the value stored in the pointer is unchanged following the call to free, it is possible, on some targets, that the memory to which it points no longer exists and the act of copying that pointer could cause a memory exception.

I'm ok with almost all the sentence but the end. As p and q are both allocated on the stack, how can the copy of the pointers cause a memory exception ?

like image 982
toto Avatar asked Nov 02 '14 21:11

toto


People also ask

Why are pointers dynamic memory allocated?

The user could enter any value he wanted for steve, which means the arr could be any size at all. Since the compiler needs to know how much space to tell the computer to set aside, this code won't work. So, how do we get around this? The answer is dynamic memory allocation, and for that, we need pointers.

Do pointers have memory?

Yes, a declared pointer has its own location in memory.

Do pointers use less memory?

In many situations pointers actually save memory. A common alternative to using pointers is to make a copy of a data structure. A full copy of a data structure will be bigger than a pointer. One example of a time critical application is a network stack.

What is pointers in memory management?

A pointer is a variable whose value is an address, typed by its declaration. Pointers "point to" a variable (memory) with a typed value by referencing that variable, not by name, but by address.


2 Answers

According to the Standard, copying the pointer q = p;, is undefined behaviour.

Reading J.2 Undefined behaviour states:

The value of a pointer to an object whose lifetime has ended is used (6.2.4).

Going to that chapter we see that:

6.2.4 Storage durations of objects

The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it. An object exists, has a constant address,33)and retains its last-stored value throughout its lifetime.34)If an object is referred to outside of its lifetime, the behavior is undefined. The value of a pointer becomes indeterminate when the object it points to (or just past) reaches the end of its lifetime.

What is indeterminate:

3.19.2 indeterminate value: either an unspecified value or a trap representation

like image 65
2501 Avatar answered Oct 05 '22 18:10

2501


Once you free an object through the pointer, all pointers to that memory become indeterminate. (Even) reading indeterminate memory is undefined behaviour (UB). Following is UB:

char *p = malloc(5); free(p); if(p == NULL) // UB: even just reading value of p as here, is UB {  } 
like image 27
Giorgi Moniava Avatar answered Oct 05 '22 19:10

Giorgi Moniava