Thread: Segmentation fault before entering main function

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    12

    Segmentation fault before entering main function

    Hi to all!I have a program with 3 separate functions(sources) that are called in main.
    I habe 3 global variables that are arrays.Everything works fine but when i try to pass in main the 3 variables (make them local) i receive segmentation fault.Via debugging i ebserved that this happens just before entering the main functions.
    When the 3 variables(unsigned char image[2048*3*2048];int gray_image[2048*2048];
    unsigned char ee_image[2048*2048];) are outside of main as global all is ok!
    Any thoughts???
    Thanks in advance.

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,097
    It's too big for any single function to hold, to make them local you need to make use of calloc() & free(), I believe you're capable of doing your own research now that you know the direction to look

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,998
    Stack size is typically fairly limited, and here you are asking for 32 MB or so in a single call.

    Do they truly represent global state? If so, then perhaps declaring them global is okay. If not, a workaround could be to declare them static within the main function: then at least you can limit what they get passed to while avoiding the use of the stack.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jul 2020
    Posts
    12
    Quote Originally Posted by laserlight View Post
    Stack size is typically fairly limited, and here you are asking for 32 MB or so in a single call.

    Do they truly represent global state? If so, then perhaps declaring them global is okay. If not, a workaround could be to declare them static within the main function: then at least you can limit what they get passed to while avoiding the use of the stack.
    Hi and thanks for the help
    I passed them as static but i get the same error into the code not at the beginning!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,998
    Quote Originally Posted by stzog
    I passed them as static but i get the same error into the code not at the beginning!
    Here's a test program that I wrote that allocates the same amount of memory as you are trying to allocate on the stack, assuming sizeof(int) == 4:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int numbers[8 * 1024 * 1024];
        printf("%zu\n", sizeof(numbers));
        return 0;
    }
    I compiled and ran this program, and ran into a segmentation fault. Here's the same program with the use of static:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        static int numbers[8 * 1024 * 1024];
        printf("%zu\n", sizeof(numbers));
        return 0;
    }
    I compiled and ran this program, and this was the output:
    Code:
    33554432
    Since having the variables as global variables works for you, you must have enough memory. Therefore, you must have done something wrong when changing to use static, but I cannot tell you what since you didn't show the code.

    awsdert's suggestion in post #2 is also a valid strategy, except that there's more book keeping for you to do.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,097
    Quote Originally Posted by stzog View Post
    Hi and thanks for the help
    I passed them as static but i get the same error into the code not at the beginning!
    Well segmentation faults are typically the result of NULL pointer but more generally because an attempt to read/write memory that doesn't have the appropriate permissions, in the case of arrays declared via the form of name[count] it normally means that an attempt to index just beyond it's limits was made and that normally is in function execution space which is normally readable by the CPU only, meaning it's contents are not allowed to be returned to the code. Basically check for NULL pointers and any attempts to read/write values are within bounds of the array

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    440
    Quote Originally Posted by stzog View Post
    Hi to all!I have a program with 3 separate functions(sources) that are called in main.
    I habe 3 global variables that are arrays.Everything works fine but when i try to pass in main the 3 variables (make them local) i receive segmentation fault.Via debugging i ebserved that this happens just before entering the main functions.
    When the 3 variables(unsigned char image[2048*3*2048];int gray_image[2048*2048];
    unsigned char ee_image[2048*2048];) are outside of main as global all is ok!
    Any thoughts???
    Thanks in advance.
    You want to malloc such large variables.
    Declaring them as locals will blow the stack, as others have pointed out. Declaring them as globals will work but might lead to odd behaviour if the OS can't find memory for them. Also, you probably want to generalise at some point to images of arbitrary dimensions. malloc() will return a neat error code, in theory at least, on the out of memory condition.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  8. #8
    Registered User
    Join Date
    Jul 2020
    Posts
    12
    i used again static declaration of the variables with the same array size and i put just after a printf("%zu\n", sizeof(gray_image)); All of a sudden the program run flawlessly and segmentation fault disappeared.
    Can that be explained?I had also used static declaration previously but i had the problem.
    Last edited by stzog; 09-17-2020 at 12:17 PM.

  9. #9
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,097
    Quote Originally Posted by stzog View Post
    i used again static declaration of the variables with the same array size and i put just after a printf("%zu\n", sizeof(gray_image)); All of a sudden the program run flawlessly in debug mode and segmentation fault disappeared.. In release mode the problem insists.
    Can that be explained?I had also used static declaration previously but i had the problem.
    Debug mode ALWAYS initializes uninitialized variables to 0, meaning you clearly have an uninitialized variable or variables somewhere, go look for that

    Edit: Those kind of errors are normally reported if you at least the -Wall or /Wall switch active (which one depends on compiler), I don't remember about VC but GCC and compilers that mimic it usually have a -Wextra switch as well, even a -pedantic switch, try to have as many of those switches active at once to catch any possible errors they can catch
    Last edited by awsdert; 09-17-2020 at 12:21 PM.

  10. #10
    Registered User
    Join Date
    Jul 2020
    Posts
    12
    Quote Originally Posted by awsdert View Post
    Debug mode ALWAYS initializes uninitialized variables to 0, meaning you clearly have an uninitialized variable or variables somewhere, go look for that

    Edit: Those kind of errors are normally reported if you at least the -Wall or /Wall switch active (which one depends on compiler), I don't remember about VC but GCC and compilers that mimic it usually have a -Wextra switch as well, even a -pedantic switch, try to have as many of those switches active at once to catch any possible errors they can catch
    After clean and rebuild the program runs correctly in release mode also !

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    Debug mode ALWAYS initializes uninitialized variables to 0, meaning you clearly have an uninitialized variable or variables somewhere, go look for that
    Unless you're talking about a static or global variable, not all compilers will initialize your variables in debug mode. Static and global variables are always default initialized.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault in ioctl function
    By Venturina in forum C Programming
    Replies: 1
    Last Post: 08-19-2014, 11:24 AM
  2. Segmentation Fault -> Main not initializing
    By PoisonMind in forum C Programming
    Replies: 2
    Last Post: 03-15-2013, 01:53 PM
  3. Segmentation fault when entering a for loop
    By rehpot in forum C Programming
    Replies: 6
    Last Post: 02-17-2011, 09:59 PM
  4. Segmentation Fault in main()
    By sa125 in forum C Programming
    Replies: 5
    Last Post: 07-27-2008, 06:20 AM
  5. segmentation fault before main
    By Cristian Negresco in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2002, 09:14 AM

Tags for this Thread