Error in vector calculation in C

Asked

Viewed 42 times

0

I’m refactoring my code to make it more dynamic, but by including the line int n = sizeof(vetor)/sizeof(int); within the function bubble it has failed to function properly. Where am I missing?

Modified Code

void bubble(int vetor[])
{
    int n = sizeof(vetor)/sizeof(int);
    int k = n;
    int aux;

    for(int i=0; i<n; i++)
    {
        for(int j=0; j<k; j++)
        {
            if(vetor[j]>vetor[j+1])
            {
                aux=vetor[j];
                vetor[j]=vetor[j+1];
                vetor[j+1]=aux;
            }
        }
        k--;
    }
}

int main()
{
    int vetor[]={10,3,5,8,1,9,2,4,7,0,6,-1};
    int n=sizeof(vetor)/sizeof(int);

    bubble(vetor);

    printf("\n\n\n");

    for(int i=0;i<n;i++)
    {
        printf("%d\n",vetor[i]);
    }

    printf("\n\n\n");

    system("pause");
    return 0;
}

Original Code

void bubble(int vetor[],int n)
{
    int k = n;
    int aux;

    for(int i=0; i<n; i++)
    {
        for(int j=0; j<k; j++)
        {
            if(vetor[j]>vetor[j+1])
            {
                aux=vetor[j];
                vetor[j]=vetor[j+1];
                vetor[j+1]=aux;
            }
        }
        k--;
    }
}

int main()
{
    int vetor[]={10,3,5,8,1,9,2,4,7,0,6,-1};
    int n=sizeof(vetor)/sizeof(int);

    bubble(vetor,n);

    printf("\n\n\n");

    for(int i=0;i<n;i++)
    {
        printf("%d\n",vetor[i]);
    }

    printf("\n\n\n");

    system("pause");
    return 0;
}

1 answer

3


Because when you use an array as a parameter in a function, in this case the compiler interprets it as a pointer. That is to say:

void bubble(int vetor[])

That’s the equivalent of:

void bubble(int *vetor)

And there the sizeof(vetor) becomes sizeof(int *) and will give you 8, regardless of how many elements there are in the vector.

The reason for this is that the sizeof is solved at compile time, and corresponds to the amount of bytes in memory that will be allocated to store the contents of a variable. It is not determined at runtime. In the case of the original code, the compiler can easily figure out how much memory it will need to allocate the vetor. In the case of modified code, the only thing it will allocate is a pointer, and then the code will go wrong.

The solution to this is to revert to what was used in the original code. Do not rely on the use of sizeof for strings, arrays, or linked lists, because often it won’t do what you would think it would do.

See here a test proving this:

int xxx(int vetor[]) {
    return sizeof(vetor);
}

int main() {
    int vetor[] = {10, 3, 5, 8, 1, 9, 2, 4, 7, 0, 6, -1};
    int n = sizeof(vetor) / sizeof(int);
    printf("%d\n", n);
    printf("%d\n", xxx(vetor));
}

Here’s the way out:

12
8

See here this running on the ideone.

  • Victor, thank you for the information regarding the use of sizeof. I didn’t know about this problem that may occur.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.