Reputation: 1545
I am trying to create a memory pool in this function which returns true if the pool is created successfully. This function is supposed to create a pool of memory with name passed as "*name" and size of "int size".
boolean create_memory_pool(char *name, int size)
{
boolean task;
task = (name = (char *) malloc (size));
if(task == true)
printf("task is true memory pool created");
else if(task ==false)
printf("task is false");
else
printf("unexpected");
return task;
}
Its printing "unexpected" which means something else is happening. I have defined the boolean as:
typedef enum{
false,
true
}boolean;
Upvotes: 0
Views: 139
Reputation: 122423
After malloc
, name
is a non-null pointer if successful, a null pointer otherwise.
So the expression name = (char *) malloc (size)
has type char *
, you are assigning this value to task
, which should give you a warning because you didn't use a cast.
In a successful malloc
, the pointer name
is non-null, which is not the integer 1
, that's why task
would be neither equal to true
(1
), nor false
(0
)
Actually you don't need task
, just test if the value name
is a null pointer:
boolean create_memory_pool(char *name, int size)
{
name = malloc(size);
if (name != NULL)
{
printf("malloc successful\n");
return true;
}
else
{
printf("malloc failed\n");
return false;
}
}
Upvotes: 2
Reputation: 3812
malloc returns NULL (ie, 0, in your case false), if it cant allocate memory. It returns the address of newly allocated memory if it successfully allocated a memory, which wont be exactly 1. Your true is set as 1, so all values not equal to 1 will not be 'true'. That's why you are getting unexpected, even it is successful.
What you can do is
if(task ==false)
printf("task is false");
else
printf("task is true memory pool created");
Upvotes: 0
Reputation: 224992
The simplest solution to your problem is to make sure that task
only gets a 1
or 0
, as opposed to your current code, which assigns a pointer value to it instead. The following code will do just that:
task = !!(name = malloc(size));
The first !
operator will convert the result of the malloc
call into a 1
if malloc
returned NULL
and 0
otherwise, and the next !
operator will change it back into the 1
or 0
you care about.
Alternately, use a real boolean type rather than rolling your own:
_Bool task;
or:
bool task;
if you #include <stdbool.h>
first.
Note: I also removed the unnecessary typecast. Your current code should probably be giving you a warning about converting a pointer to an integer without a cast - you should heed such warnings and correct the code appropriately. For example, from clang:
example.c:12:8: warning: incompatible pointer to integer conversion assigning to
'boolean' from 'char *' [-Wint-conversion]
task = (name = (char *) malloc (size));
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.
Upvotes: 1
Reputation:
To make it work use
task = ((name = (char *) malloc (size))!=NULL);
Everything in C returns a value. and enum
is basically int
.
when you say
task = (name = (char *) malloc (size));
The expression (name = (char *) malloc (size));
also return value. The value returned by that expression is pointer.
task
gets the pointer value. So it is printing unexpected
Upvotes: 1
Reputation: 6499
First, when you do something like
task = (name = (char *) malloc(size));
You're saying that task gets the same thing as name. Since name gets the return of malloc, that's going to be a memory address. So task is unlikely to be either true or false. Instead try something like if(task) return true; else return false;
Secondly, name is an input parameter to the function. your caller will not be able to read the output. It's not clear why you call the name input name anyway; it's never used as the name of anything, and in fact never read. Instead, you assign the output of malloc to it. Which is probably not what you want.
Upvotes: 0