|
|||
C 2D arrays
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? Code:
int main() { int a[1000000][1000000]; a[0][0] = 0; } Any help would be appreciated. |
|
|||
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. |
|
|||
gcc --version says: gcc (GCC) 4.2.1 20070719 [FreeBSD]. I'm using FreeBSD release 7.0.
|
|
|||
As ephemera said, large static allocations are never a good idea..
|
|
|||
Quote:
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 Quote:
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. |
|
||||
> 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 Last edited by ephemera; 1st August 2008 at 06:41 PM. |
|
||||
When you get to malloc(), you might also take a stab at the debugger after that. It can be very helpful while learning and 'playing' with dynamic memory allocation.
__________________
My Journal Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''. |
|
||||
here's a program to get you started:
Code:
#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; } $ 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.) Last edited by ephemera; 2nd August 2008 at 06:52 AM. |
|
|||
Quote:
Quote:
Quote:
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()). |
|
|||
'echo $?' prints the return value of the previously executed command..
@ephemera, the [cmd][/cmd] bbcode is really useful... |
|
|||
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.)
|
|
|||
A return value of 0 is usual, it indicates success... but, a program doesn't necessarily have to stick with tradition.
|
|
|||
Ok, I understand. The program does return 53 using = and not ==.
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Basic Perl arrays question | stukov | Programming | 12 | 18th November 2008 08:44 PM |
How to vectorize a function for numpy arrays in Python | kasse | Programming | 0 | 26th August 2008 12:12 PM |
ksh arrays | mtx | Programming | 11 | 7th May 2008 10:00 AM |