Whats the problem with int *p; *p=23;
Yesterday in my interview I was asked this question. (At that time I was highly pressurized by so many abrupt questions).
int *p;
*p=23;
printf("%d",*p);
Is there any problem with this code?
I explained him that you are trying to assign value to a pointer to whom memory is not allocated.
But the way he reacted, it was like I am wrong. Although I got the job but after that he said Mohit think about this question again. I don't know what he was trying to say. Please let me know is there any problem in my answer?
EDIT I added the code on the sheet;-
int *p;
p=malloc(sizeof(int));
*p=23;
printf("%d",*p); 开发者_高级运维
This must be the perfect code...Am i right..
EDIT2
int *p;
*p=23;
OR
int *p=23;
I think both has problem. Cause some body is saying about the title of the post.
"trying to assign value to a pointer to whom memory is not allocated"
I think you just misphrased it a bit. You're not trying to assign a value to a pointer, you're trying to assign a value to the referand of a pointer.
Since the pointer is uninitialised, this is, as you say, undefined behaviour. The pointer doesn't refer to anything (at least not validly - as other answers point out, the bits of storage of p
might just so happen to contain a value which is the address of some memory location, and your code might overwrite that. The standard permits anything to happen with UB, but knowing something about your implementation you can often take a shrewd guess).
So probably in the interviewer's mind you have the right idea, but it's valuable to have it exactly straight in your mind and in your speech what the difference is between a finger and the moon, and which one you're talking about.
p
is not initialized - it stores some address. Dereferencing it is undefined behavior.
The address stored in p
may be mapped into the address space of the process or may be not mapped. If it is mapped some unrelated (but maybe important for the program) data is stored at that address. So either your program crashes immediately because of memory protection or you change some data belonging to the program. The consequences of the latter may vary - maybe nothing happens, maybe you don't notice, maybe you corrupt important data and the program breaks apart - classic undefined behavior.
'p' is pointing to an unknown location, and not to a "not allocated" memory.
The difference is that since it's undefined, it could point to an allocated memory, even if this memory wasn't meant to be accessed by this function.
int *p=23;
This is completely different. Here you're declaring a pointer, and assigning it a value of 23. Meaning it now points to the memory location 23, which may or may not contain readable data.
But simply assigning an arbitrary value to a pointer without dereferencing it is perfectly safe.
#include <stdio.h>
#include <stdlib.h>
int main(char** argv) {
// THIS POINTER IS NOT DECLARED (SO IT'S NOT USABLE/INVALID)
int *p;
// 1) SO RESERVE MEMORY AND SET POINTER TO VALID MEMORY...
p=malloc(sizeof(int));
// 2) ...CHECK IF ALLOCATION WAS OK...
if (p) {
*p=23;
printf("%d",*p);
} else printf("sorry, no memory");
// 3) ...AND FINALLY DE-ALLOCATE MEMORY (FREEING IS IGNORED WHEN p IS NULL)!
free(p);
}
int *p;
initailly the above pointer contains some garbage value( invalid adress value or un allocated address ).
*p = 23;
then your are trying to put some value (23) in the invalid memory leads to undefined behaviour.
Apart from the fact that the example doesn't allocate memory for the value 23, it also won't compile in the first place because you've got two characters in your characted constant (which is meant to be a string). Eg, replace '%d' with "%d" in the printf-statement.
精彩评论