指针失败 (C++)
可能的重复:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您的定义指定了一个 char 数组指针作为其返回类型,但您在函数内初始化了一个 char 数组并尝试返回它。试试这个:
}
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:
}
问题是你从来没有在堆上分配 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.
这应该有效:
编辑:完成后,不要忘记使用“删除”来释放内存;)
This should work:
EDIT: when you are done, don't forget to use 'delete' to free the memory ;)
你不能返回 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)
您正在返回在堆栈上创建的局部变量的地址。当您从函数返回时,堆栈将倒带并且您的
dup
变量消失。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.该行在
C++ 中不起作用。数组需要在编译时指定一个常量大小; strlen(s) 是一个变量。
就您的实际警告而言,向调用者返回指向局部变量的指针是一种不好的做法;由于局部变量(在本例中为数组 dup)是在堆栈上分配的,因此当函数返回时,它会被释放,因此返回的指针可能无效。编译器旨在捕获此类错误并标记警告,指出这可能是潜在的问题来源。
The line
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.
dup
变量是一个char
数组,分配在堆栈上,而不是堆上(通过>new
或malloc
)。一旦堆栈帧离开(即:函数离开),这就是未定义的内存,很快就会被其他东西覆盖。您需要将
dup
转换为char *
并使用new
或malloc
来分配必要的内存。The
dup
variable is an array ofchar
and is allocated on the stack rather than the heap (vianew
ormalloc
). 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 achar *
and usenew
ormalloc
to allocate the necessary memory.char dup[strlen(s)]
定义一个本地堆栈变量;当函数结束时,this 就会超出范围,因此此后任何访问它的尝试都将导致未定义的行为。您的选择是:
new
获得)。您必须记住在某个时候删除
它。void strdup(char *out, const char *in)
)。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:
new
). You will have to remember todelete
it at some point.void strdup(char *out, const char *in)
).std::string
, which do all the hard work for you.As you have marked your question "C++", I strongly recommend Option #3.