Problem either in allocation or handling array of pointer to struct - arrays

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);
...

Related

Segmentation fault error in function using 3D dynamic array

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...

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);

how to pass list in functions in C

I am new here and I need some help. I want to learn how can I pass a NULL list to a function fill it and then returning it to my main function.
FOR INSTANCE:
#include <cstdio>
typedef struct empNode {
char amka[12];
char first_name[30];
char last_name[30];
int year; //etos proslhpshs
float salary;
int num_of_children;
Child *children;
struct empNode *next;
} empNode;
void load_employees_from_file(empNode **employees);
//===========================================================
int main(void) {
empNode *employees = NULL;
load_employees_from_file(&employees);
while (employees != NULL) {
printf("%s ", employees->amka);
employees = employees->next;
}
}
//===========================================================
void load_employees_from_file(empNode **employees) {
FILE * fp;
int num_of_employees;
int i;
fp = fopen("employees.txt", "r");
if (fp == NULL) {
printf("Something went wrong, try again.\n");
return;
}
// here we read the first line of file to see how employee we have
fscanf(fp, "%d ", &num_of_employees);
while (num_of_employees) {
*employees = (empNode*) malloc(sizeof (empNode));
fscanf(fp, "%s ", (*employees)->amka);
fscanf(fp, "%s ", (*employees)->first_name);
fscanf(fp, "%s ", (*employees)->last_name);
fscanf(fp, "%d ", &(*employees)->year);
fscanf(fp, "%f ", &(*employees)->salary);
fscanf(fp, "%d\n", &(*employees)->num_of_children);
if ((*employees)->num_of_children > 0) {
(*employees)->children = (Child*) malloc(((*employees)->num_of_children) * sizeof (Child));
for (i = 0; i < (*employees)->num_of_children; i++) {
fscanf(fp, "%s ", (*employees)->children[i].fname);
strcpy((*employees)->children[i].lname, (*employees)->last_name);
fscanf(fp, "%d\n", &(*employees)->children[i].year_of_birth);
}
}
(*employees)->next = (empNode*) malloc(sizeof (empNode));
*employees = (*employees)->next;
num_of_employees--;
}
fclose(fp);
}
I get an error when I am running the while in my main function, more specifically my program crashes.
-------------------------------------------------------------------
ok let mu put it more clearly, after corrections i dont have problem with my list in the function the problem is that my list cant transfer to my main function. A clear example of this this:
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
void read_int(node **nn);
int main(void)
{
node *nn=NULL;
read_int(&nn);
printf("%d", nn->data);
return 0;
}
void read_int(node **nn)
{
FILE *fp;
fp=fopen("test.txt", "r");
fscanf(fp, "%d", (*nn)->data);
fclose(fp);
}
here is exactly the same problem and more easier to understund, the test.txt files contains just the number 2. but i cant print it in my main.
In your function void load_employees_from_file(empNode **employees)
Change :
(*employees)->next = (empNode*) malloc(sizeof (empNode));
to
(*employees)->next = NULL;
as immediately after that *employees becomes next and the while loop starts from the beginning where memory is dynamically allocated:
*employees = (empNode*) malloc(sizeof (empNode));
and then populated.
Otherwise, if this was the last employee, the next element remains the NULL pointer.
In this way, your main's while loop termination condition will be true when the last next element (pointing to NULL) is reached.
Skimming through your code, there seems to be several problems. You should learn to use a debugger, which would let you step through the code line-by-line during execution and observe the actual behavior.
Here are some obvious issues I think you should look at:
What do you think the value of *employees will be when the while loop exits? Specifically, think about this statement: *employees = (*employees)->next;
I assume you want this list to be NULL-terminated (otherwise, the while (employees != NULL) in your main function wouldn't make sense). In that case, where do you set the NULL terminator value? You don't seem to ever set the next field to NULL anywhere...
Why do you call malloc twice in the body of your loop? You seem to be allocating two different blocks of memory to represent the same employee.
I think #1 above is the reason that your program is crashing right now. (But if you fix that issue, your program will probably still crash due to one of the other issues.)
Right now, on the last iteration of your loop, you do this:
(*employees)->next = (empNode*) malloc(sizeof (empNode));
*employees = (*employees)->next;
This means that *employees (which points to employees from main) will always end up pointing to an uninitialized empNode struct. Since the memory is uninitialized, the next pointer will be garbage. That's definitely going to cause some problems.

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.

Reallocating memory for a struct array in C

I am having trouble with a struct array. I need to read in a text file line by line, and compare the values side by side. For example "Mama" would return 2 ma , 1 am because you have ma- am- ma. I have a struct:
typedef struct{
char first, second;
int count;
} pair;
I need to create an array of structs for the entire string, and then compare those structs. We also were introduced to memory allocation so we have to do it for any size file. That is where my trouble is really coming in. How do I reallocate the memory properly for an array of structs? This is my main as of now (doesn't compile, has errors obviously having trouble with this).
int main(int argc, char *argv[]){
//allocate memory for struct
pair *p = (pair*) malloc(sizeof(pair));
//if memory allocated
if(p != NULL){
//Attempt to open io files
for(int i = 1; i<= argc; i++){
FILE * fileIn = fopen(argv[i],"r");
if(fileIn != NULL){
//Read in file to string
char lineString[137];
while(fgets(lineString,137,fileIn) != NULL){
//Need to reallocate here, sizeof returning error on following line
//having trouble seeing how much memory I need
pair *realloc(pair *p, sizeof(pair)+strlen(linestring));
int structPos = 0;
for(i = 0; i<strlen(lineString)-1; i++){
for(int j = 1; j<strlen(lineSTring);j++){
p[structPos]->first = lineString[i];
p[structPos]->last = lineString[j];
structPos++;
}
}
}
}
}
}
else{
printf("pair pointer length is null\n");
}
}
I am happy to change things around obviously if there is a better method for this. I HAVE to use the above struct, have to have an array of structs, and have to work with memory allocation. Those are the only restrictions.
Allocating memory for an array of struct is as simple as allocating for one struct:
pair *array = malloc(sizeof(pair) * count);
Then you can access each item by subscribing "array":
array[0] => first item
array[1] => second item
etc
Regarding the realloc part, instead of:
pair *realloc(pair *p, sizeof(pair)+strlen(linestring));
(which is not syntactically valid, looks like a mix of realloc function prototype and its invocation at the same time), you should use:
p=realloc(p,[new size]);
In fact, you should use a different variable to store the result of realloc, since in case of memory allocation failure, it would return NULL while still leaving the already allocated memory (and then you would have lost its position in memory). But on most Unix systems, when doing casual processing (not some heavy duty task), reaching the point where malloc/realloc returns NULL is somehow a rare case (you must have exhausted all virtual free memory). Still it's better to write:
pair*newp=realloc(p,[new size]);
if(newp != NULL) p=newp;
else { ... last resort error handling, screaming for help ... }
So if I get this right you're counting how many times pairs of characters occur? Why all the mucking about with nested loops and using that pair struct when you can just keep a frequency table in a 64KB array, which is much simpler and orders of magnitude faster.
Here's roughly what I would do (SPOILER ALERT: especially if this is homework, please don't just copy/paste):
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
void count_frequencies(size_t* freq_tbl, FILE* pFile)
{
int first, second;
first = fgetc(pFile);
while( (second = fgetc(pFile)) != EOF)
{
/* Only consider printable characters */
if(isprint(first) && isprint(second))
++freq_tbl[(first << 8) | second];
/* Proceed to next character */
first = second;
}
}
int main(int argc, char*argv[])
{
size_t* freq_tbl = calloc(1 << 16, sizeof(size_t));;
FILE* pFile;
size_t i;
/* Handle some I/O errors */
if(argc < 2)
{
perror ("No file given");
return EXIT_FAILURE;
}
if(! (pFile = fopen(argv[1],"r")))
{
perror ("Error opening file");
return EXIT_FAILURE;
}
if(feof(pFile))
{
perror ("Empty file");
return EXIT_FAILURE;
}
count_frequencies(freq_tbl, pFile);
/* Print frequencies */
for(i = 0; i <= 0xffff; ++i)
if(freq_tbl[i] > 0)
printf("%c%c : %d\n", (char) (i >> 8), (char) (i & 0xff), freq_tbl[i]);
free(freq_tbl);
return EXIT_SUCCESS;
}
Sorry for the bit operations and hex notation. I just happen to like them in such a context of char tables, but they can be replaced with multiplications and additions, etc for clarity.

Resources