5

I can't find a way to return an array without dynamic memory allocation. I will explain it in detail using an example:
With dynamic memory allocation:

Device* getConnectedDevices() {
    // scan for devices.
    Device *connectedDevices = new Device[deviceCount]; // Dynamically allocate memory for the Devices
    // Get for each device an ID
    return connectedDevices;
}
void doSomething() {
    // some code
    // I need a list with all the connected devices!!
    Device *connectedDevices;
    connectedDevices = getConnectedDevices();
    // do something with the array
}

doSomething() don't know the size of the array, so i used a struct to solve that problem:

struct deviceArray {
    Device* devices;
    int deviceCount;
};

Without dynamic memory allocation: I have no idea how to do this. I tried the following things:

  • Pass by reference. Problem: we don't know the array size before scanning.
  • Return without dynamic memory allocation (local variable) . Problem: the object no longer exist (of course).
5
  • 6
    Don't allocate anything manually, in c++ just use std::vector or similar classes. Commented Jul 9, 2017 at 11:46
  • 1
    returning a local pointer is always a disaster as address of that is not valid, upon exit of function. Hence, dynamic allocation is required Commented Jul 9, 2017 at 11:49
  • 1
    std::vector uses dynamic memory allocation. The difference is that your function doesn't need to explicitly manage dynamically allocated memory - a vector manages its own memory. Commented Jul 9, 2017 at 11:50
  • 1
    @dlmeetei With new the memory is allocated on heap, so it would be valid to return the pointer of that allocation. If the object would be created on stack and the address would be returned using & then it would be invalid. Commented Jul 9, 2017 at 11:54
  • 2
    You can't avoid dynamic memory because only dynamic memory lasts longer than the function. Commented Jul 9, 2017 at 12:05

2 Answers 2

10

In general it is not possible to do so without dynamic memory allocation.
The problems that arise with that (keeping track of the size, correct deallocation, etc.) are common.

These problems you describe are solved in c++ using container classes and dynamic memory management smart pointers.

In your case it would be appropriate to use a std::vector<Device> to represent the list of devices and hide the details and complications of dynamic memory management from you.

Sign up to request clarification or add additional context in comments.

Comments

3

You don't need to new anything for this,

std::vector<Device> getConnectedDevices()
{
    std::vector<Device> devices;
    // Fill `devices' with devices.push_back()
    return devices;
}

you can iterate through the list of devices in many different ways, usng iterators for instance.

Normally you don't need pointers at all in c++, if you know how to use references and STL containers, you can do anything you want without a single pointer. There are cases however where it does make sense to use pointers but I doubt this is one of such.

5 Comments

address will not be valid upon exit of function
@dlmeetei No address involved in this code at all. That's only when you return a pointer to a local variable, for example return &devices would be a problem.
local variable comes into existence upon entering the function and life is ended upon exit of function
@IharobAlAsimi "There will be copying involved." I highly doubt so. I'd count on RVO move.
Even is there was no RVO it would be a move rather than a copy.

Your Answer

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