This question already has answers here:
Why don't I get a segmentation fault when I write beyond the end of an array?
(4 answers)
Closed 7 years ago.
I've brushing up on my C coding and going over some old exercises I did a couple of years back. I came accros a situation I am almost 100% sure it should give a segmentation fault, but instead the program runs smoothly and terminates correctly. Why is that happening?
#include <stdio.h>
int main(void){
int vals[6] = {0,0,0,0,0,0};
vals[8]++; //This should not be ok!!?
printf("Done");
return 0;
}
The behaviour of vals[8] is undefined.
It's equivalent to *(vals + 8) which is dereferencing memory outside the bounds of the array.
A "segmentation fault" is one of many things that could happen. The compiler could also eat your cat.
Related
This question already has answers here:
How dangerous is it to access an array out of bounds?
(12 answers)
Closed 2 years ago.
I can't figure out where the problem is in my program?
#include <stdio.h>
int main()
{
int a[2][2]={{1,2},{3,4}};
printf("The value of a[2][1] is %d",a[2][1]);
return(0);
}
I expected the answer to be 3, it's actually wow! 32765 wait! what!? I'm pretty confused.
Can someone help?
You don't have anything in the spot a[2][1]. I think what you meant to put is a[1][0]. Remember that the index starts at 0 not at 1.
The reason why you are getting that big number is because that number was already sitting there in that memory location. It has nothing to do with the array you created.
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
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.
This question already has an answer here:
Segmentation Fault, large arrays
(1 answer)
Closed 4 years ago.
The following code, when compiled and run, gives me a segmentation fault. Why is this?
#include <stdio.h>
#include <limits.h>
int main(void)
{
int fat_array[INT_MAX];
return 0;
}
What you are requesting is to have about 2,147,483,647integer spaces allocated to you. Each integer is usually four bytes so that's 8,589,934,588 bytes which is 8 gigabytes of memory. This is likely above the allowed amount of memory a single process is allowed to reserve, and for good reason, so you get an error.
This question already has answers here:
Segmentation fault on large array sizes
(7 answers)
Segmentation Fault - Large Array
(1 answer)
Closed 8 years ago.
I am currently creating a large array that looks like this:
unsigned char arr[35000][500];
I then try to write in 256 characters into the array like so:
for(i=0; i < 256; i++)
{
arr[i][0] = i;
}
When I do this, I get the following seg fault:
Program received signal SIGSEGV, Segmentation fault.
0x00000000004007e3 in main () at arr.c:41
41 arr[i][0] = i;
Any suggestions on why this is happening?
You probably have some stack overflow happening. Consider using dynamic memory allocation