AceTheInterview
Jobs in Pune | Work better in teams | Socialize with friends | Submit Q&A | Tell a friend
Search site for 

Top 100 Interview Questions & Answers in a convenient and easy to read book!

“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!”

– Ravi, California

Read more comments...

Interview Questions And Answers RSS Feed

Answers »

  1. Submitted By: jinaljhaveri — October 6, 2006
    +21 votes
      + -

    The memory is not allocated…before assigning it a value

  2. Submitted By: monkeyboy — October 6, 2006
    -20 votes
      + -

    jinal,….
    I dont think so…. the snippet should run fine….

  3. Submitted By: Glottis — October 6, 2006
    +16 votes
      + -

    Geniuses… ‘ptr’ is an uninitialized pointer. Results are unpredictable but will most likely result in an invalid memory access (write).

  4. Submitted By: js_sandiego — October 6, 2006
    -16 votes
      + -

    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.

  5. Submitted By: 1mahesh — October 6, 2006
    -18 votes
      + -

    js is right.
    It should work fine.

  6. Submitted By: Dhiraj Nilange — October 6, 2006
    +51 votes
      + -

    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

  7. Submitted By: vira — October 6, 2006
    -12 votes
      + -

    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.

  8. Submitted By: manoj_rn — October 6, 2006
    +2 votes
      + -

    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.

  9. Submitted By: saurabh — October 6, 2006
    -2 votes
      + -

    Well….Apparently Dhiraj is always right….. :)

    good job Dhiraj (Dhiraj means patience)

  10. Submitted By: Brahmaji B S — October 6, 2006
    -1 votes
      + -

    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.

  11. Submitted By: yahon — October 6, 2006
    -7 votes
      + -

    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.

  12. Submitted By: Venu — October 6, 2006
    +1 votes
      + -

    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.

  13. Submitted By: Sridhar — October 10, 2006
    +1 votes
      + -

    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 !!

  14. Submitted By: MC — October 22, 2006
    -2 votes
      + -

    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;
    }

  15. Submitted By: raushan — December 13, 2006
    +2 votes
      + -

    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.

  16. Submitted By: aman mittal — February 3, 2007
    +1 votes
      + -

    it would compile , when address would given to variable.

    *ptr=&10;

  17. Submitted By: vikram bhat — August 12, 2007
    +1 votes
      + -

    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.

  18. Submitted By: sunil k r — October 18, 2007
    -1 votes
      + -

    the program ll run there is no prob with the codeing.
    if u r quistion is different then u have to give detail

  19. Submitted By: KUNAL — October 31, 2007
    -1 votes
      + -

    the code is perfectly fine…
    it will be compiled as well as it will smoothly run without giving any errors or warnings..

  20. Submitted By: someone — December 16, 2007
    not yet rated
      + -

    main()
    {
    int *ptr;
    *ptr=10;
    }

    int *ptr is variable while 10 is const . hence it will compile bu not run.

  21. Submitted By: RAJESH JNU — January 8, 2008
    not yet rated
      + -

    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.

  22. Submitted By: Varatharajan — January 28, 2008
    -1 votes
      + -

    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

  23. Submitted By: Anjali — February 17, 2008
    not yet rated
      + -

    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

  24. Submitted By: hmm — January 12, 2009
    not yet rated
      + -

    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.

  25. Submitted By: Rohit Kumar — February 28, 2009
    not yet rated
      + -

    it works fine just give it a check!!

  26. Submitted By: Dhiraj Nilange — April 21, 2009
    not yet rated
      + -

    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.

  27. Leave an Answer/Comment

    To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
    Click to hear an audio file of the anti-spam word

Our Sponsors
Our Sponsors
Contact Us | FAQ | Sitemap | Terms of Use | Privacy Policy | Tell a Friend

Copyright © 1999-2006 Jeeve Technologies LLC. All rights reserved.