Question

I am coming from Python so using malloc is new to me. Intuitively the below should work but having syntax issues. In my first line I want to set array size to be a max of 8 ints. In the second line, I want to add those 4 ints. This line is for example only, in production I will have user input up to an 8-digit number. When I go to compile (clang) I get size of array has non-integer type 'void *' If I comment out this first line and initialize with the second line (and adding int type) the code works. So I am obviously setting the size incorrectly. Any ideas?

int main(void)

{

    int mult_digits[malloc(8 * sizeof(int))];
    mult_digits[] = {1,2,3,4};
    int size_mult = sizeof mult_digits / sizeof *mult_digits;
    printf("Size of the array is %d\n", size_mult);
    return 0;
}
Was it helpful?

Solution

This code is all wrong. You call malloc to allocate memory, and malloc returns a pointer. Rather than deconstructing your syntax, which is very broken, I'll give a couple of variants of your program.

int main(void)
{
    int mult_digits[] = {1,2,3,4};
    int size_mult = sizeof mult_digits / sizeof *mult_digits;
    printf("Size of the array is %d\n", size_mult);
    return 0;
}

Here the array is not allocated dynamically. It's a local variable with automatic, stored on the stack.

For dynamic allocation you would do this:

int main(void)
{
    int *mult_digits = malloc(4*sizeof *mult_digits);
    mult_digits[0] = 1;
    mult_digits[1] = 2;
    mult_digits[2] = 3;
    mult_digits[3] = 4;
    free(mult_digits);
    return 0;
}

The argument to malloc is the number of bytes to be returned. The value returned is the address of the new block of memory. Note also here that we made a call to free to deallocate the memory. If you omit that, the memory will be leaked.

With this variant, there is no way to recover the length of the array from mult_digits. I know that might freak you out, coming from Python, but I repeat. There is no way to recover the length of the array from mult_digits. It's your job to keep track of that information.

Now, you wanted to over-allocate the memory. You can certainly do that:

int main(void)
{
    int *mult_digits = malloc(8*sizeof *mult_digits);
    mult_digits[0] = 1;
    mult_digits[1] = 2;
    mult_digits[2] = 3;
    mult_digits[3] = 4;
    free(mult_digits);
    return 0;
}

Here we only used the first 4 elements, and ignored the final 4. That's just fine. In case you do over-allocate you would typically need to keep track of both the allocated length, and the in-use length. You can then add new items by increasing the in-use length, up until you reach the allocated length. The you need to reallocate a larger block. I guess that's what you are driving at.

OTHER TIPS

The problem is your syntax. What you meant was: int *mult_digits = malloc(8 * sizeof(int));

After that, mult_digits[] = {1,2,3,4}; is wrong. You could, however,

mult_digits[0] = 1;
mult_digits[1] = 2;
mult_digits[2] = 3;
mult_digits[3] = 4;

But unless you have some reason to swim into the pointer deep end, you might just want to:

int mult_digits[] = {1, 2, 3, 4};

Edit: to help with applying this answer, here is a full modified function that compiles and runs:

#include <stdio.h>

int main(void)
{
    int mult_digits[] = {1,2,3,4};
    int size_mult = sizeof mult_digits / sizeof *mult_digits;
    printf("Size of the array is %d\n", size_mult);
    return 0;
}

in square brackets you must specify the number of elements needed, not the length in byte of the array. you can use malloc to do

int* multdigits = malloc(sizeof(int)*8);
mult_digits[0] = 1;

etc

(sorry if my syntax is wrong, i don't write c code from years!)

they're two ways to do the same thing. see "malloc" as the "new" operator.

Try

int main(void)
{
    int mult_digits[] = {1,2,3,4};
    int size_mult = sizeof mult_digits / sizeof int;
    printf("Size of the array is %d\n", size_mult);
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top