problems with array sizes

This is a discussion on problems with array sizes within the C Programming forums, part of the General Programming Boards category; forgive me if I'm being stupid, but I've been using C for about a week so bear with me.... I ...

  1. #1
    Registered User Markallen85's Avatar
    Join Date
    Nov 2002
    Posts
    53

    problems with array sizes

    forgive me if I'm being stupid, but I've been using C for about a week so bear with me....

    I need to use a few pretty large arrays (highest would be about 256*256*256) of floats or doubles - most smaller than that though. But I keep getting errors when I define them.

    If I just start a new program and put:

    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char *argv[])
    {
    float list1[1024][1024];
    system("PAUSE");
    return 0;
    }

    the program crashes on loading. despite the fact that there is nothing actually being done to the arrays yet.

    It's definitley not a problem with lack of memory (I got 450mb free while windows is running). this is a console C program in the bloodshed compiler.

    I tried reducing the size to 512*512 and that worked, but crashed again when I added another large array.

    I've tried the debug thingy in the bloodshed compiler (even thought there's not exactly a lot to debug) and it says.

    "An Access Violation (Segmentation Fault) raised in you program"

    anyone know what's going on/how I can solve it. I always though C was essentially limitless.....

    thanks
    "never argue with an idiot, they will drag you down to their level and beat you with experience"

  2. #2
    Registered User Markallen85's Avatar
    Join Date
    Nov 2002
    Posts
    53
    BIG thankyou works fine now, woohoo!!

    could you just give a brief explanation of what static actually does?

    thanks again
    "never argue with an idiot, they will drag you down to their level and beat you with experience"

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    29
    When a variable within a function is declared with the static specifier, the variable has a permanent duration. In other words, the memory storage allocated for the variable is not destroyed when the scope. If excecution ever returns to the scope of the variable, the last value stored in the variable is still there. Here is an example:

    Code:
    //using the static specifier
    #include<stdio.h>
    int add_two(int x, int y){
    static int counter=1;
    printf("This is the function call of %d,\n",counter++);
    return (x+y);}
    
    main(){
    int i,j;
    for(i=0,j=5;i<5;j--)
    printf("the addition of %d and %d is %d.\n",i,j,add_two(i,j));
    return 0;}
    the output you get should be something like
    ----output----
    this is the function call of 1,
    the addition of 0 and 5 is 5

    this is the function call of 2,
    the addition of 1 and 4 is 5

    this is the function call of 3,
    the addition of 2 and 3 is 5

    this is the function call of 4,
    the addition of 3 and 2 is 5

    this is the function call of 5,
    the addition of 4 and 1 is 5
    ----output----

    The vaules stored by counter are retained because the duration of the variable is permanent. In other words, although the scope of counter is within the block of the add_two() function, the memory loction of counter and the value saved in the location are not changed after the calling of add_two(). I hope this help with what the static specifier does.

  4. #4
    Registered User Markallen85's Avatar
    Join Date
    Nov 2002
    Posts
    53
    that seems to make sense. thanks, I'll remember that if I need it again
    "never argue with an idiot, they will drag you down to their level and beat you with experience"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 11:01 AM
  2. Helllppp!!! I/O and array problems
    By dorky in forum C++ Programming
    Replies: 3
    Last Post: 07-02-2005, 10:24 AM
  3. problems finding the average of an array column
    By mrgeoff in forum C Programming
    Replies: 4
    Last Post: 04-19-2005, 12:49 AM
  4. array creation from variable sizes
    By AtomRiot in forum C Programming
    Replies: 9
    Last Post: 02-09-2003, 04:03 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21