0

I just wanted to ask what is the difference between

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

and

int *a = malloc(sizeof(int))

because I came across this situation while working on a project, and I tried to create an array as a pointer, and dynamically allocate memory to it. But when I was using int * a = malloc(sizeof(int)) I could not change any of the values of the array, causing a segmentation fault, and when I changed it to int * a =(int *)malloc(sizeof(int)) it all started working fine and dandy. To be more exact this is the code in question:

     int i = 0;
     for (i = 0; i < valCounter; i++) {
          if (strcmp(vars[i].name, name) == 0) {
               return 2;
          }
     }
     strcpy(vars[valCounter].name, name);
     strcpy(vars[valCounter].type, type);
     i--;
     vars[i].intArrayVal = (int *)malloc((dimension + 5) * sizeof(int)); 
     int j = 0;
     char currentNumber[1000];
     char elemente[1000];
     int *pointer;
     strcpy(elemente, elements);
     strcpy(currentNumber, strtok(elemente, ","));
     vars[i].intArrayVal[j++] = atoi(currentNumber);
     while (currentNumber != NULL && j <= i) {
          strcpy(currentNumber, strtok(NULL, ","));
          vars[i].intArrayVal[j++] = atoi(currentNumber);
     }
     valCounter++;
     return 0;

Here if I was using the version without the cast it wasn't working properly, and after changing on the fact that I put the cast there it started working as intended.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
HelpMePLs
  • 55
  • 1
  • 5
  • 4
    There's no difference, that can't be the reason for the problem. – Barmar Dec 29 '21 at 21:07
  • 1
    *" I could not change any of the values of the array."* They both given an array of length 1. The "undefined behaviour" might have any result: good, bad or ugly. – Weather Vane Dec 29 '21 at 21:07
  • 1
    Regarding the cast part of your question, there is a discussion on this topic [here](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – ryyker Dec 29 '21 at 21:07
  • 1
    Normally you shouldn't have the cast. See https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc for the reason. – Barmar Dec 29 '21 at 21:07
  • What do you mean by "any of the values of the array"? There's no array, you allocated memory for just one integer. – Barmar Dec 29 '21 at 21:08
  • `int * a = malloc(sizeof(int))` -> `int * a = malloc(sizeof(*a) * desiredLenOfArray)` – ryyker Dec 29 '21 at 21:09
  • 3
    If you tried to change anything other than `a[0]` or `*a`, it was undefined behavior and anything could happen. It may just be a coincidence that it seemed to work when you added the cast. – Barmar Dec 29 '21 at 21:09
  • 1
    In C, you can assign a `void *` value to any pointer, so the cast is unnecessary. And in general, you want to avoid needless casts as they can only cause problems. Contrary to what you claim, adding the case did not cause your program to start working fine. – ikegami Dec 29 '21 at 21:10
  • 1
    "I could not change any of the values of the array, causing a segmentation fault" [Edit](https://stackoverflow.com/posts/70525012/edit) your question and show this code. – yano Dec 29 '21 at 21:11
  • _"it all started working fine and dandy"_. Only in appearances. You are actually writing to memory that your program does not own if you use more that `a[0]` – ryyker Dec 29 '21 at 21:12
  • The difference between `int *a = malloc(sizeof(int))` and `int *a = (int *)malloc(sizeof(int))` is basically the same as the difference between `double d = 2;` and `double d = (double)2;`. – Steve Summit Dec 29 '21 at 21:49

1 Answers1

5

There is no difference in C language if you cast or not. If you get an error it means that you compile as C++. C++ is a completely different language and do not use C++ compilers to compile C code

I could not change any of the values of the array, causing a segmentation fault

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

You allocate the space for one integer only - the array is only one element long. When you tried to access any element other than a[0] you were accessing the invalid memory location.

If you want to allocate space for more integers you need to:

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

or better

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

Use objects in sizeofs instead of types. If you change the type of the object, you will not have to change all appearance of the sizeof. It is very easy to miss one or two introducing very hard to debug and find errors

0___________
  • 60,014
  • 4
  • 34
  • 74