I am new to C++ programming and want to try out manual memory management (I am aware that it is advised not to use manual memory management, but I still want to give it a try).
My goal was to write a function that can append an element to an array by using realloc.
I use this struct:
struct pathToCity{
int lengthCities;
char cities[100];
};
Here is my function:
void appendToPathToCitiesArray(pathToCity **array, int *length, pathToCity newCity) {
if (*length == 0) {
*array = (pathToCity*) malloc(sizeof(pathToCity));
}
else {
*array = (pathToCity*) realloc(*array, (*length + 1) * sizeof(pathToCity));
}
if (array == NULL) {
cout << "Error allocating memory." << endl;
exit(-1);
}
(*array)[*length] = newCity;
(*length)++;
}
Here I define an array:
int lengthQueue = 0;
pathToCity *queue;
Here I instantiate an instance of my struct:
pathToCity newPath = {0, ""};
Finally I use my function:
appendToPathToCitiesArray(&queue, &lengthQueue, newPath);
This function works fine as it is using double pointers. According to my research (unfortunately I cannot find the stackoverflow post where this was explained) double pointers here are necessary, because once a pointer gets passed to a function, it is impossible to change the pointer inside the function. Therefore double pointers are required once I want to use realloc to update my array.
But I have also tried this in a different file:
#include <iostream>
using namespace std;
struct shortText {
int lengthText;
char text[3];
};
void append(shortText *array, int *length, shortText newCity) {
if (*length == 0) {
array = (shortText*) malloc(sizeof(shortText));
}
else {
array = (shortText*) realloc(array, (*length + 1) * sizeof(shortText));
}
if (array == NULL) {
cout << "Error allocating memory" << endl;
exit(-1);
}
array[*length] = newCity;
(*length)++;
}
int main() {
int length = 3;
shortText *arr = (shortText*) malloc(sizeof(shortText) * length);
arr[0] = {2, "ab"};
arr[1] = {2, "ab"};
arr[2] = {2, "ab"};
shortText s = {2, "ab"};
append(arr, &length, s);
arr[3] = {8, "ji"};
cout << length << endl;
cout << arr[3].lengthText << endl;
free(arr);
}
It also works perfectly and does not use double pointers. Why are double pointers necessary in one case and why not in the other?
If you require any more information, please ask. I have not provided the entire first code, to give a better overview, if I have missed out an important part, I will post the rest of the code.
std::coutis C.std::vectorclass if you want to know how to handle dynamically allocated memory properly in C++. What you have done is as mentioned, just plain oldC. Plus none of this even works if you had tried this ifpathToCitycontained astd::stringor another non-trivially-copyable type.struct pathToCity{ int lengthCities; std::string cities; };-- Now all of thosemallocandrealloccalls are worthless, as they will invoke undefined behavior.malloc. but they are definitely in C style