Null pointers | SoloLearn: Learn to code for FREE!


Null pointers

If we were to use the null pointer to pre allocate memory for variables, how would we know we are getting enough space for whatever you need it for? Would additional memory from the heap automatically be given to accommodate or are we required to provide additional memory space some way?

4/12/2017 8:55:24 PM

Daniel Corum

5 Answers

New Answer


whenever the variable is holding an address (pointer) to some memory the space needed for it stays the same no matter how big the object may be. char * p; // this variable needs as much space // as an memory - adress needs. initializing it with "null" is good practice. an example: your phone has saved numbers. the size of the numbers is independent from the size of the objects they refer to. when you just have a name (and not an adress) you could save the value "0"


so any pointed variable has the same amount of space allocated to it. What about the value of each pointed variable data types? Will an image have enough memory the same as a video would or would the pointed variable point towards a location on a hard drive that is physically storing it. This also being slightly different if the pointed variable was pointing towards something in the memory correct?


Good question Daniel. The response is not so easy.Firstly,the dynamic allocated memory was designed for sharing or reusing the same piece of memory between application modules or parts and frees it again if the app does not need it at the moment. Second,you need to have an imagination about purpose or for what you want to use the memory (Buffer,canvas,structures,handlers).The approach is the same as a static allocation.The basic principl of heap using does not support automatic resizing of memory.Each allocation call returns part of memory which is not integrated together (it is not entire space).If you need to have dynamicly resized spce you have to use extra functions or structures like linked list i.e. from standard template library.


whenever you store bigger data, the amount of memory is so big that it goes over several adresses. in the pointer variable you store only the beginning point of the data. in c-style string as char.-array for example you know that the end is reached when you find the char '\0'. some document formats also use some "end-of-file" signal.


thanks guys, I appreciate the answers. I have a lot to learn lol I'm doing all this on my own before school.