Question:
C programming using malloc() for an array?
bill k
2012-12-23 11:14:04 UTC
How can I malloc an array of integers that has 5 elements?

#include
#include

int main()
{
int numbers[5]={1,2,3,4,5};

malloc();
printf("Malloc successful #number of bytes malloced from address to adrress");//please make this print out what it says
return 0;
}
Six answers:
Jonathan
2012-12-23 13:09:26 UTC
I liked (and up'd) John H's answer. But he forgot to mention free(). You need to free() everything you malloc(), somewhere in your code. (And Bill, you KEEP forgetting to include 'void' in your definition of main().)



Unfortunately, you can't use compile-time initializers for malloc()'d arrays. You have to write code to do that.



#include

#include

#define N       (5)

int main( void ) {

    int i, *numbers;

    size_t s;

        s= N * sizeof( numbers[0] );

        numbers= (int *) malloc( s );

        for ( i= 0; i < N; ++i )

            numbers[i]= i+1;

        printf( "Bytes allocated for the %d element array is %lu.\n", N, (unsigned long) s );

        printf( "\n Address   Value\n--------   -----\n" );

        for ( i= 0; i < N; ++i )

            printf( "%8p  %5d\n", (void *) &numbers[i], numbers[i] );

        free( numbers );

    return 0;

}



That's a complete application that does what you ask and perhaps a little more.



(Note the use of (void *) to cast the pointer. %p uses void* pointers, not int*. They usually are the same... but... stay with the standard because they do not have to be the same. Also note that I chose a different way of using sizeof(), applying it to numbers[0] instead of int. Just to show another way to approach figuring the size of something. Could also use *numbers, too. It's all good. I'd normally use int, but I just wanted to point up another approach.)
Peter
2012-12-24 09:06:19 UTC
Steps:

1. Simply declare a pointer, to which we will malloc some memory

2. Allocate 5 integers space, and associate that memory with our pointer (in step 1)

3. Print what you want!



Here, I did the program for you:-



#include

#include



int main()

{

       int *numbers;       //pointer

       numbers = (int *) malloc(5 * sizeof(int));

       printf("Malloc successful\n");

       printf("%d bytes malloced from %ud to %ud", 5, numbers, numbers+5);

       return 0;

}
green meklar
2012-12-24 13:17:23 UTC
malloc() takes an argument (the number of bytes to allocate) and returns a pointer to the newly allocated memory. The correct usage is something like this:



int* numbers=(int*)malloc(5*sizeof(int));



That is to say, you want an amount of memory equal to 5 times the size of a single int, and when it's allocated, you want to cast the starting memory address to a pointer to int, and assign its value to a variable of type pointer to int.



In the real world, you usually want to keep track of the length somewhere else, so you'd do something more like this:



int n=5;

int* numbers=(int*)malloc(n*sizeof(int));



Now you can just keep referring to n later on, and you only have to change its value once in order to change all the corresponding program logic, rather than replacing the magic number 5 in every single spot where it gets used.
pippin
2016-08-08 09:51:56 UTC
Your malloc looks right. If the error you're getting is whatever like "can not convert (void*) to (double *) and not using a cast" then you're using a C++ compiler no longer a C compiler you do not need the solid in C
John H
2012-12-23 11:39:10 UTC
int *numbers;



numbers = (int *) malloc(5 * sizeof(int));

numbers[0] = 1;

numbers[1] = 2;



etc...



Your malloc() with no argument is completely wrong.
David Allan
2012-12-23 11:38:14 UTC
malloc is used to allocate contiguous memory so to use it you need at the least a pointer to the allocated memory and you need to know how much memory is needed.



int main(){

int *numberPtr;

//number pointer

numberPtr=malloc(sizeof (int)*5);

//allocate space for 5 integers

//and make sure our pointer points to that location using =

*numberPtr={1,2,3,4,5};

//assign values to the location pointed to by pointer


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...