1

I'm writing a program that calculates the greatest common denominator of two numbers, but i'm getting problem with malloc function and pointers. Actually it's clear how the stack and the heap segments work in the memory and why. But yet i'm not yet able to understand when declaring a pointer and using malloc is functional or not, is necessary or not, in a program. here is the code :

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

int *calcolaDivisori(int);

int main(int argc, char** argv) {

    int foundCounter = 0;
    int i,j,s1,s2;
    int n1,n2;
    int mcd = 1,mcm;
    int *pn1,*pn2;
    int d1[100],d2[100];

    // INPUT dei due interi

    printf("Inserisci il primo numero :");
    scanf(" %d", &n1);
    printf("\nInserisci il secondo numero :");
    scanf(" %d", &n2);

    // calcolo divisori del primo e del secondo numero e li assegno ai relativi array

    pn1 = calcolaDivisori(n1);
    if (!pn1) return 1;
    pn2 = calcolaDivisori(n2);
    if (!pn2) return 1;

    for (i=0;i<n1;i++) {
        d1[i] = pn1[i];
    }

    for (i=0;i<n2;i++) {
        d2[i] = pn2[i];
    }

    free(pn1);
    free(pn2);

    // confronto i divisori e calcolo il MCD

    s1 = sizeof(d1) / sizeof(int);
    s2 = sizeof(d2) / sizeof(int);

    for(i=0; i<s1; i++) {
        for (j=foundCounter; j<s2;j++) {
            if (d1[i] == d2[j]) {
                mcd*= d1[1];
                foundCounter = j+1;
                break;
            }
        }
    }

    printf("\n\nIl minimo comune divisore e' : %d", mcd);

    return 0;
}

int *calcolaDivisori(int num) {
    int i;
    int *a = malloc(num * sizeof(int));
    if (!a) return NULL;
    for (i=2;i<num;i++) {
        if (num%i == 0) {
            num/=i;
            a[i-2]=i;
        }
    }

    return a;
}

I get the error in the title when is run the command :

int *a = malloc(sizeof(int));
dragosht
  • 3,237
  • 2
  • 23
  • 32
Ghislo
  • 85
  • 1
  • 2
  • 10
  • 9
    You should only get this warning if you are compiling your code as C++. A C compiler will not give this warning. – kaylum Dec 15 '15 at 10:12
  • 5
    @kaylum - `#include ` another giveaway I'd say. – Persixty Dec 15 '15 at 10:15
  • 1
    @DanAllen Yeah you're right. I missed that and only saw the `C` tag. @Ghislo please fix up your tags as it appears you are writing C++ code and not C code (if that is indeed your intention). – kaylum Dec 15 '15 at 10:16
  • 1
    `#include ` delete this line. – BLUEPIXY Dec 15 '15 at 10:17
  • 2
    You've tagged the question C but used a couple of C++ constructs. You need to decide whether you're trying to write a C or C++ program. The advice in this area is quite different between the two. – Persixty Dec 15 '15 at 10:23
  • This is `C` code an not `C++`. Use a proper compiler. – Michi Dec 15 '15 at 10:42
  • 1
    @Michi `#include ` is not C code – M.M Dec 15 '15 at 12:39
  • @M.M You right, but this is not part of he's problem. And I see now that the OP doesn't respond at all. – Michi Dec 15 '15 at 12:41

1 Answers1

6

You need to cast:

int *a = (int*)malloc(num * sizeof(int));

Because there's no implicit conversion from void* to type * in C++.

Note that this cast is not required in C and could potentially be dangerous to do so in C.

Except for #include <iostream>, nothing in your code is C++. So remove it and compile it with a C compiler and you wouldn't need this cast.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • Since this is C++, may as well use `static_cast<>`, makes it harder to mistake for C, and reminds the programmer to remove the cast if the code is copy-pasted into an actual C source. Or better yet, remove the malloc altogether in C++, using an `std::vector` instead. – Medinoc Dec 15 '15 at 10:19
  • @Medinoc Yes, I agree your suggestion. But I am not sure if it's really C++. I thought of editing the tag to C++. But it really seems to be C code with the exception of including `` (and hence OP compiles it as C++). – P.P Dec 15 '15 at 10:22
  • You shouldn't use malloc in C++ programs so there is no need to ponder the cast in the first place. Mixing malloc/free while everything else in C++ uses the non-compatible `new`/`delete`, is a very stupid idea which will only cause bugs. – Lundin Dec 15 '15 at 10:39
  • 1
    The OP removed the `C++` tag, there is no code which suggest using of `C++`, but `C`. I think the only problem is the compiler, he use a `C++` compiler. – Michi Dec 15 '15 at 10:41
  • 1
    @Lundin Keep in mind though, don't replace the malloc() with a `new[]` if you can't see the matching free() to replace with a `delete[]` (here, we can see the free() in the main function). – Medinoc Dec 15 '15 at 10:41
  • @Michi good eye. Ooh, maybe it's a POSIX platform and the file is saved with a capital `.C` extension? – Medinoc Dec 15 '15 at 10:42
  • perfect answer still works in 2022 – Pir Fahim Shah Mar 29 '22 at 12:55