Pointers In Embedded C – Null Void Wild Dangling Pointers

What Are Null, Void, Dangling, and Wild

Pointers in Embedded C?

⌂ Back To Embedded Systems Interview Q&A Page

Pointers in Embedded C - Wild Dangling Void Null Pointers

What’s A Void Pointer in C?

A Void Pointer 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.

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.

Void pointers in C are used to implement generic functions. And note that void pointers cannot be dereferenced. And C standards don’t allow pointer arithmetic with void pointers.


What’s A Null Pointer in C?

A Null Pointer 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.

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.

 Note that: A null pointer is a value, while void pointer is a type.


What’s A Dangling Pointer in Embedded C?

A Dangling Pointer 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.

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.




What’s A Wild Pointer in Embedded C?

A Wild Pointer is a pointer that has not been initialized to anything (not even NULL). The pointer may be initialized to a non-NULL garbage value that may not be a valid address. Therefore, dereferencing a wild pointer will potentially lead to runtime errors or logical errors that mess up the whole application.

 

⌂ Back To Embedded Systems Interview Q&A Page



Share This Page With Your Network!

Khaled Magdy

I'm an embedded systems engineer doing both Software & Hardware. I'm an EE guy who studied Computer Engineering, But I'm also passionate about Computer Science. I love reading, writing, creating projects and Technical training. A reader by day a writer by night, it's my lifestyle. You can view my profile or follow me via contacts.

You may also like...

Leave a Reply

%d bloggers like this: