简体   繁体   中英

Return array from function in c++ through pointer, but how does the program know the array size?

For example, I have a function as below.

int* function(int a)
{
    int b = 2;
    int *c = new int[b];
    return c;
}

My main function calls it like this:

int main()
{
    int *array = function(123);
    return 0;
}

However, I have no idea the array length of array because I passed a pointer. Apparently, sizeof(array)/sizeof(*array) does not work for pointer array. Therefore, I cannot display or use the array because I don't know the size of it. Is there anyway to let the function return both the size (or calculate it) and the array? Thanks!

Please recommend the remedy before providing alternative solutions such as using a vector.

Your question makes perfect sense because the C++ runtime must know how big the array that was allocated is.

However while it's true that the runtime given a pointer can find the size of the memory block allocated with new[] and the number of elements of the allocated array (at least if destruction requires code) there is no portable way to get this information with C++ code.

The standard solution is to just return an std::vector instance instead that is slightly more that a pointer and a size (a very common implementation is just a pointer, a size for the used part and a size for the available extra memory for new elements that wouldn't require a reallocation).

You are in C++, use a class, like std::tuple . Anyway, raw pointers are frowned upon, better use a standard container like std::vector if possible.

If you really want to do neither, you must pass the length some other way, by using a pointer or a reference for example. You might instead make use a sentinel value or make the length contractual though.

As some seem to think the C++ runtime must have a secret marker providing the type and element count originally asked for, just to be clear: Most common implementations save neither for most cases, and only the element count for allocations using new[] of a type having non-trivial destructor or a custom new[] delete[] -pair demanding the element count.

For the specific problem, you would pass a pointer or a reference to an integer as well to the function, that would be updated by the function to store the size of the array. It's used often in library design.

int* function(int a, int* size)
{
    int b = 2;
    *size = b;
    int *c = new int[b];
    return c;
}

int main()
{
    int sizeofarray;
    int *array = function(123, &sizeofarray);
    return 0;
}

*PS Deduplicator is right about using standard containers :-)

A trivial solution could be

struct array
{
    int *data;
    int  size;
};

array function(int a)
{
    int b = 3;
    int *c = new int[b];
    array arr = { c, b };
    return arr;
}

int main()
{
    array arr = function(123);
    std::cout << arr.size;
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM