Application of null pointer [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I was giving interview in a company and they asked me "What are applications of null pointers in C"? I told them what null pointer is and how it leads to segmentation fault. However, they were not happy with the answer. I have never heard anything like this before. If anyone has any idea, please share.
Thank you

they were asking when you need to use NULL pointers, not when they lead to problems.
The classic answer is that they are used when there is nothing to point to. For example the next pointer in a list when its the end of a list -> there is no next item.
and so in your code you go
if(nextItem != NULL)
{
// do stuff with nextItem
}
else
{
// the end
}
As mooseboy points out this is a sentinel value i.e, a special value that your code recognizes as having a deciding value.
NULL is the perfect pointer sentinel since NULL is assumed to never be a valid value for a pointer (for example you could not use 0x000042 since 42 might be a real address)

This question is kind of vague, but one thing they might have been unhappy about is that from a pure language perspective, dereferencing NULL is not guranteed to cause a segfault. It causes unspecified behavior, i.e. the language doesn't say what will happen. On most platforms, yes, null reference causes a segfault, but an implementation of C could do something else and still conform to the standard.

Related

Seting null value to x(in the language implementation) in free(x) [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
It is generally advised to assign NULL to a freed up pointer as otherwise it may lead to dangling pointer. I want to know if there is any case where not assigning NULL has some use?
If not, why is it not implemented in the call to 'free()' itself? I understand that the pointer is passed by value and thus cannot be modified. Could the function free() have been be implemented to accept pointer to a pointer(i.e reference to the pointer) where the pointer(not pointer to pointer) points to NULL in the end?
I am asking this since we have to always keep in mind about assigning NULL to pointer after freeing which is a very commonly called function(I am assuming).
Generally it's not for the code but us. cause free doesn't set the pointer to NULL, as if may be needed in some low level situation for some, and more generally the approach of C/C++ is to do things manually by programmer as if they may need specific requirements and this is one the being so pro of C/C++.
Anyway, by setting the pointer to NULL , now if it's happened to be used later in code now we can check if it's NULL or not, and this prevent from dangling and using unallocated memory pointers.
Setting variable to NULL after free
Why is my pointer not null after free?
https://www.quora.com/Why-should-we-assign-NULL-to-the-elements-pointer-after-freeing-them
https://www.acodersjourney.com/top-20-c-pointer-mistakes/
https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pageId=87152148

Why does C use the asterisk to reference the value of a pointer? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I find that I refer to the value at the memory location pointed to by a pointer far more often then I want to refer to the actual value of the pointer. As a result I wonder why C does not use the asterisk in the to refer to the actual value of the pointer since it is more typing.
I have already read a post on another website about how B did it this way. This does not answer my question; it just changes it to why did B do it this way.
I doubt anyone can tell you why it was designed the way it was. But a pointer is a variable that holds an address. Therefore, it's value is the address. I would find it strange and unexpected if it required special syntax to get the value of any variable.
Note that C++ lets you use references to access the value pointed to without special syntax. But you are dereferencing the pointer, so for me it makes perfect sense to require a different syntax.

why we use function returning pointer in c and application of function returning pointer [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
What is the main reason to use function returning pointer? Basically its return the pointer value but there are we can also return the value through pointer.
So why we need function returning pointer?? Is it reduce the size of code or any else?
Is there any application where we use function returning pointer?
Not sure what you are asking but yes? you could copy a 2Gb variable or an address to that variable that is orders of magnitude smaller. You tell me which is quicker/more efficient ;)
Here is a tutorial that explains their usage:
C pointers
Right off the bat it explains how it is easy to use numbers to lookup a safety deposit box so you can go access it.
without the ability to return pointers there would be no point to even having pointers at all. i.e. when you allocated memory for data, a function returns the pointer to its location. So without that you would never know were the memory you allocated is.
So I guess the functionality of returning pointers is arguably to allow the presence of pointers themselves?
One example, you can return a pointer to a value. Anyone who has that pointer can then change the value.

Is printf("%d",*(++(*a))); Undefined [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
is printf("%d",*(++(*a))); Undefined? Here a is pointer to a pointer to integer.
I do not have anymore code other than this.It is an extension of This question which had created lots of confusions.Just want to know what is happening in this print.Does it depend on Architechture(32 Vs 64) or compiler versions.
Hoping answers will be descriptive and clear.
If you break it down, it does this:
Take the value of what a is pointing at: *a
Increment by one ++(*a)
Dereference that *(++(*a))
So, if the value+1 of what is stored at a is a valid pointer, this will work. Otherwise, the result is undefined and will most likely result in a runtime error.
Yes, your code is correct and even if cryptic can make some (little) sense as in:
void print_next(int **a) {
printf("%d\n",*(++(*a)));
}
int arr[] = {1,5,6,3,5,6};
int *p = arr;
while (p<arr+6)
print_next(&p);
If your question is specifically about *(++(*a)) expression, then there's nothing undefined here (assuming all pointers involved are valid). There are no attempts to perform multiple modifications of the same object. There are no independent reads of any of the modified objects. End of story.
Basically, there's nothing to explain here, since the code is perfectly fine in a rather straightforward manner. There's really no room for anything more "descriptive and clear" than that.
If this is not sufficiently clear, you have to explain what exactly looks suspicious to you in this expression.

Why is my 2D Array code working with malloc(0)? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have weird problem: my code is working. Specifically this is working. Why?
char **array = malloc(0);
array[0] = malloc(0);
strcpy(array[0],"hello");
array[1] = malloc(0);
strcpy(array[1],"world");
What the hell is going on? When I replace any of the mallocs with NULL or remove them
it doesn't work but it doesn't seem to matter what value is inside malloc granted it's
not negative.
Dereferencing an invalid pointer is an undefined behavior; so anything can happen.
malloc(0) is implementation-defined and returns either a null pointer or an invalid pointer.
You are invoking undefined behavior by using the pointer returned by malloc(0).
Besides the undefined behaviour of malloc(0). Malloc implementations often have a minimal granularity for the allocated blocks for alignment reasons. Which means that any block will always have at least a size of 4, 8, 12, 16 bytes, depending on implementation. If I remembered correctly, you can even set this value with a mallopt call on certain platforms.
The other thing is, that buffer overflows in allocation code often only crashes when you try to free the pointer(s), because only then is the memory around your block used for internal book keeping used by the library. That's one of the reasons why buffer overflows are so difficult to catch, they manifest at different locations and times then when they occured.

Resources