C Array probelm showing different result on different platforms

Viewed 104

Problem statement:- There are n kids with candies. You are given an integer array candies, where each candies[i] represents the number of candies the ith kid has, and an integer extraCandies, denoting the number of extra candies that you have.

Return a boolean array result of length n, where result[i] is true if, after giving the ith kid all the extraCandies, they will have the greatest number of candies among all the kids, or false otherwise.

Note that multiple kids can have the greatest number of candies.

Input: candies = [12,1,12], extraCandies = 10 Output: [true,false,true]

for the above problem statement i am writing this below function:-

bool* kidsWithCandies(int* candies, int candiesSize, int extraCandies, int* returnSize) {
    *returnSize = candiesSize;
    //  printf("%d ", *returnSize);
    int *tempcandie = (int*)malloc(sizeof(int)*candiesSize);
    int it1 = 0, it2, temp = 0;
    bool result;
    for (it1;it1 < candiesSize;it1++)
    {
        result = 1;
        tempcandie[it1] = candies[it1] + extraCandies;
        for (it2 = 0;it2 < candiesSize;it2++)
        {
            if (tempcandie[it1] <= candies[it2])
            {
                result = 0;
                break;
            }
        }

        tempcandie[it1] = result;


    }
    return (bool*)tempcandie;
}

if i run this function in my PC in visual studio then i debugger window i can see the tempcandie is holding expected result but if i am running on the one specific coding platform its showing different result what might be the issue

Visual basic debugger window :- enter image description here

Platforms output:-

enter image description here

I am unable to figure out why i am getting difference in output

2 Answers

You created an array of integers

int *tempcandie = (int*)malloc(sizeof(int)*candiesSize)

but return a pointer to the first element of the array as having the type _Bool (bool is an alias for the type _Bool). Objects of the type int and of the type _Bool can have different sizes. This is the reason of the incorrect output.

Here is a demonstrative program

#include <stdio.h>

int main(void) 
{
    printf( "sizeof( int ) = %zu\n", sizeof( int ) );
    printf( "sizeof( _Bool ) = %zu\n", sizeof( _Bool ) );
    
    return 0;
}

The program output might look like

sizeof( int ) = 4
sizeof( _Bool ) = 1

Also this function parameter int* returnSize is redundant. And the first parameter should have the qualifier const.

The function can be defined the following way as it is shown in the demonstrative program below.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

bool * kidsWithCandies( const int *candies, size_t candiesSize, int extraCandies )
{
    bool *result = NULL;
    
    if ( candiesSize )
    {
        result = malloc( candiesSize * sizeof( bool ) );
    
        if ( result )
        {
            size_t max = 0;
        
            for ( size_t i = 1; i < candiesSize; i++ )
            {
                if ( candies[max] < candies[i] ) max = i;
            }
            
            for ( size_t i = 0; i < candiesSize; i++ )
            {
                result[i] = candies[max] - candies[i] < extraCandies;
            }
        }           
    }
    
    return result;
}

int main(void) 
{
    int candies[] = { 12, 1, 12 };
    const size_t N = sizeof( candies ) / sizeof( *candies );
    
    int extraCandies = 10;
    
    
    bool *result = kidsWithCandies( candies, N, extraCandies );
    
    if ( result )
    {
        for ( size_t i = 0; i < N; i++ )
        {
            printf( "%s ", result[i] ? "true" : "false" );
        }
        
        putchar( '\n' );
    }
    
    free( result );
    
    return 0;
}

The program output is

true false true

Never ever use pointers to pun the types.

#include <stdbool.h>
#include <stdio.h>

int main(void) {
    printf("%zu, %zu\r\n", sizeof(bool), sizeof(int));
}

The result is 1,4. VS19 result: enter image description here

You need to

 int *tempcandie = malloc(sizeof(*tempcandie)*candiesSize);
 bool *result = malloc(sizeof(*result)*candiesSize);

 /* ... */

  for(int i = 0; i < candiesize; i++) result[i] = !!tempcandie[i];
Related