简体   繁体   English

当我知道char数组的最大大小时使用malloc

[英]using malloc when I know the maximum size of the char array

In my program, I'm declarign a character array holding the location of a config file, it should be something like: "/home/user/.config" 在我的程序中,我声明一个保存配置文件位置的字符数组,它应该类似于:“ /home/user/.config”

now I understand the longest username can be 32 bytes long(GNU Linux), so I know that array will not hold more than 46 characters, in this case should I be using malloc or not. 现在我知道最长的用户名可以是32个字节长(GNU Linux),所以我知道数组最多可以容纳46个字符,在这种情况下,是否应该使用malloc。

should I use: 我应该使用:

char config_file_location[46];
strcpy (config_file_location, getenv("HOME"));
strcat(config_file_location,"/.config");

or: 要么:

char *config_file_location;
config_file_location = (char *) malloc(43);
strcpy (config_file_location, getenv("HOME"));
strcat(config_file_location,"/.config");
//code goes here
free(config_file_location);

also should I use realloc in the above example to get the config_file_location to use exactly the amount of memory it is supposed to? 我还应该在上面的示例中使用realloc来获取config_file_location来确切使用它应该使用的内存量吗?

I'm looking for best practice info, if it is not worth doing in this case, I would like to know when it would be, and I would like to know the reason behind which approach is better. 我正在寻找最佳实践信息,如果这种情况下不值得这样做,我想知道何时发布,并且想知道哪种方法更好的原因。

Thanks I appreciate it. 谢谢,我很感激。

There are two reasons why you would use dynamic allocation: 使用动态分配的原因有两个:

  • Either because the amount of memory needed isn't known at compile-time, or because the amount of memory needs to be reallocated in run-time. 这是因为所需的内存量在编译时未知,或者是因为需要在运行时重新分配内存量。
  • Or because you need to allocate large amounts of data and don't want to burden the stack with it. 或者是因为您需要分配大量数据,而又不想给堆栈增加负担。 Allocating too much memory on the stack can in the worst case lead to mysterious run-time crashes caused by stack overflow. 在最坏的情况下,在堆栈上分配过多的内存可能导致由堆栈溢出引起的神秘的运行时崩溃。 To avoid this, large amounts of data should be allocated on the heap instead. 为避免这种情况,应改为在堆上分配大量数据。

In your case, you have a fixed amount of data and 43 bytes is hardly a large amount. 就您而言,您有固定数量的数据,而43个字节几乎不是很大。 So there is no need to use dynamic allocation here. 因此,这里无需使用动态分配。

Apart from the usual issues with memory leaks and heap fragmentation, you also have to consider that each call to malloc (and free) is quite time-consuming. 除了常见的内存泄漏和堆碎片问题之外,您还必须考虑到每次对malloc的调用(和释放)都非常耗时。 On systems where dynamic allocation is feasible (such as Linux), it almost always makes more sense to optimize for speed instead of memory consumption. 在可行的动态分配系统(例如Linux)上,优化速度而不是消耗内存几乎总是更有意义。

Unless you are working in some really, really memory constrained environment, I wouldn't be worried about optimising how much memory your application uses. 除非您在确实受内存限制的环境中工作,否则我不会担心优化应用程序使用多少内存。 Just allocate a buffer on the stack that is "big enough" for the largest path you might encounter. 只需在堆栈上分配一个“足够大”的缓冲区,用于可能遇到的最大路径。

As to how big that is, no one is going to give you a definite answer. 至于有多大,没人会给你一个明确的答案。 You could use PATH_MAX, although it has been noted even that has problems. 您可以使用PATH_MAX,尽管已经注意到它存在问题。 In these situations I would just take a pragmatic approach and go for something like 256 bytes. 在这种情况下,我只会采取务实的方法,并选择256个字节。 Job done. 任务完成。 Move on. 继续。

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

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