[SOLVED] C - How do you malloc for a void pointer?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
int mm_init (mm_t *MM, int hm, int sz) {
int i;
for (i=0;i<hm;i++)
{
MM->Mem_Chunk[i]=(void*)malloc(sz);
MM->Mem_Chunk_Used[i]=0; //initially, no chunks are in use
if (MM->Mem_Chunk[i] == -1) return -1;
}
MM->MAX_CHUNKS=hm;
return 0;
}
With the struct defined as:
Code:
typedef struct {
void *Mem_Chunk;
int *Mem_Chunk_Used;
int MAX_CHUNKS;
} mm_t;
Right now I know that malloc is being casted to a void instead of a void pointer. How can I get it to be cast as a void pointer? Thanks for your time!
int mm_init (mm_t *MM, int hm, int sz) {
int i;
for (i=0;i<hm;i++)
{
MM->Mem_Chunk[i]=(void*)malloc(sz);
MM->Mem_Chunk_Used[i]=0; //initially, no chunks are in use
if (MM->Mem_Chunk[i] == -1) return -1;
}
MM->MAX_CHUNKS=hm;
return 0;
}
With the struct defined as:
Code:
typedef struct {
void *Mem_Chunk;
int *Mem_Chunk_Used;
int MAX_CHUNKS;
} mm_t;
Right now I know that malloc is being casted to a void instead of a void pointer. How can I get it to be cast as a void pointer? Thanks for your time!
Your
Code:
(void*)malloc(sz);
casts to pointer to void, and this is what it should do according to
You will have to use the data type void** for Mem_Chunk. This because you cannot create objects of type void, only pointers to void* are valid. Thus you will need Mem_Chunk to be either an array of void* or void**. But you will have to allocate space for Mem_Chunk and Mem_Chunk_Used, too: Somewhere the addresses of each chunk allocated by malloc must by stored:
Code:
typedef struct {
void **Mem_Chunk;
int *Mem_Chunk_Used;
int MAX_CHUNKS;
} mm_t;
int mm_init (mm_t *MM, int hm, int sz) {
int i;
MM->Mem_Chunk = malloc( sizeof(void*) * hm );
MM->Mem_Chunk_Used = malloc( sizeof(int) * hm );
for (i=0;i<hm;i++)
{
MM->Mem_Chunk[i]=(void*)malloc(sz);
MM->Mem_Chunk_Used[i]=0; //initially, no chunks are in use
if (MM->Mem_Chunk[i] == -1) return -1;
}
MM->MAX_CHUNKS=hm;
return 0;
}
Actually, if I take what you gave me and just comment out a few parts:
Code:
typedef struct {
void **Mem_Chunk;
int *Mem_Chunk_Used;
int MAX_CHUNKS;
} mm_t;
int mm_init (mm_t *MM, int hm, int sz) {
int i;
//MM->Mem_Chunk = malloc( sizeof(void*) * hm ); //commented out here
//MM->Mem_Chunk_Used = malloc( sizeof(int) * hm ); //commented out here
for (i=0;i<hm;i++)
{
MM->Mem_Chunk[i]= /*(void*)*/ malloc(sz); //commented out a part here
MM->Mem_Chunk_Used[i]=0;
if (MM->Mem_Chunk[i] == -1) return -1;
}
MM->MAX_CHUNKS=hm;
return 0;
}
Then that gives me more of what I was looking for. Then I don't have to malloc the multiple times ahead of time too. Thanks!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.