Thread: segmentation faults

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    3

    Question segmentation faults

    Hi guys,
    I was running a simple code on Fedora 11. it has this error " segmentation fault". The code is too simple to have any mistakes........ Can anyone help me with this? What is this "segmentation fault".

    Thank you very much.

    Code:
    #include "stdio.h" 
    int main()
    {
    int T[1001][4001]; 
    int i=0;
    int j=0; 
    for(j=0;j<1001;j++)
    { 
    for(i=0;i<4001;i++)
    { 
    T[j][i]=0; 
    }
    } 
    
    printf("Thank you!\n"); 
    
    return 0;
    }

    I saved this file as mainapp.cpp.

    command line:

    g++ mainapp.cpp -o test
    ./test

    then it says segmentation fault........

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The probability of you being allowed to have four million ints on the stack is zero. If you need four million ints, you will have to manage that memory yourself (with malloc/free/and friends).

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't program in linux, but I suspect that you're just asking for too much memory, on the stack.

    That's a truck load of bytes there, partner!

    Try it with 1,000 and see what happens.

    Segmentation fault is an "Oh ........, my engine broke the crank", kind of error. Usually occurs when something in your program (like a pointer), refers to something outside of it's allocated memory space.

    If you need more memory than you can get this way, try making it a global (before main()). If that still isn't enough (and it won't be enough for this amount), then you'll need to malloc() that memory from the heap. The memory must not only be available, in the quantity you're requesting, but it must also be available in one contiguous "chunk".

    For your program code, I have two suggestions:

    1) Use spaces instead of tabs - tabs don't always work with the forum software, just right. Two to four spaces, is ideal for each level of indentation.

    2) Use indentation, always. Don't leave home without it, buddy!

    Good on ya, for using code tags around your program - you're ahead of the curve, already.
    Last edited by Adak; 09-21-2009 at 09:18 PM.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    Quote Originally Posted by tabstop View Post
    The probability of you being allowed to have four million ints on the stack is zero. If you need four million ints, you will have to manage that memory yourself (with malloc/free/and friends).
    Thanks for the help. Could you tell me how to change the size of the stack. I know with VC++, it is easy to resize the stack. But in Linux, how to do it?

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    Quote Originally Posted by Adak View Post
    I don't program in linux, but I suspect that you're just asking for too much memory, on the stack.

    That's a truck load of bytes there, partner!

    Try it with 1,000 and see what happens.

    Segmentation fault is an "Oh ........, my engine broke the crank", kind of error. Usually occurs when something in your program (like a pointer), refers to something outside of it's allocated memory space.

    If you need more memory than you can get this way, try making it a global (before main()). If that still isn't enough (and it won't be enough for this amount), then you'll need to malloc() that memory from the heap. The memory must not only be available, in the quantity you're requesting, but it must also be available in one contiguous "chunk".

    For your program code, I have two suggestions:

    1) Use spaces instead of tabs - tabs don't always work with the forum software, just right. Two to four spaces, is ideal for each level of indentation.

    2) Use indentation, always. Don't leave home without it, buddy!

    Good on ya, for using code tags around your program - you're ahead of the curve, already.
    Thanks for the tips....... But I still do not know how to resize the stack? I know how to do it in VC++. but in Linux,, I dont know how.......... Could you tell me how to do it?

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It might work if you make the array a global variable, since globals are also allocated on a separate block.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    26
    You should try this before running GDB

    ulimit -c unlimited

    It might increase the size of stack....

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by subhashish1213 View Post
    You should try this before running GDB

    ulimit -c unlimited

    It might increase the size of stack....
    Nope, that will only increase the allowable size of a core file. What he wants to do, (assuming he insists on ignoring Tabstop's advice about using dynamic memory allocation) is to issue the ulimit -s command

    At the OP, you can read about it in your ulimit man page, but if you do

    Code:
    ulimit -a
    you will get a listing of your shell resource limits. There are soft and hard resource limits, and the foregoing command will show you (if any) the soft limits. By doing

    Code:
    ulimit -Ha
    you will get the same listing of all (-a) limits, but now they will be displayed with the hard (-H) limits. In the case of your Fedora install, I am willing to bet that the soft stack size will be 10240 kilobytes, and the hard limit will be unlimited. To change the size of the stack, just do

    Code:
    ulimit -s <new size in kb>
    So then, if you wanted the stack to be 20500 kb you would do:

    Code:
    ulimit -s 20500
    If you wanted that as your hard limit, you would do:

    Code:
    ulimit -Hs 20500
    If your original stack hard limit is unlimited (and I would guess it is), then you can get to that limit by doing:

    Code:
    ulimit -s unlimited
    A couple of things to remember though:

    1) The change you make is not persistent across logins. Once you logout of that shell, the change is gone.

    2) Once you set the upper limit, you will not be allowed past that. If you set it too low, you will need to log in to a new shell.

    Finally, I put all of this up here for information purposes, but I really think it is best, as per Tabstop's advice, to use dynamic memory allocation.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    1) Use spaces instead of tabs - tabs don't always work with the forum software, just right. Two to four spaces, is ideal for each level of indentation.
    That is subjective. Use spaces OR tabs, whichever you prefer. There are both cons and pros with both.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation faults when initializing large arrays
    By MathFan in forum C++ Programming
    Replies: 5
    Last Post: 07-14-2008, 05:24 AM
  2. oldiofclose.c and segmentation faults
    By sd_padilla in forum C Programming
    Replies: 1
    Last Post: 12-11-2005, 02:24 PM
  3. Segmentation faults on Linked Lists. (Please help!!)
    By summerrainx in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2005, 07:23 AM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  5. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM

Tags for this Thread