Creating a large 2D array? [duplicate] - c

This question already has answers here:
Why int array[1000][1000] is memory issue in C program? [duplicate]
(1 answer)
Getting a stack overflow exception when declaring a large array
(8 answers)
Closed 5 years ago.
I'm using a pretty simple 2D array to store values (it's part of a mandelbrot set program).
int toBeWritten[xres][yres]; // xres and yres are calculated based on command line arguments
The 2D array works fine until my numbers get larger.
These, for example, work:
int toBeWritten[1024][1160];
int toBeWritten[2048][2321];
But when the size of the array grows to this:
int toBeWritten[4092][4637]; // the size I start getting seg faults
int toBeWritten[8192][9284]; // the largest size I want to get to
I get a seg fault if I try and access this array at any point after creating it.
Is it simply too big? Am I not allocating memory correctly?
If I can't make a 2D array this large, how could I store the values instead?
Thanks for any help!

Related

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.

finding the size of an unknown array in C [duplicate]

This question already has answers here:
How do I determine the size of my array in C?
(24 answers)
Closed 4 years ago.
We have been given a large array of unknown size with elements given , is there any function or something other through which we can find the size of that array in language C
int a[]={4,6,4,26,3,2,5,7,3,7,3,2,5,4,6,3,7,232,6,32,6,3,7,3,6,2,5,7,3,6,3,6,36,3,67,23,6};
The size in bytes you can get by
sizeof(a);
The number of elements in that array you can get by
sizeof(a)/sizeof(a[0]);

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.

Resizing a allocatable array [duplicate]

This question already has answers here:
Fortran increase dynamic array size in function
(3 answers)
Fortran array automatically growing when adding a value
(1 answer)
How to increase array size on-the-fly in Fortran?
(3 answers)
Closed 5 years ago.
I am trying to allocate an array and want to resize the array ie., grow the array size as and when required in the program.
My sample code looks like this
program main
implicit none
integer, allocatable, dimension(:)::test1
integer i, c1, c2
c1=10
c2=5
allocate(test1(1:c1))
! I basically want to do this
!allocate(test1(c1+1:c2))
end program main
How do I do this?

How to succesfully pass a 2D array to a function? [duplicate]

This question already has answers here:
Passing a 2D array to a C++ function
(18 answers)
Closed 7 years ago.
GNU GCC compiler
Here is a function: int sumsintriangle(int *a,int n)
where a is a n*n matrix .
for some purpose I added
if(*(a+(i+1)*n+(j+1)) > *(a+(i+1)*n+j))
condition to my code which was working properly ;as the condition is true for the correct values.
but for the same code when I added
sum=sum + *(a+(i+1)*n+(j+1));
then it didn't work (eg;let say sum was initially 1 and *(a+(i+1)*n+(j+1) was 4 ) then summation it should be giving me 5..but it gives me 1 as output...why??
Even ,when I called the same value *(a+(i+1)*n+(j+1)) in printf function,for just an enquiry, it is giving me 4 (original value)as output ...?
Why it is , that *(a+(i+1)*n+(j+1)) is working properly with printf but when I called it with sum it gives me incorrect value?
If you can post your function properly it could be easier to help you. but i think you have an error when you put * before your expression that will give you the content of that expression, so be sure to get the values properly.
example:
int a[]; //declare an array
a[n] // will give you the element in position 9 of the array.
*a // will give you the first element, cause an array can be treated as a pointer (indeed it is).
I hope this answer help you. If not please tell me. Good luck!
Use This code code may be its work.
*(a+(i+1))*n+(j+1)

Resources