Segmentation fault error in function using 3D dynamic array - c

I'm working on a code which purpose is to manipulate a 1D array coming from a 3D array. Showing the code will explain everything:
First of all, I'm working with array of structures:
typedef struct range_in_memory {
double E, R;
} RANGE;
And I wrote this function that works like a charm when called in simple debug implementation:
RANGE *dq_Eloss_load_range_file (double Aion, double Zion, double Atar, double Ztar, int *n){
char *filename;
char errormsg[80], dummy;
int N = 2;
int i = 0;
FILE *fp;
RANGE *memrange;
filename = (char*)malloc (50*sizeof (char));
dq_Eloss_set_filename (filename, Aion, Zion, Atar, Ztar);
fp = dq_myfopen (filename, "r", errormsg);
while ( !feof (fp) ){
fscanf (fp, "%c", &dummy);
if (dummy == '\n') {
N++;
}
}
rewind (fp);
memrange = (RANGE*) calloc (N, sizeof (RANGE));
while ( !feof (fp) ){
fscanf (fp, "%lf\t%lf\n", &memrange[i].E, &memrange[i].R);
i++;
}
*n = N;
// for (i=0; i<N; i++){
// printf ("\n%lf %lf", memrange[i].E, memrange[i].R);
// }
fclose (fp);
return (memrange);
}
The commented for cycle is crucial, so keep it in mind.
Now, coming to my problem, I need to use a 3D array like:
RANGE ***memrangeTAR;
The function *dq_Eloss_load_range_file loads the content of a file int an array of RANGE type. I want to do this for several files, which are named according to two parameters Aion and Zion. In my mind it would have been something like:
(double) memrangeTAR [Aion][Zion][i].E
(double) memrangeTAR [Aion][Zion][i].R
which would have been the values in the i-th line in the file related to Aion and Zion.
This is the code:
void dq_load_range_files (RANGE ***memrangeTAR){
int NTAR;
double *ZMAX;
int Aion, Zion, AMAX = 250.;
int i;
printf ("\n Allocating Memory for range data..."); fflush (stdout);
ZMAX = (double*) malloc ((AMAX+1)*sizeof (double));
memrangeTAR = (RANGE***) malloc ((AMAX+1)*sizeof (RANGE**));
for (Aion=1; Aion<=AMAX; Aion++){
ZMAX[Aion] = ceil (dq_range_table_get_max_Zion (Aion));
memrangeTAR [Aion] = (RANGE**) malloc ((ZMAX[Aion]+1)*sizeof(RANGE*));
}
printf (" ...ALLOCATED! \n\n");
printf ("\n Loading range data...\n"); fflush (stdout);
for (Aion=1; Aion <=AMAX; Aion++){
for (Zion=1; Zion<=ZMAX[Aion]; Zion++){
memrangeTAR [Aion][Zion] = dq_Eloss_load_range_file ((double)Aion, (double)Zion, cor_sosau16.mass.Mtar, cor_sosau16.charge.Ztar, &NTAR);
// for (i=0; i<N; i++){
// printf ("\n%lf %lf", memrangeTAR[Aion][Zion][i].E, memrangeTAR[Aion][Zion][i].R);
// }
}
}
}
It compiles (gcc on unix machine using gnu99 standard). It runs unless i try to access to the 3D array. If I de-comment the printing cycle in the first function, I can get what I want: it prints on terminal the content of the current file. If I de-comment the for cycle in the second function, it gives me segmentation fault.
What am I doing wrong?

I somehow managed to solve but not to understand WHY it's a solution.
By defining a
RANGE *tmp;
variable, calling the usual function as
tmp = dq_Eloss_load_range_file ((double)Aion, (double)Zion, cor_sosau16.mass.Mtar, cor_sosau16.charge.Ztar, &NTAR);
and then assigning wanted pointer:
memrangeTAR[Aion][Zion] = tmp;
I can use the content of the variables in any way now, like:
for (i=0; i<N; i++){
printf ("\n%lf %lf", memrangeTAR[Aion][Zion][i].E,
memrangeTAR[Aion][Zion][i].R);
}
In this way no segmentation fault error is encountered, making me think that the problem was the way I was passing the pointer to the function, but still don't understand...

Related

Problem either in allocation or handling array of pointer to struct

I have a problem (or maybe a few)
I'm trying to struggle with pointers to pointers to struct, and I'm not understanding whether is a problem of memory allocation, or a problem of passing the right things to my function.
Here's (part of) the code
typedef struct Corsa {
char codice[15];
char partenza[15];
char destinazione[15];
int giorno, mese, anno, oraP, minP, oraA, minA, ritardo;
} corsa;
corsa** allocaStructArray(int numStruct);
void leggiEStampaStruct(FILE *fp, corsa **pntToCorsa, int numR);
// ..main and other functs..
corsa** allocaStructArray(int numStruct) //numStruct is the number of 'rows'
{
corsa **pointerToStructCorsa;
pointerToStructCorsa = malloc(numStruct * sizeof(corsa));
if (pointerToStructCorsa == NULL)
printf("\nErrore di allocazione.\n");
return pointerToStructCorsa;
}
void leggiEStampaStruct(FILE *fp, corsa **pntToCorsa, int numR) {
int i;
fclose(fp);
fp = fopen("file.txt", "r");
if (fp == NULL)
exit(232);
for (i = 0; i < numR; i++) {
fscanf(fp, "%s %s %s %d/%d/%d %d.%d %d.%d %d", pntToCorsa[i]->codice,
pntToCorsa[i]->partenza, pntToCorsa[i]->destinazione,
&pntToCorsa[i]->giorno, &pntToCorsa[i]->mese, &pntToCorsa[i]->anno,
&pntToCorsa[i]->oraP, &pntToCorsa[i]->minP, &pntToCorsa[i]->oraA,
&pntToCorsa[i]->minA, &pntToCorsa[i]->ritardo);
}
for (i = 0; i < numR; i++) {
printf("\n%s %s %s %d/%d/%d %d.%d %d.%d %d\n", pntToCorsa[i]->codice,
pntToCorsa[i]->partenza, pntToCorsa[i]->destinazione,
pntToCorsa[i]->giorno, pntToCorsa[i]->mese, pntToCorsa[i]->anno,
pntToCorsa[i]->oraP, pntToCorsa[i]->minP, pntToCorsa[i]->oraA,
pntToCorsa[i]->minA, pntToCorsa[i]->ritardo);
}
return;
}
Another thing that I don't like is that I can't check manually if everything is at its place.
Like if it was a normal matrix, I could have checked if the values were placed in the right spots but with pointers I literally can't and that doesn't help finding the problem.
Do you guys have some tips on this other problem?
At least these problems
No width in fscanf()
Do not use "%s" without a width limit in fscanf(). Use "%14s %14s %14s ... as each points to an array of 15.
Check input result
Check the return value. Add a test to see if the 11 specifiers all parsed.
if (fscanf(....) != 11) Handle_Error();
Allocation error
Wrong type in sizeof.
Avoid this problem by sizing to the refenced object and not the type. Easier to code right, review and maintain. Also, unclear what OP wants to do when numStruct < 0, so added a test.
// Wrong size v-----------v
// pointerToStructCorsa = malloc(numStruct * sizeof(corsa));
if (numStruct >= 0) {
// v--------------------------v Right size for referenced object.
pointerToStructCorsa = malloc(sizeof *pointerToStructCorsa * numStruct);
...

Parse file into struct

I'm working on this C project (I'm fairly new to C), where I have to parse data from a file into a 2D array which is an element of a structure defined as follows:
typedef struct {
int x;
int y;
char ** result;
} gameF;
The file contains two integers at the top and these integers dictate the size of the 2D array result, while the data to be entered into this array is in a newline below them. Thus far, I have written the following code, but keep getting a segmentation error. I know that the problem lies somewhere between lines 11 and 16, since I have tried the rest of the code after commenting out these lines and it works fine.
gameF * Parsefile(char * fp){
FILE * ResData;
ResData = fopen(fp, "r");
gameF * MyF = malloc(sizeof(gameF));
char line[52];
while(fgets(line, 52, ResData) !=NULL){
int num = 0;
if(sscanf(line, "%d %d", &(*MyF).x, &(*MyF).y) != 0){
continue;
}
int i; // L11
for(i=0; i<52; i++){
if(line[i]==' '){
break;
}
(*MyF).result = line [i]; // L16
}
num ++;
}
return MyF;
}
Please help!
You are not allocating space (malloc) to (*MyF).result, before the assignment at line 16.
So the pointer is pointing to an invalid location in memory.
Also you are assigning the to member result to a single char (instead of a string) with
(*MyF).result = line [i];

pass dynamic array of pointers to C function

I want to store each line of the file in an 2D array, and anthor array pointing each line (so I can identify each line), I need to pass this pointers array to a function so I can manipulate my lines, I d'ont know how to do that
I have this code to read and store in the arrays
char ligne[MAX];
//open end test the file
FILE* fichier = fopen("csp.txt","r");
if(fichier == NULL){
printf("can't open the file \n");
return EXIT_FAILURE;
}
//the first line in the file contain number of other lines
fgets(ligne, sizeof(ligne), fichier);
int nbrTaille = strtol(ligne, NULL, 10);
//array of pointers
char (*tab)[nbrTaille] = malloc(nbrTaille * sizeof(ligne));
int i = 0;
//tab array point each line
while(fgets(ligne, sizeof(ligne), fichier)){
if(ligne == NULL) EXIT_FAILURE;
strncpy(tab[i], ligne, strlen(ligne));
printf("line%d : %s\n", i, tab[i]);
i++;
}
//call the funnction by passing array of pointers and the number of lines
allDiff(tab, nbrTaille);
the file I'm reading is
2
1 2
2 3
to receive the array by the function I tried this but it doesn't work
void allDiff(char** T, int taille)
I am always confused with the parenthesies and asterisks. The easier way is to declare a double pointer:
char ** tab;
//you need to take care of errors. I am not doing it for simplicity
//tab = {pointer1, pointer2, pointer3, ...., pointerN} total memory needed N * sizeof(pointer)
tab = malloc(lines* sizeof(tab)); //how many pointers you want
for(i = 0; i < lines; i++){
tab[i] = malloc(MAX); //each string
}
In the end free the memory:
for(i = 0; i < lines; i++){
free(tab[i]);
}
free(tab);
EDIT complete code
char ligne[MAX];
//open end test the file
FILE* fichier = fopen("csp.txt","r");
if(fichier == NULL){
printf("can't open the file \n");
return EXIT_FAILURE;
}
//the first line in the file contain number of other lines
fgets(ligne, sizeof(ligne), fichier);
int nbrTaille = strtol(ligne, NULL, 10);
//array of pointers
//char (*tab)[nbrTaille] = malloc(nbrTaille * sizeof(ligne));
int i = 0;
char **tab;
tab = malloc(nbrTaille * sizeof(tab)); //how many pointers you want
for(i = 0; i < nbrTaille; i++){
//sizeof(ligne) is equal to MAX
tab[i] = malloc(MAX); //each string
}
i = 0;
//tab array point each line
while(fgets(ligne, sizeof(ligne), fichier)){
if(ligne == NULL) EXIT_FAILURE;
strncpy(tab[i], ligne, strlen(ligne));
printf("line%d : %s\n", i, tab[i]);
i++;
}
//call the funnction by passing array of pointers and the number of lines
allDiff(tab, nbrTaille);
void function(char tab[][MAXLEN], ...);
This will do.
For the sake of readbility and sanity, typedef C function pointers before creating arrays or double pointers to them.
/* typedef the function pointer. An ftabptr points to a void function that
takes a char * */
typedef void (*ftabptr)(char *p);
/* create an uninitalised list of twenty of them */
int N = 20;
ftabptr *pointerlist = malloc(N * sizeof(ftabptr));
However I don't think you really want to do this. You can write a
functioning program that does weird things with tables of function
pointers, but normally you use a language other than C if you
want to play that game (Lisp-like languages, etc). The high level
language then often emits C as an intermediate step.

segmentation 11 in C

I'm writing a homework program in C. The program should take records from an input file and write those record to an output file. It seems like there is something wrong with the print_to_file function. I keep getting segmentation fault 11. Please help. My code is as below.
#include <stdio.h>
#include <stdlib.h>
typedef struct car { // create a struct type Car
char *license_plate;
int parking_spot;
int num_tickets;
int time_left;
} Car;
#define LICENSEPLATELENGTH 10
Car* import_cars(char *filename, int numCars);
void print_to_file(char* filename, Car* garage, int numCars);
int main(int argc, char * argv[]) {
if(argc != 4)
printf("Incorrect input.\n");
else {
int number = atoi(argv[1]);
Car* parked_car = (Car*)malloc(sizeof(Car) * number);
parked_car = import_cars(argv[2], number);
print_to_file(argv[3], parked_car, number);
free(parked_car);
}
return 0;
}
Car* import_cars(char* filename, int numCars)
{
Car* inCar = (Car*)malloc(sizeof(Car) * numCars);
inCar->license_plate = (char*)malloc(sizeof(char) * 8);
//Question: How do I do if I the plate length is varied. How to malloc space to it?
FILE* inFilePtr;
if((inFilePtr = fopen(filename, "r")) == NULL)
printf("Error! Unable to open file %s. Check again.\n", *filename);
else
{
int i = 0;
fscanf(inFilePtr, "%s", inCar[i].license_plate);
fscanf(inFilePtr, "%d%d%d", inCar[i].parking_spot, inCar[i].num_tickets, inCar[i].time_left);
printf("%s %d %d %d \n", inCar[i].license_plate, inCar[i].parking_spot, inCar[i].num_tickets, inCar[i].time_left);
for(i = 1; i < numCars; i++)
{
fscanf(inFilePtr, "%s", inCar[i].license_plate);
fscanf(inFilePtr, "%d%d%d", inCar[i].parking_spot, inCar[i].num_tickets, inCar[i].time_left);
printf("%s %d %d %d \n", inCar[i].license_plate, inCar[i].parking_spot, inCar[i].num_tickets, inCar[i].time_left);
}
}
fclose(inFilePtr);
return(inCar);
//free(inCar.license_plate); `
//Question: Do I need to free space here would it remove the value
//stored in the variable which passed to main?
}
void print_to_file(char* filename, Car* garage, int numCars) {
FILE* outFilePtr;
if((outFilePtr = fopen(filename, "w+")) == NULL){
printf("Error! Cannot Open File %s!", *filename);
printf("here\n");
} else {
int i = 0;
for(i = 0; i < numCars; i++) {
printf("%s\n%d %d %d\n", garage[i].license_plate, garage[i].parking_spot, garage[i].num_tickets, garage[i].time_left);
fprintf(outFilePtr, "%s\n%d %d %d\n", garage[i].license_plate, garage[i].parking_spot, garage[i].num_tickets, garage[i].time_left);
}
}
fclose(outFilePtr);
}
This is my input command.
./a.out 6 garage.txt output.txt
Here is what print in my terminal.
fi590dz 20 2 25
57fjgmc 8 0 55
7dkgjgu 25 1 15
f9e829d 1 2 60
4jgfd81 12 2 10
Segmentation fault: 11
By the way, I'm pretty new in programming and really bad with debugging. Could you give me some tips of how to debug or any debugging tools? I use a mac so gdb doesn't work.
Not a complete answer, because it’s a homework problem and you want to figure it out yourself, but here are some hints.
First, you really want to learn how to run your program in a debugger and get it to tell you which line crashed the program, and on which data.
Second, make sure you initialize the pointers for every element of the array before you try to read or write them.
Third, you’ll save yourself a lot of trouble if you initialize all your dynamic and local variables to zeroes, not garbage. It will make a lot of bugs reproducible, make a lot of bugs crash immediately instead of corrupting memory, and also make it obvious when you debug that you’re using uninitialized data.
Therefore, I suggest you get in the habit of allocating your dynamic arrays with calloc(), not malloc().
The problem lies within your parked_car = import_cars(argv[2], number); and Car* import_cars(char* filename, int numCars);functions.
Indeed in Car* import_cars(char* filename, int numCars); you are doing this:
Car inCar;
inCar.license_plate = (char*)malloc(sizeof(char) * 8);
So you are creating a local variable that is not accessible outside of the function (many different things can happen to the memory after the end of the function).
So when you do: parked_car = import_cars(argv[2], number); you are assigning to parked_car a freed variable.
A solution is to simply use the parked_caras an argument of your import_cars() function. All modifications made within the function will still be valid after it returns. So you should have:
void import_cars(char* filename, int numCars, Car* car);
For everyone who met the issue here, I found the problem in my program. The problem is that I didn't allocate space for each of the license_plate pointer in the structure. So my way to solve it is add a line as below in the for loop of the import_cars function.
inCar[i].license_plate = (char*)malloc(sizeof(char) * LICENSEPLATELENGTH);

Adding a struct to an array using a pointer

I am fairly new to the C language and allocating memory/using pointers in general. Anyway, I was experimenting with reading a file, putting those values in a struct, etc. I know exactly what I want to do and of course the program runs but the output is incorrect and some kind of jumbled numbers and letters.
There is a text file with new information for each line. Each line represents one object.
This is how a line in the file might look:
meat sirloin 6.55 8 8.50 4
Overall I want to be able to store all of my PRODUCT objects in an array (so I have an array of structs). So I attempted to allocate memory with a pointer, use a line count, and then send the pointer to a function called read. In read I add each struct to the array via a pointer. The program doesn't crash, the output is just not correct and I have no clue why not. It's prob something with pointers. If anyone could help me I would really appreciate it. Any help at all would be great.
//prototype
void read(pointerToArr);
typedef struct
{
char supType[15];
char prodName[15];
double wholePrice;
int quantWhole;
double retPrice;
int retProdQuantity;
}PRODUCT;
FILE *fr;
int lineCount = 0;
int main()
{
PRODUCT *ptr;
int i;
char check[50];
fr = fopen("ttt.txt", "r");
while(fgets(check, sizeof(check), fr)!= NULL)
{
if(check[0] != '\n')
{
lineCount++;
}
}
// allocate memory for array based on line count.
PRODUCT prodContainter[lineCount];
ptr = (PRODUCT*)malloc(sizeof(PRODUCT)*lineCount);
ptr = prodContainter;
read(ptr);
//print after adding to array via pointer from other
//function. this was a test.
for(i = 0; i < lineCount; i++)
{
printf("%s ", prodContainter[i].supType);
printf("%s ", prodContainter[i].prodName);
printf("%f ", prodContainter[i].wholePrice);
printf("%d ", prodContainter[i].quantWhole);
printf("%f ", prodContainter[i].retPrice);
printf("%d\n\n", prodContainter[i].retProdQuantity);
}
return 0;
}
void read(PRODUCT *pointerToArr)
{
// objective in this method is to read in data from the file, create an object for every line and
// then use the pointer array to add those objects to prodConstainer up above.
char supplyName[15];
char productName[15];
double wholeP = 0;
int quantityWhole = 0;
double retailPrice = 0;
int retailProductQuant = 0;
while(fscanf(fr, "%s %s %lf %d %lf %d", supplyName, productName, &wholeP, &quantityWhole, &retailPrice, &retailProductQuant) == 6)
{
PRODUCT record;
int i;
strcpy(record.supType, supplyName);
strcpy(record.prodName, productName);
record.wholePrice = wholeP;
record.quantWhole = quantityWhole;
record.retPrice = retailPrice;
record.retProdQuantity = retailProductQuant;
for(i = 0; i < lineCount; i++)
{
pointerToArr[i] = record;
}
}
fclose(fr);
}
You never rewind the file, so all the reading after you count the number of lines fails.
What you're printing is just what happens to be in memory.
There are many ways to fix this, of course.
Rewind the file, using rewind()
Close the file and let your read() function (whose name collides with a POSIX standard function, btw) re-open the file. This would also involve removing the scary global variable fr.
Re-structure so you never count the number of lines, by just reading and letting the ptr array grow as necessary (see realloc()).
Also, you should really avoid casting the return value of malloc() in C. This:
ptr = (PRODUCT*)malloc(sizeof(PRODUCT)*lineCount);
is better written as:
ptr = malloc(lineCount * sizeof *ptr);
This does away with the cast, and also uses sizeof on a value of the type pointed at to automatically compute the proper number of bytes to allocate.

Resources