Why C compiler says *** stack smashing detected ***: terminated [duplicate] - c

This question already has answers here:
Stack smashing detected
(10 answers)
How do I determine the size of my array in C?
(24 answers)
Closed 9 months ago.
What is problem with my code, that it shows "stack smashing detected"
Problem Statement:
Given an array, we have to find the smallest element in the array.
#include<stdio.h>
int main(){
int arr[20],i,j,c,x,num;
scanf("%d",&num);
for(x=0;x<num;x++){
scanf("%d",&arr[x]);
}
for(i=0;i<sizeof(arr)-1;i++){
if(arr[i]>arr[i+1]){
c=arr[i];
arr[i]=arr[i+1];
arr[i+1]=c;
}
}
printf("%d",*(arr+0));
return 0;
}

If the user-provided value num is greater than 20, your code will write to memory off the end of the array arr. This is undefined behaviour, and likely to cause a crash.

Two problems:
(1) int arr[20] can only hold twenty values, but you let the user put in any number.
(2) sizeof(arr) gives you the size in bytes, not the number of elements.
The compiler is able to detect one or both of these problems, and give you an error message telling you.

Related

why does pointer memory command not working [duplicate]

This question already has answers here:
Printing pointer to integer causes segmentation fault. Why?
(7 answers)
What is a segmentation fault?
(17 answers)
What is the meaning of "wild pointer" in C?
(11 answers)
Closed 2 years ago.
I am trying to follow this basic program involving pointer into the memory.
At first We define counter to be 0 (outside main) then we make p_int to point at the same address as a counter.
But when i go into the loop for some reason it compares the register with 21 instead of 2.
after that when i have tried to change the adress and value of the pointer to a tottaly different vaue and address, it exits in an error,although it compiled well.
Whele did i go wrong?
Thanks.
int counter=0;
int main()
{
int *p_int;
p_int=&counter;
while (*p_int <2)
{
(*p_int)++;
}
p_int=(int*)0x20000002U;
*p_int=0xDEADBEEF;
return 0;
}
enter image description here
enter image description here
enter image description here
enter image description here

How does arrays bypass its declared length [duplicate]

This question already has answers here:
How dangerous is it to access an array out of bounds?
(12 answers)
Why doesn't my program crash when I write past the end of an array?
(9 answers)
Array index out of bound behavior
(10 answers)
No out of bounds error
(7 answers)
Closed 3 years ago.
I was making practises on the logic of arrays in c and my thought on the array length declaration was unformattable if you declare an array length to be 10 integers, that array could not keep 20 integers in memory but when I tested it I saw that I was completely wrong
int main(){
int i;
int arr[10];
for (i = 0;i<20;i++){
arr[i] = i;
}
for (i = 0;i<20;i++){
printf("%d \n",arr[i]);
}
}
I was expecting to see 10 printed numbers but it prints out 20 could someone explain how is it possible?
C and C++ don't have explicit bounds checking on array sizes. When you read/write past the end of an array, you invoke undefined behavior.
With undefined behavior, your program may crash, it may output strange results, or (as in your case) it could appear to work properly. Also, making a seemingly unrelated change such as adding an unused local variable or adding a printf for debugging can change how UB manifests itself.
Just because a program may crash doesn't mean it will.

Why can I assign an int to an array with not enough memory allocated? [duplicate]

This question already has answers here:
Why doesn't my program crash when I write past the end of an array?
(9 answers)
How dangerous is it to access an array out of bounds?
(12 answers)
Closed 5 years ago.
Here is my code:
#include<stdio.h>
int main()
{
int i, list[1];
list[0]=1;
list[1]=2;
list[2]=7;
list[55]=70;
i=sizeof list;
printf("%d %d %d %d %d Size of array is %d",list[0],list[1],list[2],list[3],list[55],i);
return(0);
}
It returns "1 2 7 4 70 Size of array is 4". Why can i assign, say 55 to list[55]. list[55] should not exist as I only gave the array list enough memory for 1 integer, right? In addition shouldn't this give me an error as list[3] doesn't exist? and if for some reason i am changing the size of the array why isn't the size 56? It comes out as 4.
So what is happening to give me the output i got?<--{main question}
[As i don't want to create a separate thread for a related question, why when i code int list[0]; the program crashes, if i am somehow changing the size from 1 to 4 shouldn't I be able to change the size from 0 to 4?]
Thanks for your help, I know this probably a stupid or obvious question.

What happens after the end of character array in c? [duplicate]

This question already has answers here:
Segmentation Fault doesn't come up immediately after accessing out-of-bound memory
(5 answers)
Closed 7 years ago.
What's stored after the end of character array? I was assuming there would be some random garbage but it did not print anything after the end while looping 10 times.
char a[] = "Pencil";
int i;
for (i = 0; i < 10; i++)
{
printf("%c", a[i]);
}
so the character array a has the size of 7. And for loop looped until 10th position which are 3 more values looped through. But it did not print anything or Error. What's going on here?
Accessing beyond the end of an array in C is undefined behavior. Your program could continue running unchanged or it could crash horrifically depending on what is stored past the end of the array. The compiler makes no guarantees about what is stored there - it could be useless memory or it could be critical to your program.
Accessing an array element beyond the array-length invokes undefined behavior which means anything could happen.

Array indexing size [duplicate]

This question already has answers here:
Why doesn't my program crash when I write past the end of an array?
(9 answers)
Closed 7 years ago.
I am getting confused with an array code.
According to me the program should raise an error but it's working fine. The code :
#include<stdio.h>
#include<conio.h>
void main()
{
int a[1],n,i;
clrscr();
printf("Enter the length");
scanf("%d",&n);
for( i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
getch();
}
Here the array size is 1 but when I enter the length 5 then it works fine : the program show all 5 elements that i have entered.
This is the output screen.
Accessing array out of bounds causes undefined behavior. Anything can happen including the outcome you are observing. In this case you are overwriting some objects stored after the array. They are just not used in this particular case and your program doesn't crash.
Such bugs are really hard to debug. It works fine now, but might start to fail, for example, when different compiler is used. Memory analyzer can help detecting such bugs. It will detect some invalid memory accesses, even if they do not cause a crash.
When you have defined int a[1], only space for one int is allocated on the stack. Any access beyond array bound causes undefined behavior. Therefore, the code is wrong according to the C standard.
In your case, the program is accessing some space beyond the array and by the chance of luck you didn't end up yourself with a segmentation fault.

Resources