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".
我试图在此函数中创建一个内存池,如果成功创建池,则返回true。该函数应该创建一个内存池,其名称传递为“* name”,大小为“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;
5 个解决方案
#1
2
After malloc
, name
is a non-null pointer if successful, a null pointer otherwise.
在malloc之后,如果成功,name是非空指针,否则为空指针。
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.
因此表达式name =(char *)malloc(size)的类型为char *,您将此值赋给task,由于您没有使用强制转换,因此应该给出警告。
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
)
在成功的malloc中,指针名称是非空的,这不是整数1,这就是为什么任务既不等于true(1),也不是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;
}
}
#2
1
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
C中的所有内容都返回一个值。和枚举基本上是int。当你说
task = (name = (char *) malloc (size));
The expression (name = (char *) malloc (size));
also return value. The value returned by that expression is pointer.
表达式(name =(char *)malloc(size));也回报价值。该表达式返回的值是指针。
task
gets the pointer value. So it is printing unexpected
任务获取指针值。所以它打印意外
#3
1
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:
解决问题的最简单方法是确保任务只获得1或0,而不是当前代码,而是为其分配指针值。以下代码将执行此操作:
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.
首先 !如果malloc返回NULL,则运算符会将malloc调用的结果转换为1,否则为0,然后是下一个!操作员会将其更改回您关心的1或0。
Alternately, use a real boolean type rather than rolling your own:
或者,使用真正的布尔类型而不是滚动自己的类型:
_Bool task;
or:
要么:
bool task;
if you #include <stdbool.h>
first.
如果你首先#include
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:
注意:我还删除了不必要的类型转换。你当前的代码可能应该给你一个关于在没有强制转换的情况下将指针转换为整数的警告 - 你应该注意这些警告并适当地纠正代码。例如,来自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.
#4
0
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;
你说这个任务和名字一样。由于name获得malloc的返回,这将是一个内存地址。所以任务不可能是真或假。而是尝试if(task)返回true;否则返回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.
其次,name是函数的输入参数。您的来电者将无法读取输出。目前尚不清楚为什么要调用名称输入名称;它从未被用作任何东西的名称,实际上从未读过。相反,您将malloc的输出分配给它。这可能不是你想要的。
#5
0
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.
如果无法分配内存,malloc将返回NULL(即0,在您的情况下为false)。如果成功分配了一个内存,它将返回新分配的内存的地址,该内存不会完全为1.您的true设置为1,因此所有不等于1的值都不会为'true'。这就是为什么你会出乎意料,即使它是成功的。
What you can do is
你能做的是
if(task ==false)
printf("task is false");
else
printf("task is true memory pool created");
#1
2
After malloc
, name
is a non-null pointer if successful, a null pointer otherwise.
在malloc之后,如果成功,name是非空指针,否则为空指针。
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.
因此表达式name =(char *)malloc(size)的类型为char *,您将此值赋给task,由于您没有使用强制转换,因此应该给出警告。
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
)
在成功的malloc中,指针名称是非空的,这不是整数1,这就是为什么任务既不等于true(1),也不是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;
}
}
#2
1
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
C中的所有内容都返回一个值。和枚举基本上是int。当你说
task = (name = (char *) malloc (size));
The expression (name = (char *) malloc (size));
also return value. The value returned by that expression is pointer.
表达式(name =(char *)malloc(size));也回报价值。该表达式返回的值是指针。
task
gets the pointer value. So it is printing unexpected
任务获取指针值。所以它打印意外
#3
1
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:
解决问题的最简单方法是确保任务只获得1或0,而不是当前代码,而是为其分配指针值。以下代码将执行此操作:
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.
首先 !如果malloc返回NULL,则运算符会将malloc调用的结果转换为1,否则为0,然后是下一个!操作员会将其更改回您关心的1或0。
Alternately, use a real boolean type rather than rolling your own:
或者,使用真正的布尔类型而不是滚动自己的类型:
_Bool task;
or:
要么:
bool task;
if you #include <stdbool.h>
first.
如果你首先#include
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:
注意:我还删除了不必要的类型转换。你当前的代码可能应该给你一个关于在没有强制转换的情况下将指针转换为整数的警告 - 你应该注意这些警告并适当地纠正代码。例如,来自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.
#4
0
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;
你说这个任务和名字一样。由于name获得malloc的返回,这将是一个内存地址。所以任务不可能是真或假。而是尝试if(task)返回true;否则返回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.
其次,name是函数的输入参数。您的来电者将无法读取输出。目前尚不清楚为什么要调用名称输入名称;它从未被用作任何东西的名称,实际上从未读过。相反,您将malloc的输出分配给它。这可能不是你想要的。
#5
0
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.
如果无法分配内存,malloc将返回NULL(即0,在您的情况下为false)。如果成功分配了一个内存,它将返回新分配的内存的地址,该内存不会完全为1.您的true设置为1,因此所有不等于1的值都不会为'true'。这就是为什么你会出乎意料,即使它是成功的。
What you can do is
你能做的是
if(task ==false)
printf("task is false");
else
printf("task is true memory pool created");