C 分配时作废*

C 分配时作废*,c,memory,malloc,dynamic-memory-allocation,void,C,Memory,Malloc,Dynamic Memory Allocation,Void,什么时候使用合适 void* space_to_use = malloc(size); 无法复制粘贴整个C语言书。任何一本书,答案都在前10页。可以是任何你想要的。请澄清你的具体问题或添加额外的细节,以突出你所需要的。正如目前所写的,很难准确地说出你在问什么。请参见帮助澄清此问题的页面。在提交答案后完全更改您的问题有点粗鲁,因为现在答案没有意义。您需要回答此代码所在的某些上下文。仅供参考:sizeof char定义为始终为1,因此不需要重复使用它。但是,CHAR\u位的定义可能不同。另外,请

什么时候使用合适

void* space_to_use = malloc(size); 

无法复制粘贴整个C语言书。任何一本书,答案都在前10页。可以是任何你想要的。请澄清你的具体问题或添加额外的细节,以突出你所需要的。正如目前所写的,很难准确地说出你在问什么。请参见帮助澄清此问题的页面。在提交答案后完全更改您的问题有点粗鲁,因为现在答案没有意义。您需要回答此代码所在的某些上下文。仅供参考:
sizeof char
定义为始终为
1
,因此不需要重复使用它。但是,
CHAR\u位的定义可能不同。另外,请参见“是”,它将始终是一个字节,但它将是整数*或结构变量或指针的整数的通用表示形式。我们需要添加sizeof(int)或sizeof(int*)或sizeof(struct abcd),因此,如果我们提到sizeof(CHAR),它不会造成任何损害。
void* space_to_use = malloc(size); 
// malloc always return void pointer that means it can be typecast to any type.
// before using void pointer it is necessary to typecast it into proper type.
// for example:-
// if size is 8 byte.It will allocate 8 byte of memory.
/*
void* space_to_use = malloc(size); 
char * ptr = (char*)space_to_use;
*/
// These two line can be combine in one statement.

char * ptr = (char*)malloc(size*sizeeof(char));
// NOTE:sizeof(char) is to make sure platform independent.

// Same for int if we want to store some integer.
 int * ptr = (int*)malloc(size*sizeeof(int));