array declaration

This is a discussion on array declaration within the C Programming forums, part of the General Programming Boards category; Please allow me to make a beginner's question. I'd like to declare an array of structures for holding data of ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    4

    array declaration

    Please allow me to make a beginner's question. I'd like to declare an array of structures for holding data of a worksheet:
    Code:
    struct row {
    int name; int age; int dob; ...(many entries)...
    }; main() {
    struct row worksheet[MAXROW]; ....
    }
    and I got an error message "Segmentation fault (core dumped)" when MAXROW=500. (size of struct row = 5244byte)
    As I did not get the error when MAXROW=10, the problem seems to be an available memory space, but I could also make it without the error under the following two versions of codes:

    version1 (MAXROW=500)
    Code:
    struct row {
    int name; int age; int dob; ...(many entries)...
    } worksheet[MAXROW]; main() {
    ....
    }
    version2 (MAXROW=500)
    Code:
    #include <stdlib.h>
    
    struct row {
    int name; int age; int dob; ...(many entries)...
    }; main() {
    struct row *worksheet; worksheet = calloc(MAXROW, sizeof(struct row)); ....
    }
    Could anyone please point out what's wrong in the first code?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    2622000 is the number of bytes your large array occupies.
    Note that the typical stack allocation for local variables is 1MByte (you blow that away with one line of code!)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    Thank you so much, that's explain everything. (I did not even know that there's upper limit for local variable size.)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Well you can usually change it, but exactly how you do that varies widely from one system to another.

    For the problem you had, there are a number of easy ways out which don't involve stack usage. You could even have done this if you were worried about scope.
    Code:
    int main()
    {
        static struct row worksheet[MAXROW]; ....
    
    }
    1Mb is usually plenty enough stack space for programs except ones which are massively recursive.

    If you ever get to program embedded systems, you might find the stack space allocation harsh. Sometimes as low as a few Kb.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    Thanks a lot again. Now I come to understand that there is usually no need of auto variable more than 1Mb, as in my case. static specifier works perfectly, I will go with it.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,030
    In the future, I would actually suggest you learn about dynamic memory and allocate enough to meet your demands instead of just brute forcing enough memory.
    Dynamic memory may be a little complicated, but there have been many examples on the board in the past...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    Hi, thank you for the advise. I studied a basic idea of dynamic memory allocation in K&R. Some fields of my database in fact contain strings whose length vary each row, so I should've used the method at least for that part.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 08:01 PM
  3. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 09:57 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-12-2004, 12:15 AM
  5. array of pointer objects
    By Death_Wraith in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 08:06 PM

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