Reputation: 87
Below is my code. I am receiving an error because I did not initialize bus -> BusStops, an array of pointers, after allocating memory to it. Each individual BusStop to be added to the bus -> BusStops array will have memory allocated to it in the AddBusStop method.
However, initially, the bus->BusStops in it. How do I initialize the bus -> BusStops array with that taken into account so that I don't receive this error?
I am receiving an Uinitialized value was created by heap allocation error on Valgrind and a segmentation fault
EDIT - I tried using calloc instead of malloc in my InitializeBus method, but alas, no luck
//ONE METHOD I WROTE
void InitializeBus(Bus *bus, int maxBusStops) {
bus -> numBusStops = 0; //initializing numBusStops element from struct to 0
bus -> maxBusStops = maxBusStops; //initializing maxBusStops element from struct to parameter maxBusStops
bus -> BusStops = (BusStop**)malloc(maxBusStops * sizeof(BusStop*)); //add memory to bus -> BusStops
}
//THE OTHER METHOD I WROTE
int AddBusStop(Bus *bus, const char* name) { //NOTE - the bus variable passed in is already initialized from the previous method. "name" is the name of the bus stop.
for(int i = 0; i < bus -> numBusStops; i++) { //for each bus stop that is currently in the array
if(strcmp(bus -> busStops[i] -> name, name) == 0) { //if that bus stop has the same name as the bus stop passed in as a parameter
return 1; //return 1
}
//ERROR ALERT - READ BELOW
// ***I am getting a segmentation fault error at this line. An uninitialized value was created using heap allocation** in the final line of the InitializeBus method. How do I initialize bus -> BusStops once I have added memory to it? Each individual BusStop that is being added has memory allocated to it in this method, but the bus -> busStops array is uninitialized and I am not sure what to initialize it to
} //OTHERWISE
BusStop *newBus = (BusStop*)malloc(sizeof(BusStop) + 1); //create a new BusStop pointer to be added to the array of BusStop pointers, bu. I am not sure if I am allocating enough memory
newBus -> name = (char*)name; //set the name of this newBus bus stop equal to the name passed in as 0
newBus -> numDestinations = 0; //initialize numDestinations to 0
newBus -> destinations = NULL; //set newBus -> destinations to NULL - I was specified to do this and can't change it
if(bus -> numBusStops == bus -> maxBusStops) { //if there is no more space in bu to add the newly created bus
bu = (BusStop**)realloc(bu, bus -> maxBusStops * 2 * sizeof(BusStop)); //allocate double the memory to bu
bus -> maxBusStops *= 2; //make sure maxBusStops is now doubled. I am not sure if I am allocating enough memory
}
bus->numBusStops++; //increase the amount of bus stops the bus can hold by 1
bu[bus -> numBusStops] = newBus; //add the newly created bus at the end of the array bu. **THIS LINE IS WHERE I AM GETTING THE "Invalid Write of Size 8" error
bus->busStops = bu; //update the struct element bus -> busStops and set it equal to the updated bu value
return 0; //return 0
}
//MAIN METHOD TO TEST CODE
int main() {
Bus bus; //create a new bus
InitializeBus(&bus, 10); //pass in its address and a maxBusStop value of 10
const char *london = "London"; //create a new char pointer called london
AddBusStop(&bus, london); //add the London Bus Stop to bus -> busStop
const char *paris = "Paris"; //create a new char pointer called Paris
AddBusStop(&bus, paris); //add the Paris Bus Stop to bus -> busStop
}
//STRUCTS
typedef struct Bus {
BusStop** BusStops; /*The array of all Bus stops managed by the bus -- this is an array of pointers, each with type BusStop*/
int numBusStops; /*The current number of bus stops managed by the company*/
int maxBusStops; /*The max number of bus stops the BusStop array can store*/
} Bus;
typedef struct BusStop{
char \* name; /\*The bus stop name\*/
int numDestinations; /\*The number of destinations the bus stop offers rides to\*/
struct BusStop \*\* destinations; /\*The array of destinations the bus stop offers rides to\*/
} BusStop;
Upvotes: 0
Views: 598
Reputation: 87
I figured it out!
My problem is in AddBusStop. I am adding the BusStop to the array AFTER incrementing numBusStops. Since arrays start at index 0, this is wrong.
Upvotes: 0
Reputation: 316
First of all, did you initialized bus ? You can’t get its field without initializing it. Second: Check if BusStops is of type BusStop**. Third: because BusStop is an array of pointer, if you want to put strings in it - You need to allocate every columns allocate BusStops[0], BusStops[1]...) with a for loop and then you can put string. Without allocating the column - your program won’t work when you will try to compare a string - The program will go to BusStops[i] but there will be no string/memory allocated.
Upvotes: 1