• C

Segmentation fault with C array...

***Array1 loads from fpmap; but I get a segmentation fault
when entering the load function for Array2 from fps21!
I'm using Sun Workshop C compiler 4.2 on Solaris 2.5.1.

I would apprecieta some help.  Thanks in advance for your
time.

Regards!

Denmark

/* Filename: S21flocs.c */
#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#define S21_COUNT 6492

#define MAP_COUNT 210

char S21_filename[80]="\0";

char map_filename[80]="\0";

char rpt_filename[80]="\0";



main(argc, argv)

int argc;

char *argv[];

{

FILE *fps21;

FILE *fpmap;

FILE *fprpt;

char chk_val[10]="\0";

char tmp_val[10]="\0";

char *s21_arr[S21_COUNT];

char *map_arr[MAP_COUNT];

int i=0;
int ctr=0;

int loop_ctr=0;



if (argc == 1)

{

sprintf(S21_filename,"%s.lst","/export/home/s21files");
sprintf(map_filename,"%s.dat","/export/home/mfextmap");
sprintf(rpt_filename,"%s.rpt","/export/home/s21flocs");
}

 

printf("\n\nWorking...");

 

if ((fpmap = fopen(map_filename,"r")) == NULL)

{

printf("\n\n%s file not found.",map_filename);

}

printf("\n\nLoading (MFEXTMAP.DAT) file into array1...");

for (i=0 ; i < MAP_COUNT ; i++)

{

map_arr[i] = (char *) malloc(strlen(chk_val) + 10);

fscanf(fpmap,"%s",map_arr[i]);

}

fclose(fpmap);

printf("\n\n(MFEXTMAP.DAT file loaded into Array1!");

printf("\n\nLoading (s21files.lst) file into Array2...");

if ((fps21 = fopen(S21_filename,"r")) == NULL)

{

printf("\n\n%s file not found.",S21_filename);

}


for (i=0 ; i < S21_COUNT ; i++)

{

s21_arr[i] = (char *) malloc(strlen(chk_val) + 10);
fscanf(fps21,"%s",s21_arr[i]);

}

fclose(fps21);


printf("\n\n(S21files.lst) file loaded into Array2!");

dweatherbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

q2guoCommented:
Are you sure you 're opening file successfully?
'cause the way you wrote you program, it tries to read from the file even if function fopen fails.
0
nietodCommented:
Could be the problem.  Also the 2nd array contains a huge number of entries 6000.  You are creating each of these entries in the heap.  You could simply be runnign out of heap space.
0
nietodCommented:
Note to check if you are running out of stack space, check the pointer returned fom malloc() to make sure it is not NULL befure you use it.
0
Managing Security Policy in a Changing Environment

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

nietodCommented:
Opps.  I meant "heap space", not "stack space".  
0
emmonsCommented:
This seems to work pretty well on my machine. I read in 8000 entries.  At the very least you will know where the error occurs.


                   /* Filename: S21flocs.c */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define S21_COUNT 6492
#define MAP_COUNT 210
char S21_filename[80]="\0";
char map_filename[80]="\0";
char rpt_filename[80]="\0";

void main(argc, argv)
int argc;
char *argv[];
{
    FILE *fps21;
    FILE *fpmap;
    FILE *fprpt;
    char chk_val[10]="\0";
    char tmp_val[10]="\0";
    char *s21_arr[S21_COUNT];
    char *map_arr[MAP_COUNT];
    int i=0;
    int ctr=0;
    int loop_ctr=0;
    if (argc == 1) {
        sprintf(S21_filename,"%s.lst","s21files");
        sprintf(map_filename,"%s.dat","mfextmap");
        sprintf(rpt_filename,"%s.rpt","s21flocs");
    }
    printf("\n\nWorking...");
    if ((fpmap = fopen(map_filename,"r")) == NULL) {
        printf("\n\n%s file not found.",map_filename);
        return;
    }
    printf("\n\nLoading (MFEXTMAP.DAT) file into array1...");
    for (i=0 ; i < MAP_COUNT ; i++) {
        map_arr[i] = (char *) malloc(strlen(chk_val) + 10);
        if( map_arr[i] == NULL) {
            printf("\n\nRan out of memory allocating %d entry in map_arr.", i );
            return;
        }
        fscanf(fpmap,"%s",map_arr[i]);
/*        printf( "reading map_arr[%d]=%s\n", i, map_arr[i]); */
    }
    fclose(fpmap);
    printf("\n\n(MFEXTMAP.DAT file loaded into Array1!");
    printf("\n\nLoading (s21files.lst) file into Array2...");
    if ((fps21 = fopen(S21_filename,"r")) == NULL) {
        printf("\n\n%s file not found.",S21_filename);
        return;
    }

    for (i=0 ; i < S21_COUNT ; i++) {
        s21_arr[i] = (char *) malloc(strlen(chk_val) + 10);
        if( map_arr[i] == NULL) {
            printf("\n\nRan out of memory allocating %d entry in s21_arr.", i );
            return;
        }
        fscanf(fps21,"%s",s21_arr[i]);
/*        printf( "reading s21_arr[%d]=%s\n", i, s21_arr[i]); */
    }
    fclose(fps21);
    printf("\n\n(S21files.lst) file loaded into Array2!");
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dweatherbAuthor Commented:
After making several changes using your recommendations, Including redefining the S21_COUNT variable to 500, I got the
program to run. I get the same segmentation fault when I try to
load more than 500+. I get the same error with 1000.
Any suggestions as to why I am running out of stack space, if that is the problem?

Thanks in advance for your help!

Regards!

0
nietodCommented:
It is not stack space, it is (probably) heap space.  The only way to make sure is to check the return value from malloc()  It it returns NULL you have run out of space.  Try that, first before making other changes.
0
emmonsCommented:
So you get a system error, but do not hit any of the checks in the code??
0
emmonsCommented:
Another interesting test would be to move the two big arrays out of the routine and up into gloabl space. It may well be a stack issue. I am not sure exactly where you are getting the error.
0
emmonsCommented:
Or, just make them static. The goal is to determine if you are running out of stack or heap. If it is heap, then checking the return from malloc will solve the probelm. If it is stack, then you will have allocate more stack space when you build.
0
nietodCommented:
There is no reason to run out of stack space.  The loop code is not putting anything onto the stack.  
0
emmonsCommented:
That presumes that something is checking to make sure that he does not run off the end of the stack and into never never land when he comes into the routine. THere may not be enough stack space to allocate  7000 entries.
0
dweatherbAuthor Commented:
Thanks! I finally resolved the problem.
It was due to the data length in my data files. Some entries
in one file extensions still while the other file being compared
had entries without extensions. Also I had defined the Character
array as chk_val[12] and some entries were larger than 12 characters.
After the changes I was able to load all 6000+ entries.
Does anybody know what is the physical limit of the heap.
The limit is a function of what?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.