帮我找到问题 [英] Help Me Find The Problem

查看:64
本文介绍了帮我找到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据说:运行时检查失败#2 - 变量''st''周围的堆栈已损坏。当我运行代码时。变量''st'会发生什么?



It''s said :"Run-Time Check Failure #2 - Stack around the variable ''st'' was corrupted." when I run the codes. What happen to variable ''st'' ?

#include <conio.h>
#include <stdio.h>
#include <string.h>
  int *Timxaucon(char s1[100], char s2[100], int &z)
  {
     int m,n,i,j,*a;
     z = 0;
     m = strlen(s1);
     n = strlen(s2);
     a = new int[(m+1)*(n+1)];
     for(i=0;i<m+1;i++)
	for(j=0;j<n+1;j++)
	{
	   if(i==0||j==0)
	   {
	      if(i==0) a[j]=0;
	      if(j==0) a[i*(n+1)]=0;
	      continue;
	   }
	   if(s1[i-1]==s2[j-1])
	   {
	      if(i==1||j==1) a[i*(n+1)+j]=1;
	      else a[i*(n+1)+j] = a[(i-1)*(n+1)+j-1]+1;
	      if (a[i*(n+1)+j] > z) z=a[i*(n+1)+j];
	   }
	   else a[i*(n+1)+j] = 0;
	}
      return a;
   }
   void xuat(int *a, int z,char s1[100], char s2[100])
   {
     int m,n,i,j,l,k,q,dem=1;
     char st[1]="",kq[100]="";
     st[1]='\0';
     m = strlen(s1);
     n = strlen(s2);
     for(i=1;i<m+1;i++)
	for(j=1;j<n+1;j++)
	{
	   if (a[i*(n+1)+j] == z)
	   {
	      strcpy_s(kq,100,"");
	      l=i;
	      k=j;
	      q=0;
	      while(a[l*(n+1)+k]>0&&l>0&&k>0)
	      {
		 st[0]=s1[l-1];
		 strcat_s(kq,100,st);
		 l--;
		 k--;
		 q++;
	      }
	      kq[q]='\0';
	      printf("Xau thu %d : ",dem);
	      for(l=q-1;l>=0;l--) printf("%c",kq[l]);
	      printf("\n");
	      dem++;
	   }
       }
   }
   void main()
   {
      char s1[100],s2[100];
      int *a=NULL;
      int z;
      fflush(stdin);
      printf("Nhap chuoi 1 : ");gets_s(s1,100);
      printf("Nhap chuoi 2 : ");gets_s(s2,100);
      a=Timxaucon(s1,s2,z);
      xuat(a,z,s1,s2);
      _getch();
   }

推荐答案

char st[1]="",kq[100]="";
st[1]='\0';



您声明一个包含一个元素的数组,然后在第二个(不存在的)元素中设置一个值。结果很痛苦。



你也可以通过在你的陈述中加上适当的间距,并使用有用的变量名而不是一个和两个字母符号来让自己更容易。很大程度上没有意义。


You declare an array with one element and then set a value in the second (non-existent) element. Result misery.

You could also make it easier for yourself by putting some proper spacing in your statements, and using useful variable names rather than one and two letter symbols, which are largely meaningless.


是的,我已经把你的代码整理好了,而没有改变功能。

然而,最令人担忧的事情是这段代码即使在完成所有这些之后,我仍然无法看到代码的作用。

如果我输入hello和goodbye它会输出e,o和o 。

这一切意味着什么?



你需要发表评论!!



无论如何,在回答你的问题时,我现在可以看到主要问题确实是你需要将st分配为2字符数组,并确保两个字符都设置为零:

Right, well I''ve taken your code and tidied it up, without changing the functionality.
However, the most worrying thing about this code is that even after doing all this, I still cannot see what the code does.
If I enter "hello" and "goodbye" it outputs "e", "o" and "o".
What does it all mean?

You need to comment it!!

Anyway, in answer to your question, I can now see that the main problem is indeed that you need to allocated st as a 2 char array, and ensure both chars are set to zero:
char st[2];
::memset(st, 0, sizeof(st));



因为这些行:


because of these lines:

st[0] = s1[l-1];
strcat_s(kq, _countof(kq), st);



这里设置st字符串的第一个字符(在你的情况下唯一的一个) )然后做一个字符串连接。由于你没有为st分配2个字节,你不能保证遇到的第二个字节将是空终止符,所以你最终可以连接任意数量的字符,直到目标字符串的最大数量,我就是我肯定不是你想要的。所以你需要分配2个字节,并确保至少第二个字节初始化为零(memset整个事情为零不会伤害,这是一个好习惯)。



无论如何,这是我的代码的整理版本,可能有助于您澄清问题。再次,我强烈建议您评论此代码,以解释每个函数正在做什么以及为什么。



问候,

Ian。


Here you set the first character of the "st" string (the only one in your case) and then do a string concatenation. Since you are not allocating 2 bytes to st you can''t guarantee that the second byte encountered will be the null terminator, so you could end up concatenating any number of characters up to the maximum count of the destination string, which I''m sure is not what you want. So you need to allocate 2 bytes and ensure at least the second is initialised to zero (memset the entire thing to zero can''t hurt and is good practice).

Anyway, here''s my tidied up version of your code, which might help you to clarify matters. Again, I highly recommend that you comment this code to explain what on earth each of the functions is doing and why.

Regards,
Ian.

#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>

const int* Timxaucon
    (
    const char* const s1,
    const char* const s2,
    int& z
    )
{
    z = 0;
    const int m = strlen(s1);
    const int n = strlen(s2);
    int* const a = new int[(m+1)*(n+1)];
    for (int i = 0; i < m+1; ++i)
    {
        for (int j = 0; j < n+1; ++j)
        {
            if ((i == 0) || (j==0))
            {
                if (i == 0)
                {
                    a[j] = 0;
                }
                if (j == 0)
                {
                    a[i*(n+1)]=0;
                }
            }
            else if (s1[i-1] == s2[j-1])
            {
                if ((i==1) || (j==1))
                {
                    a[i*(n+1)+j] = 1;
                }
                else
                {
                    a[i*(n+1)+j] = a[(i-1)*(n+1)+j-1] + 1;
                }
                if (a[i*(n+1)+j] > z)
                {
                    z = a[i*(n+1)+j];
                }
            }
            else
            {
                a[i*(n+1)+j] = 0;
            }
        }
    }
    return a;
}

void xuat
    (
    const int* const a,
    const int z,
    const char* const s1,
    const char* const s2
    )
{
    int dem=1;
    char st[2];
    char kq[100];
    ::memset(st, 0, sizeof(st));
    ::memset(kq, 0, sizeof(kq));

    const int m = strlen(s1);
    const int n = strlen(s2);
    for (int i = 1; i < m+1; ++i)
    {
        for (int j = 1; j < n+1; ++j)
        {
            if (a[i*(n+1)+j] == z)
            {
                ::memset(kq, 0, sizeof(kq));
                int l = i;
                int k = j;
                int q = 0;
                while ((a[l*(n+1)+k] > 0) && (l > 0) && (k > 0))
                {
                    st[0] = s1[l-1];
                    strcat_s(kq, _countof(kq), st);
                    --l;
                    --k;
                    ++q;
                }
                printf("Xau thu %d : ", dem);
                for (l = q - 1; l >= 0; --l)
                {
                    printf("%c", kq[l]);
                }
                printf("\n");
                ++dem;
            }
        }
    }
}

void main()
{
    char s1[100];
    char s2[100];
    fflush(stdin);

    printf("Nhap chuoi 1 : ");
    gets_s(s1, _countof(s1));

    printf("Nhap chuoi 2 : ");
    gets_s(s2, _countof(s2));

    int z;
    const int* a = Timxaucon(s1, s2, z);
    xuat(a, z, s1, s2);
    _getch();
}


这篇关于帮我找到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆