{"id":5241,"date":"2020-08-22T21:05:48","date_gmt":"2020-08-22T19:05:48","guid":{"rendered":"https:\/\/deepbluembedded.com\/?p=5241"},"modified":"2023-08-17T23:51:18","modified_gmt":"2023-08-17T20:51:18","slug":"pointers-embedded-c","status":"publish","type":"post","link":"https:\/\/deepbluembedded.com\/pointers-embedded-c\/","title":{"rendered":"Pointers In Embedded C – Null Void Wild Dangling Pointers"},"content":{"rendered":"\n\n\n\n
\n

What Are Null, Void, Dangling, and Wild <\/strong><\/h2>\n

Pointers in Embedded C?<\/strong><\/h2>\n<\/td>\n<\/tr>\n

\u2302 Back To Embedded Systems Interview Q&A Page<\/strong><\/a><\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"Pointers<\/p>\n

What’s A Void Pointer in C?<\/strong><\/h3>\n

A Void Pointer<\/strong><\/span> is a pointer that has no associated data type with it. A void pointer can hold the address of any type and can be type-casted to any type. This means you can use it to point to any variable type you want as shown in the code snippet below.<\/span><\/p>\n

int x = 10;\r\nchar y = 'B';\r\n\r\nvoid* ptr = &x;\u00a0\/\/ void pointer holds address of int 'x' variable\r\nptr = &y;       \/\/ void pointer holds address of char 'y' variable<\/pre>\n

Dynamic memory allocation c functions like malloc and calloc return (void *) type and this allows these functions to be used to allocate memory of any data type, as you can type-cast it to fit your variable data type.<\/span><\/p>\n

Void pointers in C are used to implement generic functions. And note that void pointers cannot be dereferenced. And C standards don\u2019t allow pointer arithmetic with void pointers.<\/span><\/p>\n


\n

What’s A Null Pointer in C?<\/strong><\/h3>\n

A Null Pointer<\/strong><\/span> is a pointer that’s declared and initialized to be Null which means it doesn’t point to any meaningful memory location. So you must not dereference a null pointer at any time in code. Actually, we usually check if a pointer is null or not to perform some kind of error detection and handling. If a pointer is correctly assigned, it shouldn’t contain null, that’s all.<\/span><\/p>\n

int* ptr = NULL;<\/pre>\n

ptr is a Null Pointer that points to nothing and you can’t dereference it like this ( x = *ptr; ). However, if assigned to any address, the pointer will no longer by null and becomes usable pointer. As shown below.<\/span><\/p>\n

int* ptr = NULL;\r\nint x = 10;\r\nptr = &x;\r\nprintf(\"%d\", *ptr); \/\/ This prints 10<\/pre>\n

\u00a0Note that: A null pointer is a value, while void pointer is a type.<\/span><\/p>\n


\n

What’s A Dangling Pointer in Embedded C?<\/strong><\/h3>\n

A Dangling Pointer<\/strong><\/span> is a pointer that points to a memory location that has been already free-ed by the application and no longer in use. Therefore, any attempt to dereference a dangling pointer will potentially lead to a runtime error.<\/span><\/p>\n

Some of the situations that lead to having dangling pointers in the system are the following. A memory location free after being in use for a while will definitely result in a dangling pointer that must not be dereferenced anytime afterward. Also making a pointer that points to a non-static local variable inside a function. Which will be a dangling pointer at the end of the function when the local variables get deleted from the stack. Pointers to static local variables in functions, on the other hand, won’t lead to having dangling pointers.<\/span><\/p>\n