指针失败 (C++)

发布于 2024-10-25 15:05:28 字数 430 浏览 3 评论 0原文

可能的重复:
c++ 警告:局部变量的地址

char* strdup(const char* s)
{
    char dup[strlen(s)];
    for (int i = 0; i<strlen(s); i++)
    {
        dup[i]=s[i];
    }
    return dup;
}

该函数应该保存新数组已向后读取并加上另一个插槽。当我编译它时,出现错误“警告:局部变量'dup'的地址返回”,当我运行程序时,它返回内存地址。

Possible Duplicate:
c++ warning: address of local variable

char* strdup(const char* s)
{
    char dup[strlen(s)];
    for (int i = 0; i<strlen(s); i++)
    {
        dup[i]=s[i];
    }
    return dup;
}

This function is supposed to hold the new array that has been read backwards plus another slot. When I compile it I get the error "warning: address of local variable 'dup' returned" and when I run the program it returns the memory address.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

梦在深巷 2024-11-01 15:05:31

您的定义指定了一个 char 数组指针作为其返回类型,但您在函数内初始化了一个 char 数组并尝试返回它。试试这个:

char* strdup(const char* s)
{
    char *dup = new char[strlen(s)];
    for (int i = 0; i<strlen(s); i++)
    {
        dup[i]=s[i];
    }
    return dup;

}

Your definition specifies an char array pointer as its return type but you initialize a char array inside your function and try to return it. Try this:

char* strdup(const char* s)
{
    char *dup = new char[strlen(s)];
    for (int i = 0; i<strlen(s); i++)
    {
        dup[i]=s[i];
    }
    return dup;

}

撩心不撩汉 2024-11-01 15:05:31

问题是你从来没有在堆上分配 dup ,所以当你退出栈帧时, dup 会自动和栈帧一起被删除。这意味着不可能有对 dup 的有效引用,因为一旦退出该函数,它就不再存在。

The problem is that you never allocate dup on the heap, so when you exit the stack frame, dup will automatically be removed with the stack frame. This means that it's not possible to have a valid reference to dup, as it ceases to exist once you exit the function.

猥︴琐丶欲为 2024-11-01 15:05:31

这应该有效:

char* strdup(const char* s)
{
    char* dup = new char[strlen(s)];
    for (int i = 0; i<strlen(s); i++)
    {
        dup[i]=s[i];
    }
    return dup;
}

编辑:完成后,不要忘记使用“删除”来释放内存;)

This should work:

char* strdup(const char* s)
{
    char* dup = new char[strlen(s)];
    for (int i = 0; i<strlen(s); i++)
    {
        dup[i]=s[i];
    }
    return dup;
}

EDIT: when you are done, don't forget to use 'delete' to free the memory ;)

迷路的信 2024-11-01 15:05:31

你不能返回 dup[] ,因为它是一个局部变量,在函数外部无效(好吧,它指向的内存将不再有效)。您必须调用类似 malloc() 的方法,它在堆上分配内存(所有应用程序都可见的空间)

you can't return dup[] because, as it is, it's a local variable and won't be valid outside the function (well, the memory it points to won't be valid anymore). You have to call something like malloc(), which allocates memory on the heap (space visible by all your app)

黯然#的苍凉 2024-11-01 15:05:31
char* strdup(const char* s)
{
    char dup[strlen(s)]; // defines *local* variable on *stack*
    for (int i = 0; i<strlen(s); i++)
    {
        dup[i]=s[i];
    }
    return dup; // returning dup[0] = dup address
}

您正在返回在堆栈上创建的局部变量的地址。当您从函数返回时,堆栈将倒带并且您的dup变量消失。

char* strdup(const char* s)
{
    char dup[strlen(s)]; // defines *local* variable on *stack*
    for (int i = 0; i<strlen(s); i++)
    {
        dup[i]=s[i];
    }
    return dup; // returning dup[0] = dup address
}

You are returning address of local variable, created on stack. When you return from the function the stack will be rewind and your dup variable gone.

骄傲 2024-11-01 15:05:31

该行在

char dup[strlen(s)];

C++ 中不起作用。数组需要在编译时指定一个常量大小; strlen(s) 是一个变量。

就您的实际警告而言,向调用者返回指向局部变量的指针是一种不好的做法;由于局部变量(在本例中为数组 dup)是在堆栈上分配的,因此当函数返回时,它会被释放,因此返回的指针可能无效。编译器旨在捕获此类错误并标记警告,指出这可能是潜在的问题来源。

The line

char dup[strlen(s)];

will not work in C++. Arrays need a constant size specified at compile time; strlen(s) is a variable.

As far as your actual warning is concerned, it is a bad practice to return a pointer to a local variable to the caller; since the local variable (in this case, the array dup) is allocated on the stack, when the function returns, it is deallocated, and hence, the returned pointer may be invalid. Compilers are designed to catch such errors and flag a warning saying that this could be a potential source of problems.

避讳 2024-11-01 15:05:31

dup 变量是一个 char 数组,分配在堆栈上,而不是上(通过>newmalloc)。一旦堆栈帧离开(即:函数离开),这就是未定义的内存,很快就会被其他东西覆盖。

您需要将dup转换为char *并使用newmalloc来分配必要的内存。

The dup variable is an array of char and is allocated on the stack rather than the heap (via new or malloc). As soon as the stack frame is left (that is: the function is left) this is undefined memory that will be overwritten by other things soon.

You need to turn dup into a char * and use new or malloc to allocate the necessary memory.

小猫一只 2024-11-01 15:05:30

char dup[strlen(s)] 定义一个本地堆栈变量;当函数结束时,this 就会超出范围,因此此后任何访问它的尝试都将导致未定义的行为

您的选择是:

  1. 使用堆变量(使用 new 获得)。您必须记住在某个时候删除它。
  2. 让函数写入调用者提供的现有缓冲区(例如void strdup(char *out, const char *in))。
  3. 使用 C++ 结构,例如 std::string,它可以完成所有操作为你辛苦工作。

由于您已将问题标记为“C++”,因此我强烈推荐选项#3。

char dup[strlen(s)] defines a local stack variable; this goes out of scope when the function ends, so any attempt to access it after that will result in undefined behaviour.

Your options are:

  1. Use a heap variable (obtained using new). You will have to remember to delete it at some point.
  2. Have the function write into an existing buffer, provided by the caller (e.g. void strdup(char *out, const char *in)).
  3. Use C++ constructs like std::string, which do all the hard work for you.

As you have marked your question "C++", I strongly recommend Option #3.

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