Knowee
Questions
Features
Study Tools

Multi Choice Type QuestionWhich of the following scenarios can lead to a dangling pointer?Marks : 1Negative Marks : 0Answer hereReturning a pointer to a local variable from a functionUsing malloc() for memory allocationAssigning a pointer to another pointerDeclaring a pointer inside main()

Question

Multi Choice Type QuestionWhich of the following scenarios can lead to a dangling pointer?Marks : 1Negative Marks : 0Answer hereReturning a pointer to a local variable from a functionUsing malloc() for memory allocationAssigning a pointer to another pointerDeclaring a pointer inside main()

🧐 Not the exact question you are looking for?Go ask a question

Solution

The correct answer is "Returning a pointer to a local variable from a function".

Here's why:

  1. "Returning a pointer to a local variable from a function": This can lead to a dangling pointer because local variables are stored on the stack, and they get destroyed once the function call is over. So, if you return a pointer to such a local variable, the pointer will point to a location that is no longer valid, hence it becomes a dangling pointer.

  2. "Using malloc() for memory allocation": This does not lead to a dangling pointer. malloc() is used to dynamically allocate memory in the heap, and the memory remains valid until it's explicitly deallocated using free(). So, unless the memory is freed and the pointer is still used, it won't become a dangling pointer.

  3. "Assigning a pointer to another pointer": This also does not lead to a dangling pointer. When you assign a pointer to another pointer, both pointers will point to the same memory location. Unless the memory is deallocated while one of the pointers is still in use, neither of the pointers will become dangling.

  4. "Declaring a pointer inside main()": This does not lead to a dangling pointer. The pointer will be valid as long as the main() function is running. It will only become invalid when the program ends, at which point all pointers become invalid anyway.

This problem has been solved

Similar Questions

Which of the following is not a consequence of using a dangling pointer?Marks : 1Negative Marks : 0Answer hereMemory leakSystem crashSegmentation faultNone of the mentioned options

In which case does a pointer become a dangling pointer?Marks : 1Negative Marks : 0Answer hereWhen it is declaredWhen it is used to allocate memoryWhen the memory it points to is reallocatedWhen the memory it points to is deallocatedClearNext

Multi Choice Type QuestionWhat type of error is typically associated with dangling pointers?Marks : 1Negative Marks : 0Answer hereCompile Time ErrorRuntime errorLogical errorSyntax error

What is the result of dereferencing a dangling pointer?Marks : 1Negative Marks : 0Answer hereThe program crashes.The pointer is set to NULL.The value at the memory location it points to.A compilation error.

Multi Choice Type QuestionWhat does malloc function do in C?Marks : 1Negative Marks : 0Answer hereAllocates memory and initializes it to zeroAllocates memory without initializing itFrees allocated memoryReallocates memory

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.