im getting undesired output ==> im getting default as output
pls explain why im not getting ‘choice 3’ as output
link --> https://ide.codingblocks.com/s/160686
moreover pls explain about memory leaks and why everyone tells to use delete after creating a node?
Switch case giving undesired output and pls explain about memory leaks
Hey there @kul_boii
Let me explain to you the concept of how switch works,
Considering your code:
#include<stdio.h>
int main()
{
int choice=3;
switch(choice)
{
case 1:
{
printf(“choice 1”);
break;
}
case 2:
{
printf(“choice 2”);
break;
}
default :
{
printf(“default”);
break;
}
}
return 0;
}
Here the switch takes parameter as the choice variable, there are three cases
the first case executes if the value of the switch variable i.e. choice is 1, the second case executes when the value of choice is 2, the default case executes when value of switch is something other than 1 or 2, here the value of choice is 3, so the default case will execute.
Also regarding memory leak, Memory leak occurs when programmers create a memory in heap and forget to delete it.Basically If a program is buggy and allocates dynamic memory but does not free them, eventually there will be no more memory available. To avoid memory leaks, memory allocated on heap should always be freed when no longer needed. That’s why we should delete the list/memory after using
what if forget to use delete ?
like I created a node*n=new node(); //allocates a memory node type
and if i forget to use delete? then the memory i created will still persist? even if i have closed the code and its execution?
new creates memory on the heap, and if you don’t delete it, it still persists after the execution of the code.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.