Problem in C code running a function

Asked

Viewed 73 times

2

Hello, I have a problem with this code. I am implementing the function of Bubble Sort. But something curious happens when running this code. The variable n that indicates the amount of vector spaces in the main function is modified after executing the bubbleSort() function. And I don’t understand the reason for this modification since it doesn’t pass the pointer of the variable n to the sorting function. Could someone please explain to me why?

n always modifies to the higher value of the vector.

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

int ASC(int x, int y) {
    return x < y;
}

int DSC(int x, int y) {
    return x > y;
}

void bubbleSort(int *list, int start, int size, int cmp(int, int)) {
    int cont, hold;
    for(; start < size-1; start++) {
        for(cont = 0; cont < size-start; cont++) {
            if(cmp(list[cont+1], list[cont])) {
                hold = list[cont];
                list[cont] = list[cont+1];
                list[cont+1] = hold;
            }
            printf("hold = %d\n", hold);
            printf("cont = %d\n\n", cont);
        }
    }
}

int main() {
    int vetor[] = {1,8,7,14,5};
    int n = sizeof(vetor)/sizeof(int);
    printf("n = %d\n", n);
    bubbleSort(vetor, 0, n, ASC);

    printf("n = %d\n", n);
    int i;
    for(i = 0; i < n; i++) {
        printf("%d ", vetor[i]);
    }
    return 0;
}
  • I did a test on my machine (Linux Fedora 24) and the value of n nay is modified...

1 answer

2


gmorikawa this question really was hard to understand, but here goes:

The concept of pointers is the same concept as the arrays in c, when the data is stored in the stack (stack) they are sequenced in memory.

Imagine the following situation: You declared an array with 5 indexes and right after an integer (outside the array, but still in the stack sequence) both in the stack:

int vetor[] = {1,8,7,14,5};
int n = sizeof(vetor)/sizeof(int);

Thus the operating system organized its data as follows (fictitious, but with educational purpose)

Índice       | Valor | End. Memória
int vetor[0] |   1   | 0x000
int vetor[1] |   8   | 0x001
int vetor[2] |   7   | 0x002
int vetor[3] |  14   | 0x003
int vetor[4] |   5   | 0x004
int n        |   ?   | 0x005

And in your Bubblesort function you access during the iteration the current index (iterator) and the index + 1.

When arriving at the last element of the array the next accessed element corresponds in type (int), but is no longer part of the array; This element is accessed sequentially in memory because it is a valid pointer to an integer (points to the variable "n").

The value is tested and follows the same Sort process...

If the vector value["ultimo_indice" + 1] is changed the variable "n" will receive the value assigned to it.

C is very good, gives you many possibilities. But sometimes these possibilities generate a very different error than expected (in the case index out of range for the array).

I hope I was able to help.

Browser other questions tagged

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