0

I have this dilemma. fruit is a struct.

fruit** func2create(const int N, const int M)
{
    fruit **arr;
    arr = new fruit*[N];

    for(int i = 0; i < N; i++)
    {
        arr[i] = new fruit[M];
    }

    return arr;
}

This function returns a pointer pointing to a 2d array, at least that is what I think it does.

Now, having this function, how do I actually make a 2d array using it?

7
  • "how do I actually make a 2d array using it" What do you mean? It already returns (more or less) a 2D array. Commented Jan 9, 2022 at 11:38
  • 1
    Unless you're practicing the use of new and delete (or writing a custom container or smart pointer), you shouldn't use them at all. Returning a 1D std::vector would make your life a lot easier. Commented Jan 9, 2022 at 11:39
  • Well, I know but this is a function, so i cannot use "arr" that was created inside of it outside of it for further operations, bcs of scope. How to assign this "arr" created by it to some other name in main? And yes, I am using new and delete (this is not my choice tho). Commented Jan 9, 2022 at 11:44
  • Exactly the same as you'd do with any other function. Compare with int foo() {int x = 42; return x;}. How do you use (the copy of) x outside of the function? By writing something like int y = foo();, then using y. And here, you do fruit **a = func2create(10, 20);, then use a instead of arr. Commented Jan 9, 2022 at 11:48
  • That's also what I have figured out, but I am not sure about this solution. I am also ordered to code a funciton that will delete my new 2d array. And I have to pass a 2d array as a parameter. I do not know wheather making a funciton funcDel(**arr) and passing **a to it, will be viewed as good solution. Commented Jan 9, 2022 at 11:58

2 Answers 2

1

I wouldn't use the notation with double pointers. It is sure to result in a memory leak somewhere since it is not clear who will cleanup the memory. In C++ I would use one of the following methods (using either std::array, or std::vector). Which is also more in line with the C++ core guidelines regarding the use of new/delete (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines look for anything about pointers and/or new/delete)

#include <array>
#include <vector>

struct fruit
{
};

// sizes know at compile time then use a function template 
template<std::size_t N, std::size_t M>
auto func2create()
{
    // create initialized array
    std::array<std::array<fruit, M>, N> fruits{};
    return fruits;
}

// for runtime sizes use std::vector
// note I don't use ints (they can have negative values
// I don't want to test for that)
auto func2create(const std::size_t n, const std::size_t m)
{
    // create n rows of m fruits
    std::vector<std::vector<fruit>> fruits(n, std::vector<fruit>(m));
    return fruits;
}

int main()
{
    // compile time allocate fruits
    {
        auto fruits = func2create<4, 4>();
        auto fruit = fruits[1][1];
    }

    // runtime alloacted fruits
    {
        auto fruits = func2create(4ul, 4ul);
        auto fruit = fruits[1][1];
    }
    
    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I have read your reply just seconds after running my code for deleting this array. And as you said it has resulted in a memory leak. Unfortunately I have to stick to what my assignment requires of me.
I appreciate you having to stick with your assignement. Could you ask your teacher if he's aware of the C++ core guidelines for me? It could still be he's just teaching how hard it is to get manual memory managment right :) (Note to teacher : I have almost 30 years of C++ experience)
0

For example:

int N = ...;
int M = ...;
fruit** arr = func2create(N, M);

for(int i = 0; i < N; ++i) {
    for(int j = 0; j < M; ++j) {
        // use arr[i][j] as needed...
    }
}

... 

for(int i = 0; i < N; ++i) {
    delete[] arr[i];
}
delete[] arr;

8 Comments

Well it seems to be exactly what I have done now. And it works... well kind of. Delete function does not clear everything for some reason.
@Nemo Please clarify what you mean by "Delete function does not clear everything...".
For instance, instead of 0, it "assgins" the value of 4.59135e-41.
@Nemo You can't access the memory after it's been deleted -- that's undefined behaviour.
@Nemo you can't show it. Like GM said, accessing the contents of freed memory is undefined behavior. The correct thing to do is not use the memory at all anymore once it has been freed. But clearly you are, hence the bad output, so you need to fix that in your code. But you haven't shown that code yet, so nobody can help you fix it. So please update your original question with more details.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.