简体   繁体   English

如何在C中使用malloc正确分配内存

[英]How to properly allocate memory using malloc in C

I have a void pointer and I need to properly allocate memory of correct type.我有一个空指针,我需要正确分配正确类型的内存。 Say:说:

char * array="20080101"

Now, I know the value that this char string contains is a long type.现在,我知道这个字符字符串包含的值是一个长类型。 Now, I have a void pointer with me:现在,我有一个空指针:

void* pointer;

I need to allocate correct amount of memory to it and cast it as a long pointer so it will point to a long value (20080101);我需要为它分配正确数量的内存并将其转换为长指针,以便它指向一个长值(20080101);

The question is how do we do that?问题是我们如何做到这一点? From my research, I know I can allocate it using malloc as:根据我的研究,我知道我可以使用 malloc 分配它:

void *pointer=(long*) malloc(sizeof(long) OR sizeof(long)*strlen(array));// what should be the correct parameter.

How do we make it point to a value of long type?我们如何使它指向 long 类型的值? We have our array in string.我们有我们的字符串数组。

Oh, you are a bit confused.哦,你有点糊涂了。 First, A pointer, is a pointer, is a pointer , not a long and short pointer (anymore) and they are all the same size (generally 8-bytes on x86_64 and 4-bytes on x86 ).首先,指针是指针,是指针,而不是长指针和短指针(不再),它们的大小都相同(通常在x86_64上为8-bytes ,在x86上为4-bytes )。

Your array points to a null-terminated string literal , containing an apparent encoded date of Jan. 1, 2008. The numeric value 20080101 is easily within the size of an int or unsigned on any system and that is irrelevant to allocating storage for it (unless you are on a `16-bit system).您的array指向一个以空字符结尾的字符串文字,其中包含 2008 年 1 月 1 日的明显编码日期。数值20080101在任何系统上很容易在intunsigned的大小范围内,并且与为其分配存储无关(除非您使用的是 16 位系统)。

If you want to convert the string to a long , you can use strtol , eg:如果要将string转换为long ,可以使用strtol ,例如:

long myval = strtol (array, NULL, 10);

for base 10 conversion.为基数10转换。 The second parameter (above NULL ) is actually an endptr that on successful conversion returns a pointer to the next character in array following the number converted (if the string contains additional characters).第二个参数(在NULL之上)实际上是一个endptr ,它在成功转换时返回指向array转换后的数字后的下一个字符的指针(如果字符串包含其他字符)。 You will need to include <stdlib.h> to use strtol .您需要包含<stdlib.h>才能使用strtol

As for your cast question, if you have array and it is passed as void , eg至于你的演员问题,如果你有array并且它作为void传递,例如

long *somefunction (void *value, long *myval, ...)

Inside some function, you will need to do two things for conversion:在某些函数中,您需要为转换做两件事:

*myval = strtol (value, NULL, 10);

return myval;

Or, if you just need to create a pointer to long from myval , simply create the pointer:或者,如果您只需要从myval创建一个指向 long 的指针,只需创建指针:

long *lpointer = &myval;

Allocating Storage for array为阵列分配存储

When you allocate storage dynamically for any string, you need the length of the string ( + 1 for the null-terminator).当您为任何字符串动态分配存储空间时,您需要字符串的长度( + 1表示空终止符)。 Here is where you need to understand what sizeof will return and what strlen will return.在这里,您需要了解sizeof将返回什么以及strlen将返回什么。 If you take sizeof anypointer , you do not get the length, you get the pointer size ( 8-bytes , etc..).如果你使用sizeof anypointer ,你不会得到长度,你会得到指针大小( 8-bytes等)。 When you use sizeof dereferenced pointer you get the type size for the type pointed to (eg sizeof *somelongpointer will give you the storage size for a long on your system)当您使用sizeof dereferenced pointer您将获得sizeof dereferenced pointertype size (例如sizeof *somelongpointer将为您提供系统上long的存储大小)

If you are copying the string, it is better to include <string.h> and then:如果您要复制字符串,最好包含<string.h>然后:

size_t len = strlen (array);

Then you are ready to allocate storage:然后你就可以分配存储了:

char *mycopy = malloc (len * sizeof *array + 1);
strncpy (mycopy, array, len * sizeof *array + 1);

mycopy then holds the contents of array . mycopy然后保存array的内容。 Since it was dynamically allocated, you should free it when you no longer need it (eg free (mycopy); )由于它是动态分配的,因此您应该在不再需要它时free (mycopy);它(例如free (mycopy);

If your intent was to create a pointer to type long and dynamically allocate storage for the long , then you need sizeof to determine the size of a long on your system.如果您的目的是创建一个指向long类型和动态的分配存储long ,那么你就需要sizeof以确定您的系统上的长尺寸。 eg例如

long *mylong = malloc (sizeof *mylong);

then, (using the same somefunction example):然后,(使用相同的somefunction示例):

*mylong = strtol ((char *)value, NULL, 10);

return mylong;

Sorry for the confusion, but that should about cover all cases :) .抱歉造成混乱,但这应该涵盖所有情况:)

If you really want to allocate enough memory to hold a long integer, you can do如果你真的想分配足够的内存来保存一个长整数,你可以这样做

long* value = malloc(sizeof(*value));

and then assign to it as @David says:然后像@David 所说的那样分配给它:

*value = strtol(array, NULL, 10);

However, it is usually simpler to use a local variable to hold integers, not allocate them from the heap.然而,使用局部变量来保存整数通常更简单,而不是从堆中分配它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM