Operating System - Linux
1748230 Members
3999 Online
108759 Solutions
New Discussion юеВ

Re: C Question: char messagebuf[8199999]

 
SOLVED
Go to solution
tony j. podrasky
Valued Contributor

C Question: char messagebuf[8199999]

Hello Folks;

 

I know this isn't exactly an admin question - but I know someone can easily answer it.

 

I wrote a simple "grim reaper" program to go through the /var/log/message file

and remove all the useless (as far as I'm concerned) messages that Fedora 16

generates.

 

Here's a short chunk of code:

 

"NOTE: the [char messagebuf] is shown with 2 values: one works, one doesn't . They aren't both in the code at the same time] :-)

--------


    char messagebuf[8199999];    [NOTE THIS WORKS]
    char messagebuf[10199999];    [NOTE THIS CAUSES segmentation faults]

    FILE *messagefp; 

    if((messagefp = fopen(MESSAGE, "r")) == NULL)        
        {  
        printf ("ERROR: could not open file: %s \n", MESSAGE); 
        return 0;                                            
        }                                                      


    while(fscanf(messagefp, "%c", &messagebuf[dataend]) == 1) 
        ++dataend;   

    fclose(messagefp);                     

--------

 

The problem is when I increase the value of messagebuf past 8MB, I get a segmentation fault (core dumped) When I execute it regardless of the size of the input file.

 

Question: how can I make char messagebuf[>8MB]?

 

regards,

tonyp

 

 

REMEMBER: Once you eliminate your #1 problem, #2 gets a promotion.
3 REPLIES 3
Steven Schweda
Honored Contributor
Solution

Re: C Question: char messagebuf[8199999]

 
tony j. podrasky
Valued Contributor

Re: C Question: char messagebuf[8199999]

Hello Steven;

 

Thanx for your help, Steven.

 

I set the "static char messagebuf[10000000] and it compiled and ran fine.

 

regards,

tonyp

 

REMEMBER: Once you eliminate your #1 problem, #2 gets a promotion.
Dennis Handly
Acclaimed Contributor

Re: C Question: char messagebuf[8199999]

>I increase the value of messagebuf past 8MB, I get a segmentation fault when I execute it regardless of the size of the input file.

 

Based on Steven's comments about stack size, if the stack grows towards 0, then the first byte you put into the array will be beyond the top of the stack.