1

I call a function that returns a pointer twice when I want to read the results, the first one is overwritten with the second

int *ip1Array = new int[4]; 
int *ip2Array = new int[4];

ip1Array = ipSplit(4);
ip2Array = ipSplit(2);

int *ipSplit(int ip)
        {
            static int ipTab[4];

            for (int i = 0; i <= ip; i++) {
                ipTab[i] = ip
            }

            return ipTab;
        }

This is just a piece of code in a very simplified sense. The problem is that the result of the first function call is overwritten by its second call. How to fix it?

7
  • Don't define ipTab static? Commented Feb 4, 2022 at 19:16
  • 2
    @mmomtchev but that won't work well either?? Commented Feb 4, 2022 at 19:17
  • 4
    @mmomtchev ... and return pointer to a local variable? Commented Feb 4, 2022 at 19:17
  • Are you not permitted to use a vector? Commented Feb 4, 2022 at 19:20
  • You have to allocate, there is no magical solution to this - if you want to return a pointer from a reentrant function, you must allocate memory - or pass an array from the caller. Don't waste your time - there is no elegant solution to this - look at all the libc functions. Commented Feb 4, 2022 at 19:21

2 Answers 2

6
int *ip1Array = new int[4]; 

This defines a pointer. You've initialised it to point to a dynamic array.

ip1Array = ipSplit(4);

This assignment overwrites the previous pointer value. Since that previous value was the only pointer to the dynamic array, nothing points to it anymore, and it can no longer be deleted. This is called a memory leak. Don't do this.


static int ipTab[4];

static means that the variable is same across all invocations of the function. Every call to the function will return a pointer to (first element of) the same array.

ip1Array = ipSplit(4);
ip2Array = ipSplit(2);

Both of these arrays point to the (first element of) same array that is returned by the function. And both of the dynamic arrays have leaked.


ip1Array = ipSplit(4);

You call the function with argument 4.

static int ipTab[4];

The array has 4 elements.

for (int i = 0; i <= ip; i++) {

You access through the indices 0,1,2,3,4. Count them. You access 5 different indices. You access outside the bounds of the array, and the behaviour of the program is undefined. Don't do this.


How to fix it?

  • Don't use owning bare pointers.
  • Don't access arrays outside of their bounds.
  • If don't want all arrays to be the same, then don't use static.

Since the sizes of the arrays are constant and small, you could use std::array:

auto
ipSplit(int ip)
{
    assert(ip <= 4);
    std::array<int, 5> ipTab{};
    for (int i = 0; i <= ip; i++) {
        ipTab[i] = ip
    }
    return ipTab;
}

// usage
auto ip1Array = ipSplit(4);
auto ip2Array = ipSplit(2);
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the hint. unfortunately if I type as you do std :: array <int, 5> ipTab {}; - compiler says incomplete type not alowed
That probably means you need to #include <array>
Also, make sure you are compiling for C++11 or later.
3

It isn't overwritten, both pointers point to the same thing :

static int ipTab[4];

Comments

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.