同样的逻辑代码为什么行为不同?

huangapple go评论72阅读模式
英文:

Why the behavior of same logic code is different?

问题

以下是您要翻译的部分:

"Why below code is throwing stack smashing detected if str1=”Overflow” & str2=”stack” but not if str1=”stack” & str2=”Overflow” ?"

output : Overflowstack
*** stack smashing detected ***: terminated

Here even if input strings are changed it should not give stack smash issue as we are allocating the sufficient space.

英文:

Why below code is throwing stack smashing detected if str1=”Overflow” & str2=”stack” but not if str1=”stack” & str2=”Overflow” ?

#include <iostream>
#include<cstring>
using namespace std;
void string_Concat (char *ptr1, const char *ptr2)
{
  int length1 = strlen (ptr1);
  int length2 = strlen (ptr2);
  int i, j;

  char *temp = ptr1;
  ptr1 = new char[length1 + length2 + 1];
  ptr1 = temp;

  for (i = length1, j = 0; ptr2[j] != '
#include <iostream>
#include<cstring>
using namespace std;
void string_Concat (char *ptr1, const char *ptr2)
{
int length1 = strlen (ptr1);
int length2 = strlen (ptr2);
int i, j;
char *temp = ptr1;
ptr1 = new char[length1 + length2 + 1];
ptr1 = temp;
for (i = length1, j = 0; ptr2[j] != '\0'; i++, j++)
ptr1[i] = ptr2[j];
ptr1[i] = '\0';
}
int
main ()
{
char str1[] = "Overflow";
char str2[] = "stack";
string_Concat (str1, str2);
std::cout << str1 << std::endl;
return 0;
}
'; i++, j++) ptr1[i] = ptr2[j]; ptr1[i] = '
#include <iostream>
#include<cstring>
using namespace std;
void string_Concat (char *ptr1, const char *ptr2)
{
int length1 = strlen (ptr1);
int length2 = strlen (ptr2);
int i, j;
char *temp = ptr1;
ptr1 = new char[length1 + length2 + 1];
ptr1 = temp;
for (i = length1, j = 0; ptr2[j] != '\0'; i++, j++)
ptr1[i] = ptr2[j];
ptr1[i] = '\0';
}
int
main ()
{
char str1[] = "Overflow";
char str2[] = "stack";
string_Concat (str1, str2);
std::cout << str1 << std::endl;
return 0;
}
'; } int main () { char str1[] = "Overflow"; char str2[] = "stack"; string_Concat (str1, str2); std::cout << str1 << std::endl; return 0; }

output :
Overflowstack
*** stack smashing detected ***: terminated

Here even if input strings are changed it should not give stack smash issue as we are allocating the sufficient space.

答案1

得分: 1

这完全错了。你有多个问题。

首先,这段代码:

char str1[] = "...";

这只分配了足够的空间来存储字符串和结尾的空字符。如果你尝试追加数据,最终会遇到跟随它的任何数据。这是错误的。你绝对不能这样做。

然后你执行了这段代码:

char *temp = ptr1;
ptr1 = new char[length1 + length2 + 1];
ptr1 = temp;

这里发生了什么。temp 指向 ptr1 的值。然后你为 ptr1 分配了新的空间 - 这是好的。然后你销毁指针并将其设置回 temp - 原始的 ptr1。这是内存泄漏,毫无意义。

当然,正确的做法是使用 C++ 字符串。但你正在学习,所以几乎就是你所做的第二种正确的方法。

以你所做的方式分配空间(有点类似),然后将两个字符串都复制到其中,然后返回该值。像这样:

char * stringConcat(const char * str1, const char * str2) {
    // 获取长度
    char * newStr = new char[length1 + length2 + 1];
    // 将两个输入字符串附加到 newStr,有点像你所做的
    return newStr;
}

在主函数中,你应该保留返回的指针并打印它,有点像你所做的方式。

英文:

This is all kinds of wrong. You have multiple problems.

First, this code:

char str1[] = "...";

This allocates just enough space to store the string plus the trailing null byte. If you try appending to it, you're eventually going to run into whatever data follows it. This is BAD. You flat out can't.

Then you do this code:

char *temp = ptr1;
ptr1 = new char[length1 + length2 + 1];
ptr1 = temp;

What happens here. temp points to the value of ptr1. Then you allocate new space for ptr1 -- okay, that's cool. You then destroy the pointer and set it back to temp -- the original ptr1. This is a memory leak and accomplishes nothing at all.

The right way to do this, of course, is to use C++ strings. But you're just learning, so the second-right way to do this is almost what you have.

Allocate space the way you did (sort of), and copy both strings to it, and then return that value. Like this:

char * stringConcat(const char * str1, const char * str2) {
     // get the lengths
     char * newStr = new char[length1 + length2 + 1];
     // append the two input strings to newStr kind of like what you did
     return newStr;
}

And in main, you would do keep the returned pointer and print it kind of like you did.

huangapple
  • 本文由 发表于 2023年2月10日 03:04:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定