Problem with malloc

2

Galera, to make a small program that given any number, enters a function that divides the number into a vector and returns the number of decimal places (number of positions) that the vector has. In all the guides and tutorials I see says that the way I'm doing is right, but for some reason the code is not working. Here is the code:

#include <stdio.h>
#include <stdlib.h>
int verPal(int num1, int *v);
int main(int argc, const char * argv[]) {
    int *v;
    int numero, y;
    printf("digite o numero");
    scanf("%d", &numero);
    y = verPal(numero,v);
    for (int i = 0; i<y; i = i+1) {
        printf("%d \n", v[i]);
    }
    return 0;
}
int verPal(int num1, int *v){
    int x=num1;
    int y=0;

    while(x != 0){
        x = x/10;
        y = y+1;
    }
    x = num1;
    v = (int *) malloc(y*sizeof(int));
    for (int i = 0; x != 0; i=i+1) {
        v[i] = x%10;
        x = x/10;
    }
    return y;
}

The idea here would be, for example, enter numero = 10 , y return 2 and I have a vector with 2 positions being v[0] = 0 and v[1] = 1 . When I do malloc(y*sizeof(int)) it should be, in case, 2 * the size of an integer, it n returns me 2 spaces, only 1, how can I solve this? To doing something wrong? Thanks

    
asked by anonymous 06.09.2015 / 23:46

2 answers

2

int verPal(int num1, int *v)

// no main:
y = verPal(numero,v);

The verPal function receives a copy of the v pointer provided by main and assigns that local copy the address returned by malloc , so the v as seen in the main function does not point to the value allocated by malloc .

v as seen in main should continue to be a pointer to int (that is, int* ) but the verPal function should get the v address so that the changes it makes no v are visible outside the function.

Here is the revised code:

#include <stdio.h>
#include <stdlib.h>
int verPal(int num1, int **v); // <--- verPal recebe o endereço de um apontador
int main(int argc, const char * argv[]) {
    int *v; // <--- v continua sendo um apontador para int
    int numero, y;
    printf("digite o numero");
    scanf("%d", &numero);
    y = verPal(numero,&v); // <--- passe o endereço de v
    for (int i = 0; i<y; i = i+1) {
        printf("%d \n", v[i]);
    }
    return 0;
}
int verPal(int num1, int **v){ // <--- verPal recebe o endereço de um apontador
    int x=num1;
    int y=0;

    while(x != 0){
        x = x/10;
        y = y+1;
    }
    x = num1;
    int *vi = (int *) malloc(y*sizeof(int)); // <--- apontador local recebe retorno de malloc
    for (int i = 0; x != 0; i=i+1) {
        vi[i] = x%10; // <--- alterar valor no espaço alocado usando apontador local
        x = x/10;
    }
    *v = vi; // <--- apontador fora dessa função é atualizado com endereço novo
    return y;
}
    
07.09.2015 / 19:24
0

I have been analyzing your code and its logic seems right, but I noticed some problems, the vector v is resized in the verPal function, but it is not returned to main . So you try unsuccessfully to print it on main.

Some of the solutions to this problem would be:

Use the vector v as a global variable. Declaring it out of the int main and out of function.

Or print the vector content inside the function, without returning anything to int main.

See my changes: Global variable.

#include <stdio.h>
#include <stdlib.h>
int *v;
int verPal(int num1);
int main() {
    int numero, y,i;
    printf("digite o numero");
    scanf("%d", &numero);
    y = verPal(numero);
    for (i = 0; i<y; i = i+1) {
        printf("%d \n", v[i]);
    }
    return 0;
}
int verPal(int num1){
    int x=num1;
    int y=0,i;

    while(x != 0){
        x = x/10;
        y = y+1;
    }
    x = num1;
    v = (int*) malloc((y)*sizeof(int));
    for (i = 0; x != 0; i=i+1) {
        v[i] = x%10;
        x = x/10;
    }
 return y;
}

Another way, by printing the vector in the function:

#include <stdio.h>
#include <stdlib.h>
void verPal(int num1);
int main() {
    int numero, y,i;
    printf("digite o numero");
    scanf("%d", &numero);
    verPal(numero);
    return 0;
}
void verPal(int num1){
    int x=num1;
    int y=0,i;
    int *v;
    while(x != 0){
        x = x/10;
        y = y+1;
    }
    x = num1;
    v = (int*) malloc((y)*sizeof(int));
    for (i = 0; x != 0; i=i+1) {
        v[i] = x%10;
        x = x/10;
    }
    for (i = 0; i<y; i = i+1) {
        printf("%d \n", v[i]);
    }
}

Remembering that there are other ways to fix, such as returning the vector to main, but I think we would have to make more changes to your code.

Another observation: very large numbers (that exceed the size of an int, will give problem.) Ex: 12345678912, this number is not a valid input.

I hope you have helped.

    
07.09.2015 / 18:37