Solved

Segmentation fault with C array...

Posted on 1998-03-17
13
493 Views
Last Modified: 2010-04-02
***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!");

0
Comment
Question by:dweatherb
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 3

Expert Comment

by:q2guo
ID: 1257738
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
 
LVL 22

Expert Comment

by:nietod
ID: 1257739
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
 
LVL 22

Expert Comment

by:nietod
ID: 1257740
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
 
LVL 22

Expert Comment

by:nietod
ID: 1257741
Opps.  I meant "heap space", not "stack space".  
0
 
LVL 4

Accepted Solution

by:
emmons earned 100 total points
ID: 1257742
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
 

Author Comment

by:dweatherb
ID: 1257743
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 22

Expert Comment

by:nietod
ID: 1257744
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
 
LVL 4

Expert Comment

by:emmons
ID: 1257745
So you get a system error, but do not hit any of the checks in the code??
0
 
LVL 4

Expert Comment

by:emmons
ID: 1257746
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
 
LVL 4

Expert Comment

by:emmons
ID: 1257747
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
 
LVL 22

Expert Comment

by:nietod
ID: 1257748
There is no reason to run out of stack space.  The loop code is not putting anything onto the stack.  
0
 
LVL 4

Expert Comment

by:emmons
ID: 1257749
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
 

Author Comment

by:dweatherb
ID: 1257750
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now