Home > Back-end >  Function does not return byte array as expected in Visual Studio
Function does not return byte array as expected in Visual Studio

Time:12-12

I'm having an issue retuning a byte array in Visual Studio. The following code outputs as expected in online compiler https://www.onlinegdb.com/

The expected output is an array of 8 bytes: BB-CC-C3-02-5C-11-6D-00

And the online compiler outputs the same as expected:

*******************************************************************************/
#include <stdio.h>
#include <stdint.h>

uint8_t * createByteArray(float power, int power_coefficient);

int main()
{

    float power = 4444;
    int power_coefficient = 1;
    
    
    uint8_t * returned_ptr = createByteArray(power, power_coefficient);
    
    for (int i = 0; i < returned_ptr[3] 6; i  )
        printf("X-", returned_ptr[i]);

    return 0;
}

uint8_t * createByteArray(float power, int power_coefficient)
{

    uint16_t power_ushort = (uint16_t)(power * power_coefficient);
    uint8_t bytes_power[2];
    bytes_power[0] = (uint8_t)((power_ushort >> 8) & 0xFF);
    bytes_power[1] = (uint8_t)(power_ushort & 0xFF);
    uint8_t firstHalf_power = bytes_power[0];
    uint8_t secondHalf_power = bytes_power[1];


    int parity = (int)firstHalf_power   (int)secondHalf_power;
    uint16_t parity_ushort = (uint16_t)(parity);
    uint8_t bytes_parity[2];
    bytes_parity[0] = (uint8_t)((parity_ushort >> 8) & 0xFF);
    bytes_parity[1] = (uint8_t)(parity_ushort & 0xFF);
    uint8_t firstHalf_parity = bytes_parity[0];
    uint8_t secondHalf_parity = bytes_parity[1];

    uint8_t telegram_set_power[8] = {0xBB, 0xCC, 0xC3, 0x02, secondHalf_power, firstHalf_power, secondHalf_parity, firstHalf_parity};
    
    uint8_t * ptr = telegram_set_power;
    
    return ptr;

}

But in Visual Studio 2022, the same code returns the following output:

AA-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-00-00-00-00-00-00-00-00-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-A8-00-00-00-00-00-00-00-A8-00-00-00-00-00-00-00-45-64-DD-8D-FA-7F-00-00-00-00-00-00-00-00-00-00-64-00-1B-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-36-9D-EA-8D-FA-7F-00-00-A0-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-CC-F6-11-AE-2C-00-00-00-10-00-00-00-00-00-00-00-D0-F5-11-AE-2C-00-00-00-1C-00-1B-00-00-01-00-00-E0-F5-11-AE-2C-00-00-00-14-00-00-00-00-00-00-01-00-00-00-00-00-00-00-00-00-00-

I have been trying to fix it but failed. What could be the reason for this?

Edit: This happens when I compile as 64 bit. When I compile under x86 it works. But I need to compile as x64 for some other reasons.

CodePudding user response:

Your function createByteArray is returning a dangling pointer, i.e. a pointer to an object that no longer exists when the function returns.

The array telegram_set_power is a local array. Therfore, its lifetime ends when the function createByteArray returns. The function createByteArray is returning a pointer to this no longer existant array.

Dereferencing the dangling pointer returned_ptr in the function main will invoke undefined behavior. This explains why your program appears to work on one platform, but doesn't work on another platform.

In order to solve this problem, you have the following options:

  1. Declare the array inside the function main instead of inside the function createByteArray and pass a pointer to this array to the function createByteArray. In that case, it would probably be appropriate to rename the function createByteArray to initByteArray, since it no longer creates the array, but rather only initializes it.

  2. In the function createByteArray, instead of making telegram_set_power a local array whose lifetime ends when the function returns, you could allocate memory for the array using the function malloc. That way, you have full control of the lifetime of the array. Its lifetime will only end when you call free. Therefore, it is safe to return a pointer to such an object.

Generally, solution #1 is the easier solution, because if you use solution #2, you will have to remember to call free when you no longer need the memory. Otherwise, you will have a memory leak.

See the following question for more information on how to return an array in C:

Returning an array using C

  • Related