Problem with malloc

Asked

Viewed 295 times

2

People, doing a small program that given any number, enter 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 it 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 in the case would be, for example, enter numero = 10, y returns 2 and I have a vector with 2 positions being them v[0] = 0 and v[1] = 1. When I do malloc(y*sizeof(int)) that should be, in case, 2*the size of an integer, it n returns me 2 spaces, only 1, how can I solve this? I’m doing something wrong? Thank you

  • It’s not just this problem, that code doesn’t work.

2 answers

2


int verPal(int num1, int *v)

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

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

v as seen on main should remain a pointer to int (that is to say, int*) but the function verPal must receive the address of v so that the changes she makes in the v are visible outside the function.

Here’s 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;
}
  • I think my pointers concept is a little bit off asuhsau, anyway, thanks man, it helped a lot here :3

0

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

Some of the solutions to this problem would be:

Use vector v as the global variable. Declaring it outside the main int and outside the function.

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

See my changes: Variavél global.

#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, printing the vector into 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 correct, for example, return the vector to main, but I believe we would have to make more changes to your code.

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

I hope it helped.

Browser other questions tagged

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