LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-20-2010, 04:41 PM   #1
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Rep: Reputation: 15
C - How do you malloc for a void pointer?


So far I have this:
Code:
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!
 
Old 03-20-2010, 04:46 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by golmschenk View Post
So far I have this:
Code:
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

Code:
void *Mem_Chunk;
.

For readability/similarity you could have written

Code:
(void*)malloc(sz);
as

Code:
(void *)malloc(sz);
.
 
Old 03-20-2010, 04:47 PM   #3
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
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;
}
 
Old 03-20-2010, 04:53 PM   #4
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
Worked perfectly irmin. Thanks a bunch.
 
Old 03-20-2010, 05:21 PM   #5
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C: Casting a pointer to (void **) jrtayloriv Programming 7 09-08-2009 05:52 PM
Help on void pointer yfaye Programming 7 04-18-2009 07:10 AM
void * pointer in function xailer Programming 23 01-16-2004 02:14 PM
void pointer help gonnaWorkItOut Programming 1 10-12-2003 11:52 AM
g++ delete [] void pointer? ugenn Programming 5 06-01-2002 03:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration