View Full Version : C 2D arrays
jgroch
08-01-2008, 07:26 AM
I'm trying to learn C and I'm a little embarrassed to ask, but I've spent more time trying to find an answer than I think should be necessary without finding out why, so I'll ask.
Why does the following create a segmentation fault when run?
int main() {
int a[1000000][1000000];
a[0][0] = 0;
}
As I understand it, seg faults mean I'm trying to access areas of memory that I shouldn't. So I'm guessing I'm not initializing an array properly. Or is it something to do with dimensions being to large?
Any help would be appreciated.
deemon
08-01-2008, 07:34 AM
Yes, it's because of the large array dimensions - you're trying to allocate 3725GB of data (1000000 * 1000000 * 4 bytes) for the array a.
By the way - which compiler do you use? gcc will give an error and refuse to compile such code.
jgroch
08-01-2008, 07:48 AM
I'm really grateful. I've spent most of yesterday and today trying to figure out a bug in a practice program that boiled down to this.
I am using gcc, but it compiled ok.
deemon
08-01-2008, 08:15 AM
Ok - which version? The oldest gcc I have is 3.4.2 on FreeBSD 5.4 and it gives error on such occasions.
jgroch
08-01-2008, 08:35 AM
gcc --version says: gcc (GCC) 4.2.1 20070719 [FreeBSD]. I'm using FreeBSD release 7.0.
deemon
08-01-2008, 09:12 AM
Maybe it's because you don't have any resource limits set?
What is your limits output?
ephemera
08-01-2008, 11:12 AM
is this fbsd x64? uname -mp?
btw, allocating a lot of memory on the stack is never a good idea.
use malloc instead as it will fail gracefully at runtime if the required memory is unavailable.
BSDfan666
08-01-2008, 06:22 PM
As ephemera said, large static allocations are never a good idea.. ;)
jgroch
08-01-2008, 07:18 PM
What is your limits output?
I see a few infinities.
Resource limits (current):
cputime infinity secs
filesize infinity kB
datasize 33554432 kB
stacksize 524288 kB
coredumpsize infinity kB
memoryuse infinity kB
memorylocked infinity kB
maxprocesses 5547
openfiles 11095
sbsize infinity bytes
vmemoryuse infinity kB
is this fbsd x64? uname -mp?
It's amd64.
Thanks all. I haven't gotten into malloc and free much yet, it's actually what I wanted to look into next. And it looks like I need to.
ephemera
08-01-2008, 07:29 PM
> It's amd64.
i believe you weren't getting the array too big error from gcc because of the much larger address space on a 64 bit OS (as opposed to 4GB on a 32 bit m/c).
obviously, the array allocation will fail because you don't actually have 3725GB of RAM.
> I haven't gotten into malloc and free much yet
Checkout the man page for malloc(3):
$ man 3 malloc
TerryP
08-01-2008, 08:12 PM
When you get to malloc(), you might also take a stab at the debugger (http://users.actcom.co.il/~choo/lupg/tutorials/debugging/debugging-with-gdb.html) after that. It can be very helpful while learning and 'playing' with dynamic memory allocation.
ephemera
08-01-2008, 08:50 PM
here's a program to get you started:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int **a, row_size, col_size, i;
row_size = 1000;
col_size = 1000;
a = malloc(row_size * sizeof(int *));
if (NULL == a) {
perror("malloc");
exit(1);
}
for (i = 0; i < row_size; i++) {
a[i] = malloc(col_size * sizeof(int));
if (NULL == a[i]) {
perror("malloc");
exit(1);
}
}
return a[row_size - 1][col_size - 1] = 53;
}
Compile with the gcc compiler:
cc -Wall -g prog.c
Check if it worked:
echo $?
(note: in a real world program you will want to free(3) the pointers to avoid a memory leak.)
jgroch
08-01-2008, 11:49 PM
i believe you weren't getting the array too big error from gcc because of the much larger address space on a 64 bit OS (as opposed to 4GB on a 32 bit m/c).
That would make sense.
... take a stab at the debugger...
I started looking at it earlier this week, but the link will help.
Compile with the gcc compiler:
$ cc -Wall -g prog.c
Check if it worked:
$ echo $?
It compiled and could be run. In gdb it said "Program exited with code 065" (was the return line supposed to be "== 53" rather than "= 53", to exit normally?)
Also what did you mean with "$ echo $?"? Just to post results back, or something to do with the echo command?
The example helped though, I used a few lines in a bigger practice prog (which included free()).
BSDfan666
08-01-2008, 11:53 PM
'echo $?' prints the return value of the previously executed command.. ;)
@ephemera, the bbcode is really useful...
jgroch
08-02-2008, 12:24 AM
I see. I tried it after compiling and got 0, but wondered what it signified because I tried it after other commands and even in a new terminal window and kept getting the same thing. (The man pages didn't seem to say what the $? meant, or maybe I overlooked it.)
BSDfan666
08-02-2008, 12:54 AM
A return value of 0 is usual, it indicates success... but, a program doesn't necessarily have to stick with tradition. ;)
jgroch
08-02-2008, 02:54 AM
Ok, I understand. The program does return 53 using = and not ==.
vBulletin® v3.7.2, Copyright ©2000-2009, Jelsoft Enterprises Ltd.