C:缺少一些关于指针的逻辑

发布于 2024-08-20 07:12:52 字数 643 浏览 4 评论 0原文

我正在编写自己的字符串复制函数。以下有效:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

潜移默化 2024-08-27 07:12:52

您还没有真正说明“有效”的含义,但我假设您很困惑为什么 dest 没有在调用函数中更改为新内存。

原因是在您的 mystringcopy 函数中,参数 dest 是调用中指针 dest副本功能。

然后,您将该副本分配给一个新的缓冲区,进行复制,然后该副本就会消失。原件不变。您需要将 dest 作为指针(指向指针)传递。

另外,我假设您从内存中编写了您所做的事情,因为它不应该按原样编译(调用函数中的错误取消引用)。下面是固定代码:

char *src, *dest;
src = (char *)malloc(BUFFSIZE); // no dereference on src, it's a pointer

//Do something to fill the src
mystringcpy(src, strlen(src), &dest); // pass the address of dest

// take a pointer to a char*
void mystringcopy(char *src, size_t length, char **dest) {
    // now you should dereference dest, to assign to
    // the char* that was passed in
    *dest = (char *)malloc(length + 1);

    // for simplicity, make an auxiliary dest
    char* destAux = *dest;

    // and now the code is the same
    for(; (*destAux = *src) != '\0'; ++src, ++destAux);
}

另一种方法是返回 dest 指针:

char *src, *dest;
src = (char *)malloc(BUFFSIZE);

//Do something to fill the src
dest = mystringcpy(src, strlen(src)); // assign dest

char* mystringcopy(char *src, size_t length) {
    char* dest = (char *)malloc(length + 1);

    // for simplicity, make an auxiliary dest
    char* destAux = dest;

    for(; (*destAux = *src) != '\0'; ++src, ++destAux);

    return dest; // give it back
}

请记住,如果长度小于源缓冲区的实际长度,则会超出目标缓冲区。请参阅解决方案的评论,尽管这由您决定。

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 parameter dest is a copy of the pointer dest 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:

char *src, *dest;
src = (char *)malloc(BUFFSIZE); // no dereference on src, it's a pointer

//Do something to fill the src
mystringcpy(src, strlen(src), &dest); // pass the address of dest

// take a pointer to a char*
void mystringcopy(char *src, size_t length, char **dest) {
    // now you should dereference dest, to assign to
    // the char* that was passed in
    *dest = (char *)malloc(length + 1);

    // for simplicity, make an auxiliary dest
    char* destAux = *dest;

    // and now the code is the same
    for(; (*destAux = *src) != '\0'; ++src, ++destAux);
}

Another method is to return the dest pointer:

char *src, *dest;
src = (char *)malloc(BUFFSIZE);

//Do something to fill the src
dest = mystringcpy(src, strlen(src)); // assign dest

char* mystringcopy(char *src, size_t length) {
    char* dest = (char *)malloc(length + 1);

    // for simplicity, make an auxiliary dest
    char* destAux = dest;

    for(; (*destAux = *src) != '\0'; ++src, ++destAux);

    return dest; // give it back
}

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.

凡间太子 2024-08-27 07:12:52

在函数内部执行 malloc 是可以的,但您不会将指针从函数中传回。返回指针:

char * mystringcopy(char *src)

或将指针传递给指针:

void mystringcopy(char *src, char **dest)

Doing the malloc inside the function is OK, but you're not passing the pointer back out of the function. Either return the pointer:

char * mystringcopy(char *src)

or pass a pointer to the pointer:

void mystringcopy(char *src, char **dest)
呆° 2024-08-27 07:12:52

C 中的参数按值传递,因此您的函数会获取 dest 指针的副本,用 malloc 覆盖它,然后丢弃它。试试这个:

void mystringcopy(char *src, size_t length, char **dest) {
   *dest = (char *)malloc(length + 1);
   char *p=*dest;
   for(; (*p = *src) != '\0'; ++src, ++p);
}

现在你将一个指针传递给你的字符串,这样你就可以在主过程中覆盖它。你会这样使用它:

char *src, *dest;
*src = (char *) malloc(BUFFSIZE);
//Do something to fill the src
mystringcpy(src, strlen(src), &dest);
// now in dest you have your copy

Parameters in C are passed by value, so your function gets a copy of the dest pointer, overwrites it with malloc then discards it. Try this instead:

void mystringcopy(char *src, size_t length, char **dest) {
   *dest = (char *)malloc(length + 1);
   char *p=*dest;
   for(; (*p = *src) != '\0'; ++src, ++p);
}

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:

char *src, *dest;
*src = (char *) malloc(BUFFSIZE);
//Do something to fill the src
mystringcpy(src, strlen(src), &dest);
// now in dest you have your copy
演多会厌 2024-08-27 07:12:52

在函数内部分配没有问题。

问题是在 C 中参数是按值传递的。因此,当您为 dest 赋值时,这只是修改函数本地的 dest。

你有两个选择。您可以返回 dest 指针:

char *alloc_and_copy(const char *src, size_t length)
{
    char *dest = malloc(length + 1);
    ... do your copying
    return dest;
}

或者您可以将指针传递给参数并修改所指向的内容:

void alloc_and_copy(const char *src, size_t length, char **dest)
{
    char *local_dest = malloc(length + 1);
    ... do your copying using local_dest

    *dest = local_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:

char *alloc_and_copy(const char *src, size_t length)
{
    char *dest = malloc(length + 1);
    ... do your copying
    return dest;
}

or you can pass a pointer to the argument and modify what's being pointed to:

void alloc_and_copy(const char *src, size_t length, char **dest)
{
    char *local_dest = malloc(length + 1);
    ... do your copying using local_dest

    *dest = local_dest;
}

The technique of using a local variable is not required, but I think it makes for more readable code.

梦明 2024-08-27 07:12:52

一般来说,在分配内存时,对于哪些代码负责在完成后释放内存有一定的假设。我赞同这样一种观念:函数应该负责一项主要操作,就像一个黑匣子一样。出于这两个原因,最好分配自己的内存并将指针传递给函数以填充其缓冲区。

除此之外,您可以返回 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 to char **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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文