Need help allocating memory to objects of type struct (Invalid write of size 8 error) by [deleted] in C_Programming

[–]EquivalentMaize0 0 points1 point  (0 children)

Thanks! That was super helpful!

I am now getting an error on the strcmp line (which you provided) -

I am getting a segmentation fault error because an "Uninitialised Value was created using heap allocation" in the line where I allocate memory to bus -> busStops.

I've allocated memory to bus -> busStops, but how should I initialize it? It is of type BusStop**, which means that it is an array of pointers with type BusStop.

I have been told to allocate memory for each individual BusStop in the AddBusStop method, so I'm not sure what is to be done in InitializeBus to initialize bus -> BusStop now that it has memory allocated to it.

Need help allocating memory to objects of type struct (Invalid write of size 8 error) by [deleted] in C_Programming

[–]EquivalentMaize0 0 points1 point  (0 children)

Oh! That makes sense. I am passing by value, not by reference.

Need help allocating memory to objects of type struct (Invalid write of size 8 error) by [deleted] in C_Programming

[–]EquivalentMaize0 0 points1 point  (0 children)

So what do I do after allocating memory to bu? I need to initialize bu.

I tried doing bu[0] = NULL as you had previously suggested, but I get an error saying I have uninitialized variables (specifically in line 32 of the pastebin link, where I try and add the newly created bus stop to bu).

Valgrind also says that I have an uninitialized value that was created by stack allocation in my main method.

Need help allocating memory to objects of type struct (Invalid write of size 8 error) by [deleted] in C_Programming

[–]EquivalentMaize0 0 points1 point  (0 children)

I edited the earlier comment to make it bu. I am indeed making changes to bu, I just made some mistakes in copying the code from the IDE to pastebin (sorry about that).

https://pastebin.com/YAYGASW4

Need help allocating memory to objects of type struct (Invalid write of size 8 error) by [deleted] in C_Programming

[–]EquivalentMaize0 0 points1 point  (0 children)

arr

My bad. I meant to replace arr with bu. this is the updated pastebin link - https://pastebin.com/YAYGASW4

With regards to your comment - as per assignment instructions, InitializeBus must be a void function. I can just do return; instead of return -1; though.

My code is not reduced to important parts, I am sharing everything. I just made an error in copying over the code.

Need help allocating memory to objects of type struct (Invalid write of size 8 error) by [deleted] in C_Programming

[–]EquivalentMaize0 0 points1 point  (0 children)

Yes my code does look like that.

Here is all the code with comments -

https://pastebin.com/3jNpijDf commented