Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C Does type before malloc even matter ?

Xenon02

New Coder
Hello !

I've been testing some stuff with pointers.
Here is one of them :


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

int main()
{
    int *matrix = (int *)malloc(1);
 
    *(matrix) = 2562323;
 
    printf("%d", *matrix);
    return 0;
}

I tried to check if the output will be an error because I've only reserved 1 byte, but when I wrote 2562323 into this one byte it saved and printed this value. How ??? What if in next byte after the malloc there is something ?

Same went when I changed malloc from int to char


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

int main()
{
    int *matrix = (char *)malloc(1);
 
    *(matrix) = 2562323;
 
    printf("%d", *matrix);
    return 0;
}

I expected that the only number that will appear is 2. Because the memory is for char, so only 1 byte, so number "2" at the beggining will be saved in this one byte and the rest will be ignored Does this "type" infront of malloc matter ?
 
Last edited:
It's undefined behavior to assign an object to uninitialized memory. int has a defined 2 or 4 byte width, so maybe the compiler is using sizeof(int) from the data type of the declared matrix pointer. A program compiled using this code could arbitrarily fail or produce different outputs.

The type in front of malloc() doesn't matter, it's just a redundant operation that gets implicitly converted to int * after casting explicitly to char *.

The (int *) cast to an int * pointer isn't needed. Here's a documented ANSI C example that confirms this (with the non-standard alloc() function).

Code:
extern void *alloc(size_t);
double *dp = alloc(sizeof *dp);

The following shows the correct, standards-compliant version of your code.

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

int main() {
    int *matrix = malloc(sizeof(int));

    if (matrix != NULL) {
      *matrix = (int) 2562323;
    } else {
      exit(EXIT_FAILURE);
    }

    printf("%d", *matrix);
    return 0;
}

The (int) cast is explicit because sizeof(int) can be 2 bytes on 32-bit systems and 2562323 exceeds 2 bytes. If sizeof(int) is 2 instead of 4, the output is truncated to 24910.
 
I suggest using long instead of int to avoid truncation as well.

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

int main() {
    long *matrix = malloc(sizeof(long));

    if (matrix != NULL) {
      *matrix = 2562323;
    } else {
      exit(EXIT_FAILURE);
    }

    printf("%ld", *matrix);
    return 0;
}
 
Back
Top Bottom