C:缺少一些关于指针的逻辑
我正在编写自己的字符串复制函数。以下有效:
char *src, *dest;
src = (char *) malloc(BUFFSIZE);
//Do something to fill the src
dest = (char *) malloc(strlen(src) + 1);
mystringcpy(src, dest);
void mystringcopy(char *src, char *dest) {
for(; (*dest = *src) != '\0'; ++src, +dest);
}
但这不起作用:
char *src, *dest;
src = (char *) malloc(BUFFSIZE);
//Do something to fill the src
mystringcpy(src, strlen(src), dest);
void mystringcopy(char *src, size_t length, char *dest) {
dest = (char *)malloc(length + 1);
for(; (*dest = *src) != '\0'; ++src, +dest);
}
我不明白为什么......在被调用函数内分配内存是一个错误吗?
I am writing my own string copy function. The following works:
char *src, *dest;
src = (char *) malloc(BUFFSIZE);
//Do something to fill the src
dest = (char *) malloc(strlen(src) + 1);
mystringcpy(src, dest);
void mystringcopy(char *src, char *dest) {
for(; (*dest = *src) != '\0'; ++src, +dest);
}
But this doesn't work:
char *src, *dest;
src = (char *) malloc(BUFFSIZE);
//Do something to fill the src
mystringcpy(src, strlen(src), dest);
void mystringcopy(char *src, size_t length, char *dest) {
dest = (char *)malloc(length + 1);
for(; (*dest = *src) != '\0'; ++src, +dest);
}
and I can't understand why... Is allocating memory inside a called function a mistake?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您还没有真正说明“有效”的含义,但我假设您很困惑为什么
dest
没有在调用函数中更改为新内存。原因是在您的
mystringcopy
函数中,参数dest
是调用中指针dest
的副本功能。然后,您将该副本分配给一个新的缓冲区,进行复制,然后该副本就会消失。原件不变。您需要将
dest
作为指针(指向指针)传递。另外,我假设您从内存中编写了您所做的事情,因为它不应该按原样编译(调用函数中的错误取消引用)。下面是固定代码:
另一种方法是返回
dest
指针:请记住,如果长度小于源缓冲区的实际长度,则会超出目标缓冲区。请参阅解决方案的评论,尽管这由您决定。
You haven't really said what "works" means, but I'm assuming you're confused why
dest
isn't being changed to the new memory back in the calling function.The reason is that in your
mystringcopy
function, the parameterdest
is a copy of the pointerdest
in the calling function.You then assign that copy to a new buffer, do the copy, and then the copy goes away. The original is unchanged. You need to pass
dest
as a pointer (to a pointer).Also, I assume you wrote what you did from memory since it shouldn't compile as is (bad dereference in the calling function). Here's the fixed code:
Another method is to return the
dest
pointer:Keep in mind if length is smaller than the source buffer's real length that you'll overrun your destination buffer. See the comments for a solution, though this is left up to you.
在函数内部执行 malloc 是可以的,但您不会将指针从函数中传回。返回指针:
或将指针传递给指针:
Doing the malloc inside the function is OK, but you're not passing the pointer back out of the function. Either return the pointer:
or pass a pointer to the pointer:
C 中的参数按值传递,因此您的函数会获取
dest
指针的副本,用malloc
覆盖它,然后丢弃它。试试这个:现在你将一个指针传递给你的字符串,这样你就可以在主过程中覆盖它。你会这样使用它:
Parameters in C are passed by value, so your function gets a copy of the
dest
pointer, overwrites it withmalloc
then discards it. Try this instead:Now you pass a pointer to the pointer to your string, so you can overwrite it in the main procedure. You'd use it like:
在函数内部分配没有问题。
问题是在 C 中参数是按值传递的。因此,当您为 dest 赋值时,这只是修改函数本地的 dest。
你有两个选择。您可以返回 dest 指针:
或者您可以将指针传递给参数并修改所指向的内容:
不需要使用局部变量的技术,但我认为它可以使代码更具可读性。
There is no problem in allocating inside a function.
The problem is that in C arguments are passed by value. So when you assign a value to dest, that's only modifying the dest local to the function.
You have two choices. You can return the dest pointer:
or you can pass a pointer to the argument and modify what's being pointed to:
The technique of using a local variable is not required, but I think it makes for more readable code.
一般来说,在分配内存时,对于哪些代码负责在完成后释放内存有一定的假设。我赞同这样一种观念:函数应该负责一项主要操作,就像一个黑匣子一样。出于这两个原因,最好分配自己的内存并将指针传递给函数以填充其缓冲区。
除此之外,您可以返回 char * 指针作为返回值。
或者,将
char *dest
参数更改为char **dest
。然后,调用如下函数:mystringcopy(src, strlen(src), *dest)
。在函数中,它通过以下方式返回指针:*dest = (char *)malloc(length + 1);
。不漂亮。In general, when allocating memory there are certain assumptions about what code is responsible for freeing the memory when it's done. I subscribe to the notion that a function should be responsible for one major operation, as if a black box. For both reasons, it is best to allocate your own memory and hand the pointer to the function to populate its buffer.
That aside, you could either return the
char *
pointer as a return value.Or, change the
char *dest
parameter tochar **dest
. Then, call the function like:mystringcopy(src, strlen(src), *dest)
. And in the function, it returns the pointer by:*dest = (char *)malloc(length + 1);
. Not pretty.