C programming : Stackoverflow is a cause of abrupt termination [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
In case of stack overflow in C programming, why do we tell it abrupt termination of loop. Shack-overflow is not a cause of infinite looping. But causing abrupt termination, though we cannot see, where is it terminating.
right??
Say for this program, it is causing a stack overflow but not a case of infinite looping
int foo(int val) {
int x=0;
while(val > 0) {
x = x + foo(val--);
}
return val;
}
But why we cannot say it as an infinite looping??and why is it saying as abrupt termination??

Since this is recursion, every call of the function foo() will increase the stack. And as we know the loop never ends so at some point stack will increase so much that the OS will terminate the code and give stack-overflow error.

Related

C - while(true) vs while(condition) - performance [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I would like to know if there is a difference in performance between :
while(true)
{
.....
}
And :
bool x;
x = true;
while(x)
{
.....
}
I need the best performance and a small difference between the two is important to my application.
Info from comment by OP:
The while(true) will at some point also be left, that is however rare.
If you need a truly endless loop, then why use a condition?
If you need a loop which can be left, then your while(true){...} will contain an if(!x) which your while(x) does not contain.
Any potential optimisation benefit of while(true) over while(x) will be lost at that point.
First of all...
If you want to make an infinite loop you (always) use:
while(true)
{
...
}
There would be absolutely no reason why defining a variable before that loop should speed up your "application". So there is no reason to use:
bool x = true;
while(x)
{
...
}

how to use scanf in a custom function in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Before I start please keep in mind that I am still learning and need help with some stuff that may be easy to you but not to me. So here we go. I am having trouble using scanf in a custom function. It will not let me type anything. It just keeps running forever unless I stop it. How can I get scanf to work here:
#include <stdio.h>
#include <stdlib.h>
void function ();
int main (void)
{
char sel;
function ();
return 0;
}
void function ()
{
scanf("%c",&sel);
}
There is no problem with scanf. The problem is with your loop. l = 1 and l++ will always keep l >= 0 and hence it will keep on taking input infinite times.
Also, it will keep on overriding the value of var.a with every input

Loop iteration in c [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
#include<stdio.h>
int main()
{
int i,x=10;
for(i=0;i<7;i++);
{
x++;
}
printf("%d",x);
}
Output : 11
No matter how many times the for loop iterates, the value of x stays 11. Why is that ?
Remove the semicolon from here:
for(i=0;i<7;i++);
The semicolon makes the for loop have an empty body. It makes it equivalent to
for(i=0;i<7;i++){}
Including warning flags in your compiler(-Wextra in GCC) emits a warning about these kind of issues.
Semicolon (; ) punctuation mark in C means that block of code is finished. That means if you use
for(i=0;i<7;i++);
{
x++;
}
For loop ends before it reaches brackets. Then code between your brackets runs like normal lines out of loop. If you want your loop to include brackets, get rid of the semicolon, like:
for(i=0;i<7;i++)
{
x++;
}

What are the cases in which a loop cannot be unrolled? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What are the cases in which a loop cannot be unrolled? I've been reading a paper which shows a loop that it says is not able to be unrolled. I cannot actually post the specific code as it is private, however, I am wondering if there is something obvious I am missing in regards to not being able to unroll.
Thanks in advance. If there's any other info that I can try to provide, let me know.
well you can't unroll a loop with any type of recursion in it because it could be infinitely long, also you cant unroll an infinite loop or one with some kind of method for breaking out that isn't incremental
recursion:
method(int x){
if(x > 0)
return 0;
else
return method(x-1);
infinite loop:
while(true){
...
if(some condition)
break;
}
last one:
boolean somevar = true;
while(somevar){
...
if(some condition)
somevar = false;
}

How Output of this program comes out to be this [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Its C program which is running x86_64 machine ,Wanted to know how output is coming like this way
main()
{
int *mess;
mess=malloc(1);
mess[0]=1;
//mess[1]=2;
printf("%d",mess);
}
Now output here is 6295568
How is it??
You're printing the address where your int is stored. You need
printf("%d",*mess);
to print its value.
You are also allocating too little space for your int, you should do:
int *mess = malloc(sizeof(int));
instead of
int *mess = malloc(1);

Resources