DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 1st August 2008
jgroch jgroch is offline
Port Guard
 
Join Date: Jul 2008
Posts: 14
Default 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;
}
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.
Reply With Quote
  #2   (View Single Post)  
Old 1st August 2008
deemon's Avatar
deemon deemon is offline
Fdisk Soldier
 
Join Date: May 2008
Location: Estonia
Posts: 50
Default

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.
__________________
Fhtagn nagh Yog-Sothoth

Last edited by deemon; 1st August 2008 at 06:38 AM.
Reply With Quote
  #3   (View Single Post)  
Old 1st August 2008
jgroch jgroch is offline
Port Guard
 
Join Date: Jul 2008
Posts: 14
Default

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.
Reply With Quote
  #4   (View Single Post)  
Old 1st August 2008
deemon's Avatar
deemon deemon is offline
Fdisk Soldier
 
Join Date: May 2008
Location: Estonia
Posts: 50
Default

Ok - which version? The oldest gcc I have is 3.4.2 on FreeBSD 5.4 and it gives error on such occasions.
__________________
Fhtagn nagh Yog-Sothoth
Reply With Quote
  #5   (View Single Post)  
Old 1st August 2008
jgroch jgroch is offline
Port Guard
 
Join Date: Jul 2008
Posts: 14
Default

gcc --version says: gcc (GCC) 4.2.1 20070719 [FreeBSD]. I'm using FreeBSD release 7.0.
Reply With Quote
  #6   (View Single Post)  
Old 1st August 2008
deemon's Avatar
deemon deemon is offline
Fdisk Soldier
 
Join Date: May 2008
Location: Estonia
Posts: 50
Default

Maybe it's because you don't have any resource limits set?
What is your limits output?
__________________
Fhtagn nagh Yog-Sothoth
Reply With Quote
  #7   (View Single Post)  
Old 1st August 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Default

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.
Reply With Quote
  #8   (View Single Post)  
Old 1st August 2008
BSDfan666 BSDfan666 is offline
Real Name: N/A, this is the interweb.
Banned
 
Join Date: Apr 2008
Location: Ontario, Canada
Posts: 2,223
Default

As ephemera said, large static allocations are never a good idea..
Reply With Quote
  #9   (View Single Post)  
Old 1st August 2008
jgroch jgroch is offline
Port Guard
 
Join Date: Jul 2008
Posts: 14
Default

Quote:
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

Quote:
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.
Reply With Quote
Old 1st August 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Default

> 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.
Reply With Quote
Old 1st August 2008
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Default

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''.
Reply With Quote
Old 1st August 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Default

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;
}
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.)

Last edited by ephemera; 2nd August 2008 at 06:52 AM.
Reply With Quote
Old 1st August 2008
jgroch jgroch is offline
Port Guard
 
Join Date: Jul 2008
Posts: 14
Default

Quote:
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.

Quote:
... take a stab at the debugger...
I started looking at it earlier this week, but the link will help.

Quote:
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()).
Reply With Quote
Old 1st August 2008
BSDfan666 BSDfan666 is offline
Real Name: N/A, this is the interweb.
Banned
 
Join Date: Apr 2008
Location: Ontario, Canada
Posts: 2,223
Default

'echo $?' prints the return value of the previously executed command..

@ephemera, the [cmd][/cmd] bbcode is really useful...
Reply With Quote
Old 1st August 2008
jgroch jgroch is offline
Port Guard
 
Join Date: Jul 2008
Posts: 14
Default

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.)
Reply With Quote
Old 1st August 2008
BSDfan666 BSDfan666 is offline
Real Name: N/A, this is the interweb.
Banned
 
Join Date: Apr 2008
Location: Ontario, Canada
Posts: 2,223
Default

A return value of 0 is usual, it indicates success... but, a program doesn't necessarily have to stick with tradition.
Reply With Quote
Old 2nd August 2008
jgroch jgroch is offline
Port Guard
 
Join Date: Jul 2008
Posts: 14
Default

Ok, I understand. The program does return 53 using = and not ==.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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


All times are GMT. The time now is 08:30 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick