连接字符串后出现 Malloc() 内存损坏错误
伙计们,我正在生成一个字符串,它表示文件的路径,连接宏和字符串。函数是这样的:
char *userPath(char *username)
{
char *path = (char*)malloc(sizeof(char) * (strlen(MAILBOXES) + strlen(username) + 1));
path[0] = '\0';
strcat(path, MAILBOXES);
strcat(path, "/");
strcat(path, username);
return path;
}
返回的指针引用了正确的字符串,但是在调用该函数之后,该过程抛出了一个非常非常糟糕的* glibc检测到的。/ mmboxd: malloc(): 内存损坏: 0x085310a8 ** 带有相对回溯。我知道问题就在这里,因为我在实现它后就开始出现此错误,而且因为我使用的唯一 malloc 就在这里。这段代码有什么问题吗?
Guys I'm generating a string which rappresent a path to a file, concatenating a macro and a string. The function is this:
char *userPath(char *username)
{
char *path = (char*)malloc(sizeof(char) * (strlen(MAILBOXES) + strlen(username) + 1));
path[0] = '\0';
strcat(path, MAILBOXES);
strcat(path, "/");
strcat(path, username);
return path;
}
The returned pointer reference a correct string, but after some call to this function the process throws out a very very bad * glibc detected ./mmboxd: malloc(): memory corruption: 0x085310a8 ** with the relative backtrace. I know it's here the problem, since I started having this error once implemented it, and also because the only malloc I use is here. What's wrong with this piece of code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
考虑到您添加的分隔符和空终止符,+1 应为 +2。并且您可以省略 sizeof(char),它始终为 1。
The +1 should be +2 to take into account the separator you add and the null terminator. And you can omit sizeof(char), which will always be 1.
问题是:
您为 a) MAILBOXES 中的所有字符、b)
username
中的所有字符以及 c)'/ 分配了足够的内存'
字符,但您忘记了 d) 终止'\0'
字符!所以+ 1
应该是+ 2
您的代码还有一些其他奇怪之处,但它们并没有错,只是可以做得更好:
malloc
的返回值,有些人(比如我)认为这种风格很糟糕,原因有多种,你完全有能力进行谷歌搜索。sizeof(char)
始终为 1(这是在标准中定义的)。有人说要保持对称。有人说,既然是一个,就把它拿出来吧。有人说将其更改为sizeof *path
,这样,如果将path
更改为wchar_t *
,malloc
将正确调整以保持分配正确的尺寸。strcat
将第一位数据写入字符串可能效率较低。为什么不删除path[0] = '\0';
行并仅使用strcpy
作为数据的第一位?strcat
,它将重新遍历(之前计算的)长度以找到正确的位置。如果您存储了两次strlen
调用的结果,则无需使用strcat
并不必要地重新计算字符串结尾的位置。strcat
附加单个字符效率低下。malloc
之前,您不会检查它的返回值是否成功。Here's the problem:
You're allocating enough memory for a) all the characters in
MAILBOXES
, b) all the characters inusername
, and c) the'/'
character, but you're forgetting d) the terminating'\0'
character! So+ 1
should be+ 2
There are a few other oddities about your code, but they're not wrong, just things that could be better:
malloc
in C, and some (like me) consider it bad style for various reasons that you're more than capable of Googling.sizeof(char)
is always 1 (this is defined in the standard). Some people say to keep it in for symmetry. Some say take it out since it's one. Some say change it tosizeof *path
, so that if you changepath
to awchar_t *
themalloc
will correctly adjust to keep allocating the right size.strcat
to write the first bit of data to a string is potentially inefficient. Why not drop thepath[0] = '\0';
line and just usestrcpy
for the first bit of data?strcat
, which will re-traverse the (previously calculated) lengths to find the right spot. If you stored the results of your twostrlen
calls, you wouldn't need to usestrcat
and unnecessarily keep recalculating where the end of the string is.strcat
to append a single character is inefficient.malloc
for success or failure before you use it.您似乎没有为零终止符留出空间。您应该为此分配一个额外的
char
。我假设
malloc()
中的+1
用于路径分隔符。将其设置为+2
,这样您就有空间容纳终止空字符。You don't appear to have allowed space for the zero-terminator. You should be allocating an extra
char
for that.I'm assuming that the
+1
in themalloc()
is for the path separator. Make it+2
and you'll have space for the terminating null character.当您分配“路径”字符串时,您忘记添加在 MAILBOXES 和用户名之间添加的“/”字符的长度。
When you allocate the "path" string you forgot to add the length of the "/" char that you add between MAILBOXES and username.
看来您需要分配另一个字节以实现零终止。
It appears you would need to malloc an another byte for zero termination.
您需要为空字符“\x00”分配一个额外的字节作为 C 字符串中的字符串终止符。
目前,您只为 / 字符分配一个额外字节。
所以尝试+2而不是+1
You need to allocate one extra byte for null character "\x00" as the string terminator in C strings.
Currently you allocate only one extra byte for / character.
So try +2 instead of +1
您没有在 char 中为终止 null 进行预算。你的 malloc 长度应该是 +2,而不是 +1。
You're not budgeting in a char for the terminating null. Your malloc length should be +2, not +1.
malloc()
末尾的+1
占/
。但结尾处的空字符需要空间,该空间是由strcat()
添加的。所以它是+2
。Your
+1
in the end ofmalloc()
accounts for the/
. But you need space for the null character at the end, which is added bystrcat()
. So it's a+2
.