“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
Looks fine to me, most compilers should be happy with it. The memory for ptr is implicitly allocated, and will be deallocated once out of scope (by garbage collector). Trick question.
Actual reality is like this. When the pointer is declared it contains some garbage value as u know. Conceptually speaking it points to location indicated by that “garbage memory address”. Now when we assign some value to *a ; i.e. *a=12; we are trying to place 12 in the memory cell number indicated by ‘a’ ; which in this case is any random memory address because it is garbage. So the program will be compiled fine. But when run ; if the garbage value assigned to ‘a’ is invalid (or restricted by OS) memory address ; the OS will generate error. So its upto OS.
Dhiraj is right. The program tries to write into some garbage location. The result will be unpredictable.
It might not cause any problems in the case of such a small program, but if such code is part of a larger program running on a system with limited memory, u will almost always get a run-time error.
We are trying to place the value 10 in no location at all. Where is the space reserved for it, in the first place? Even if u r successful in placing it, u will be able to access it, only if it is a legal address. But u run the risk of corrupting another memory location.
This shud work:
Either p shud be malloc()’ed or calloc() or realloc()ed .
Or p shud be made to point to an existing variable.
What is all of this twaddle???? programming is an engineering discipline, there is a correct answer that is not arrived at by guess work.
the question main() { int *ptr; *ptr=10; }
answer 1) incorrect and will not compile, lets rewrite as:
int main(int argc, char *argv[]) { int *ptr; *ptr = 10;
}
2) anyone with even a passing familiarity with C can plainly see that ptr is uninitialized and as such attempting to write to the memory location pointed to by ptr is undefined.
This program compiles fine. But, has unpredictable behaviour at run time (that’s what Dhiraj is telling). If it runs once does not mean it will run again. The behaviour is unpredictable.
main must return a value and ptr need to be defined and deleted before the end of the program(that’s the full right answer)
int main(){
int* ptr;
ptr = new int(10);
delete ptr;
return 0;
}
this program file. but just try printing the value you have assigned . it will lead to a segmentation fault. coz the memory is not allocated yet and we are trying to put some value to that.
use malloc to allocate memory before initializing with any any value.
the prigram will give a warning and will compile
but will not run.
ptr is a pointer that must point to some valid memory allocated for storing integer values.
that memory allocation part is missing in program.
so it will fail when you assign value to invalid memory.
There will be no error shown by the compiler but there will be a warning, the program will run but only thing is it will write or assign 10 to any garbage memory location
Why are we mentioning garbage collection? C++ has no such thing. I agree with the other well-scored answers. Memory has never been allocated for the integer. This will lead to disastrous consequences, i.e. kernel panic or BSOD.
It’s nostalgic to see my answer posted 3 years back.
Considering the high rating and response that I got
for the answer I am explaining the answer in little
more depth here.
1) Whether the program is compiled or not depends
on a particular compiler. ‘C’ programming language
is a standard (ANSI C). It is the job of a particular
compiler to implement that standard. The language
standard doesn’t say how compiler should implement
a particular language. It is the choice of compiler
maker. Because of this there are many undocumented
features in programming. So in this case, my earlier
explanation still holds. I see that some people here
have been able to run the code whereas some people
have got compilation issues (errors/warnings
whatever). This will happen because it is the choice of
the compiler maker to implement the situation in the
way it finds appropriate. The language standard has no say in that. So if such question is asked in tests/interviews you should simply say that the behaviour will be unpredictable. Interviewers usually have no particular compiler in mind. If someone asks
about compilation also then the answer is that it is
highly vendor (compiler maker) specific.
2) Secondly if we assume that the compiler has successfully compiled the code then still there is no
guarantee that the code will be ‘executed’. Remember , compiler’s job is finished when it has produced executable code from the source code. But an execution is OS’ (Operating System) responsibility. So there will be an executable (machine understandable) code ready with an instruction somewhere in it causing the situation that a number 10 be inserted in some memory address (ptr) which will be probably decided at the time when OS loads the executable code into memory for execution. This is where the grey area starts. It’s upto particular OS , and even
for a particular OS it may not be standard behavior for each execution. Every time you execute the code ptr
may end up assuming different memory address. And if the memory address (we call it garbage because we have no control over what value it will assume) happens to be some memory cell which is restricted by
OS then the code may crash or some other problem may happen. In this situation we can not guarantee anything. This is what we call unpredictable behavior.
In fact hardware (CPU) facilitates OS in restricting certain memory area so that it can be used by OS itself (OS needs memory to reside in) and other important drivers etc. And that area of memory is
too critical to be used by application programs.
So CPU provides facility to OS to restrict whatever
area of memory it wants. Rest of the memory is free
for application programs to use.
So you see, there are so many C/C++ compilers and
few Operating Systems if not many. This is what we
call a ‘platform’ , means a particular combination.
Even hardware is part of this platform.
Even compiler and OS versions are
important to some extent. That’s why Java is a
popular choice for companies. It’s a platform in
itself. Because , strictly speaking, Java language
is not converted to machine executable code. It
is converted into the code (called bytecode) that
some other program provided by Sun (called JVM
Java Virtual Machine). And on all the platforms
JVM will be made available by Sun. So the actual
execution of a Java is standard across any platform.
Well I think I have gone far beyond the actual question, so I better stop it here. Hope you find
it useful.
The memory is not allocated…before assigning it a value
jinal,….
I dont think so…. the snippet should run fine….
Geniuses… ‘ptr’ is an uninitialized pointer. Results are unpredictable but will most likely result in an invalid memory access (write).
Looks fine to me, most compilers should be happy with it. The memory for ptr is implicitly allocated, and will be deallocated once out of scope (by garbage collector). Trick question.
js is right.
It should work fine.
Actual reality is like this. When the pointer is declared it contains some garbage value as u know. Conceptually speaking it points to location indicated by that “garbage memory address”. Now when we assign some value to *a ; i.e.
*a=12;
we are trying to place 12 in the memory cell number indicated by ‘a’ ; which in this case is any random memory address because it is garbage. So the program will be compiled fine. But when run ; if the garbage value assigned to ‘a’ is invalid (or restricted by OS) memory address ; the OS will generate error. So its upto OS.
But technically program is fine.
-Dhiraj
The program is fine I run it.
About the garbage memory being restricted. I think the compiler will take care of it and the program should run fine in any case.
Dhiraj is right. The program tries to write into some garbage location. The result will be unpredictable.
It might not cause any problems in the case of such a small program, but if such code is part of a larger program running on a system with limited memory, u will almost always get a run-time error.
Well….Apparently Dhiraj is always right…..
good job Dhiraj (Dhiraj means patience)
We are trying to place the value 10 in no location at all. Where is the space reserved for it, in the first place? Even if u r successful in placing it, u will be able to access it, only if it is a legal address. But u run the risk of corrupting another memory location.
This shud work:
Either p shud be malloc()’ed or calloc() or realloc()ed .
Or p shud be made to point to an existing variable.
Gentlemen….
What is all of this twaddle????
programming is an engineering discipline, there is a correct answer that is not arrived at by guess work.
the question
main()
{
int *ptr;
*ptr=10;
}
answer
1) incorrect and will not compile, lets rewrite as:
int main(int argc, char *argv[])
{
int *ptr;
*ptr = 10;
}
2) anyone with even a passing familiarity with C can plainly see that ptr is uninitialized and as such attempting to write to the memory location pointed to by ptr is undefined.
This program compiles fine. But, has unpredictable behaviour at run time (that’s what Dhiraj is telling). If it runs once does not mean it will run again. The behaviour is unpredictable.
Problem is some compilers may not allocate memory properly for pointers.
It is best to use it like this:
int *ptr;
ptr = (int *)malloc(sizeof(int));
*ptr = 10;
It will work perfectly with any compiler. Challenge !!
main must return a value and ptr need to be defined and deleted before the end of the program(that’s the full right answer)
int main(){
int* ptr;
ptr = new int(10);
delete ptr;
return 0;
}
this program file. but just try printing the value you have assigned . it will lead to a segmentation fault. coz the memory is not allocated yet and we are trying to put some value to that.
use malloc to allocate memory before initializing with any any value.
it would compile , when address would given to variable.
*ptr=&10;
the prigram will give a warning and will compile
but will not run.
ptr is a pointer that must point to some valid memory allocated for storing integer values.
that memory allocation part is missing in program.
so it will fail when you assign value to invalid memory.
the program ll run there is no prob with the codeing.
if u r quistion is different then u have to give detail
the code is perfectly fine…
it will be compiled as well as it will smoothly run without giving any errors or warnings..
main()
{
int *ptr;
*ptr=10;
}
int *ptr is variable while 10 is const . hence it will compile bu not run.
this will give the error
Segmentation fault at gcc compiler
becoz we r accessing the illegal memory means the memory we do not have allocated. we r assiging it a value.
this is not working.
ptr is the pointer variable. so it holds only address of the variable or so. we can’t asign values directly to the pointer.
Here we assign value. this is wrong
There will be no error shown by the compiler but there will be a warning, the program will run but only thing is it will write or assign 10 to any garbage memory location
Why are we mentioning garbage collection? C++ has no such thing. I agree with the other well-scored answers. Memory has never been allocated for the integer. This will lead to disastrous consequences, i.e. kernel panic or BSOD.
it works fine just give it a check!!
Hi,
It’s nostalgic to see my answer posted 3 years back.
Considering the high rating and response that I got
for the answer I am explaining the answer in little
more depth here.
1) Whether the program is compiled or not depends
on a particular compiler. ‘C’ programming language
is a standard (ANSI C). It is the job of a particular
compiler to implement that standard. The language
standard doesn’t say how compiler should implement
a particular language. It is the choice of compiler
maker. Because of this there are many undocumented
features in programming. So in this case, my earlier
explanation still holds. I see that some people here
have been able to run the code whereas some people
have got compilation issues (errors/warnings
whatever). This will happen because it is the choice of
the compiler maker to implement the situation in the
way it finds appropriate. The language standard has no say in that. So if such question is asked in tests/interviews you should simply say that the behaviour will be unpredictable. Interviewers usually have no particular compiler in mind. If someone asks
about compilation also then the answer is that it is
highly vendor (compiler maker) specific.
2) Secondly if we assume that the compiler has successfully compiled the code then still there is no
guarantee that the code will be ‘executed’. Remember , compiler’s job is finished when it has produced executable code from the source code. But an execution is OS’ (Operating System) responsibility. So there will be an executable (machine understandable) code ready with an instruction somewhere in it causing the situation that a number 10 be inserted in some memory address (ptr) which will be probably decided at the time when OS loads the executable code into memory for execution. This is where the grey area starts. It’s upto particular OS , and even
for a particular OS it may not be standard behavior for each execution. Every time you execute the code ptr
may end up assuming different memory address. And if the memory address (we call it garbage because we have no control over what value it will assume) happens to be some memory cell which is restricted by
OS then the code may crash or some other problem may happen. In this situation we can not guarantee anything. This is what we call unpredictable behavior.
In fact hardware (CPU) facilitates OS in restricting certain memory area so that it can be used by OS itself (OS needs memory to reside in) and other important drivers etc. And that area of memory is
too critical to be used by application programs.
So CPU provides facility to OS to restrict whatever
area of memory it wants. Rest of the memory is free
for application programs to use.
So you see, there are so many C/C++ compilers and
few Operating Systems if not many. This is what we
call a ‘platform’ , means a particular combination.
Even hardware is part of this platform.
Even compiler and OS versions are
important to some extent. That’s why Java is a
popular choice for companies. It’s a platform in
itself. Because , strictly speaking, Java language
is not converted to machine executable code. It
is converted into the code (called bytecode) that
some other program provided by Sun (called JVM
Java Virtual Machine). And on all the platforms
JVM will be made available by Sun. So the actual
execution of a Java is standard across any platform.
Well I think I have gone far beyond the actual question, so I better stop it here. Hope you find
it useful.
Leave an Answer/Comment