I want to create an array of struct pointers and set each pointer to null. Eventually I want to have the pointers in the array point to my struct. I believed I had the write code to this but I keep getting a seg fault.
Code:
//struct in my .h
struct bin{
double cac; // current available capacity
struct node *list //pointer to linked list... bin struct points to linked list struct
}
//main file
void main(){
struct bin *bArray[20];
struct bin *binTemp, *pTemp;
for(i=0;i<20;i++) bArray[i]= NULL;
}
I assumed this would create an array of bin pointers, but I'm getting a seg fault here. Shouldn't I be able to make all the pointers NULL regardless of what type of pointer they are?
Eventually I want to make these all point to bin structs and I thought I could do this without having to make the pointers NULL first so i tried:
for(i=0;i<20;i++){
binTemp = (struct bin *)malloc(sizeof(struct bin));
binTemp->cac = 1.0;
binTemp->list = NULL;
bArray[i] = binTemp;
}
Once again I got a seg fault. I have no idea whats going on here. I know seg faults means I'm trying to write to an illegal memory location which makes me think I would have to set the size of the array indexes with malloc. However,
for(i=0;i<20;i++) bArray[i]= malloc(sizeof(struct bin));
also gives me a seg fault. I have no idea what I'm doing wrong.
Actual code I ran:
//Header File
#ifndef hBin_h
#define hBin_h
#include <stdio.h> // stdio used for file io
#include <stdlib.h> // standard c library
#define MAX_S 20
//bin struc
struct bin {
double cac; //current available capacity
struct node *list; // pointer to linked list
};
//linked list node struct
struct node{
char *name; //name of item
double size; // weight of item
struct node *next; //pointer to next
};
//insert the new item into a node in its appropriate location using alphabetical ordering of item names
struct node *oInsert(char *item, double size, struct node *head);
//print the items of the list out along with the list’s capacity
void traverse(struct node *head);
// deallocate the nodes of the list
void destory(struct node *head);
//input info from file - name of object, weight of object
void input(FILE *inFile, char item[], double *weight);
#endif // hBin_h
#include "hBin.h"
void main(){
FILE *inFile;
char *item;
double *weight;
struct bin *bArray[20];
int i;
struct bin *binTemp, *pTemp;
inFile = fopen("run1.txt", "r"); //open file
printf("HERE1\n");
for(i=0;i<20;i++){
binTemp = (struct bin *)malloc(sizeof(struct bin));
binTemp->cac = 1.0;
binTemp->list = NULL;
bArray[i] = binTemp;
}
/*while(!feof(inFile)){
input(inFile, item, weight);
printf("%s, %.2f\n", item, *weight);
}*/
}
I used gdb (not totally sure what I'm doing here):
(gdb) run
Starting program: /home/Christopher/CSC362/Pass_F/Main
[New Thread 813244.0xc7590]
[New Thread 813244.0xc6ce0]
[New Thread 813244.0xc7320]
[New Thread 813244.0xc5994]
HERE1
0 [main] Main 813244 cygwin_exception::open_stackdumpfile: Dumping stack trace to Main.exe.stackdump
[Thread 813244.0xc7320 exited with code 35584]
[Thread 813244.0xc6ce0 exited with code 35584]
[Inferior 1 (process 813244) exited with code 0105400]
(gdb) where
No stack.
(gdb) for(i=0;i<20;i++){
binTemp->cac = 1.0;
binTemp->list = NULL;
bArray[i] = binTemp;
} /usr/src/debug/cygwin-2.2.1-1/winsup/cygwin/crt0.c: No such file or directory.
(gdb) binTemp = (struct bin *)malloc(sizeof(struct bin));
Undefined command: "binTemp". Try "help".
(gdb) binTemp->cac = 1.0;
Undefined command: "binTemp->cac". Try "help".
(gdb) binTemp->list = NULL;
Undefined command: "binTemp->list". Try "help".
(gdb) bArray[i] = binTemp;
Undefined command: "bArray". Try "help".
(gdb) } for(i=0;i<20;i++){
Undefined command: "". Try "help".
(gdb) binTemp = (struct bin *)malloc(sizeof(struct bin));
Undefined command: "binTemp". Try "help".
(gdb) binTemp->cac = 1.0;
Undefined command: "binTemp->cac". Try "help".
(gdb) binTemp->list = NULL;
Undefined command: "binTemp->list". Try "help".
(gdb) bArray[i] = binTemp;
Undefined command: "bArray". Try "help".
(gdb) } for(i=0;i<20;i++){
binTemp->cac = 1.0;
Undefined command: "". Try "help".
(gdb) binTemp = (struct bin *)malloc(sizeof(struct bin));
Undefined command: "binTemp". Try "help".
(gdb) binTemp->cac = 1.0;
Undefined command: "binTemp->cac". Try "help".
(gdb) binTemp->list = NULL;
Undefined command: "binTemp->list". Try "help".
(gdb) bArray[i] = binTemp;
Undefined command: "bArray". Try "help".