1

As a homework I have to write a program that operates around structures. As a first task i have to write a function that allocates memory for an array of N pointers that point to new structures(user decides about the value of N) and returns the adress of an array. The first problem that i have is understanding the form of malloc. I asked my professor what would be the equivalent by using "new" because it is more transparent for me but he answered that I should stick to malloc so i avoid making any mistakes. The following function looks like this:

struct Structure
{
    int a;
    char b;
    float c;
};

Structure** allocating(int N)
{
    struct Structure** tab = (struct Structure**) malloc(amount * sizeof(struct Structure*));
    for (int i = 0; i < N; i++)
    {
        tab[i] = (struct Structure*) malloc(sizeof(struct Structure));
    }
    return tab;
}

I have tried understanding this form of allocating memory but so far i understand this as if i was allocating memory for a pointer pointing to the array of pointers(double **) which is not what was stated in a task. To sum up, i don't understand the way the allocating is written and how could it be written by using new.

Towelyey
  • 57
  • 4
  • `(struct Structure**)malloc(amount * sizeof(struct Structure*))` -> `new Structure*[amount];` – Fureeish Oct 05 '19 at 19:02
  • 1
    Prefer `std::vector` if you are allowed to use it. If not, strongly consider wrapping your allocation in another structure to [automate its management](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii). In general [avoid directly using `new`](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new). – user4581301 Oct 05 '19 at 19:07
  • Do you really have to allocate memory? You could use vectors to avoid the whole task of memory allocation and deallocation. The whole function could be replaced by `std::vector allocating(int N) { return std::vector(N); }`. – Thomas Sablik Oct 05 '19 at 19:09
  • 1
    "I should stick to malloc so i avoid making any mistakes" literally the opposite advice he/she should have given you. Don't use `malloc`, don't use `new`. Use containers and RAII. – bolov Oct 05 '19 at 19:15
  • 1
    *I asked my professor what would be the equivalent by using "new" because it is more transparent for me but he answered that I should stick to malloc so i avoid making any mistakes.* Be careful with this. `malloc` has a number of failure case you have to watch for that `new` does not. The big ones are probably 1) All calls to `malloc` need to be explicitly tested for success while `new` throws an exception on failure, making it much harder to miss. 2) `malloc` does not call constructors and `free` does not call destructors. They can only be used on simple datatypes. – user4581301 Oct 05 '19 at 19:15
  • @user4581301 technically speaking using `malloc` without in-place new is UB as no object is created. It's a whole thing/mess regarding the start of life of objects. – bolov Oct 05 '19 at 19:17
  • Agreed. I took placement `new` out of the comment because I was running out of characters. – user4581301 Oct 05 '19 at 19:19

1 Answers1

0

Your C code allocates an array of pointers to Structure. I have no idea why it is done this way instead of simply allocating an array of Structure, you have to ask the author of the task for that.

Direct equivalent using new would look like this :

Structure** allocating(int N)
{
    struct Structure** tab = new Structure*[N];
    for (int i = 0; i < N; i++)
    {
        tab[i] = new Structure;
    }
    return tab;
}

Deallocating can be a bit tricky:

void deallocating(Structure** tab, int N)
{
    for (int i = 0; i < N; i++)
    {
        delete tab[i]; //delete objects
    }
    delete[] tab; //delete array itself, notice the [] after delete!
}

If you can move to array of objects instead of array of pointers, the code gets much simpler:

Structure* allocating(int N)
{
    return new Structure[N];
}

Later you also deallocate it with delete[]


As noticed in comments, in C++ we prefer to not use memory management directly (especially in modern C++ keyword new is considered to be a bad smell). std::vector can handle all your array-on-the-heap needs perfectly well, it will never leak memory and can resize itself easily.

Yksisarvinen
  • 13,037
  • 1
  • 18
  • 42