Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC++
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();
   }
Posted 24-Apr-13 10:27am
Edited 24-Apr-13 13:55pm
lewax0046.1K
v2
Comments
Richard MacCutchan at 24-Apr-13 15:46pm
   
Where did you get this message, and have you tried using your debugger to find the problem?
Member 9959824 at 24-Apr-13 17:06pm
   
When I ran the program , the problem may be in the code "strcat_s(kq,100,st)". In VC++ 2010 my debugger process in ASM code so I couldn't understand it.
Richard MacCutchan at 24-Apr-13 17:10pm
   
Well that does not help much either. Did you check the stack trace to see where it was called from in your code?
Member 9959824 at 24-Apr-13 17:22pm
   
I don't know , I think the complier manage that Stack ? I will check again to see that Stack but it's really hard because there are many ASM code when I debug in VC++ . Have you ever seen a same problem?
Richard MacCutchan at 24-Apr-13 17:28pm
   
What do you mean you don't know? See my solution below. You are modifying a piece of memory that you have not allocated so you are corrupting your address space.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

     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.
  Permalink  
v2
Comments
Member 9959824 at 24-Apr-13 17:38pm
   
I see. Because it work in TC and it's my first time use VC++ . Thanks! So I must remove the code st[1] = '\0' ?
Richard MacCutchan at 24-Apr-13 17:42pm
   
I was not aware that any C or C++ compiler would allocate more space than requested. Are you saying that in TurboC the statement char st[1] = "" will allocate 2 characters rather than 1?
Member 9959824 at 24-Apr-13 17:49pm
   
I see , Now I know my mistake. It's wrong when I make a string with statement char st[1] ;
Ian A Davidson at 24-Apr-13 20:26pm
   
No, you need to change it to: st[0] = '\0';
 
Arrays are zero-indexed, so when you create an array of 1 item, its index is 0.
 
I suspect TC is lagging behind with security checks, or perhaps the default compiler options are more relaxed. VC++ has been much improved over the years and default compiler settings more secure - e.g. many stack checking features are on by default, especially when compiling in debug. Look at what switches are set and look them up in MSDN to find out what they do, and familiarise yourself with what the defaults are in release and debug mode.
Regards,
Ian.
Richard MacCutchan at 25-Apr-13 3:36am
   
The issue is rather that he needs to allocate 2 elements to st.
Ian A Davidson at 25-Apr-13 4:41am
   
Yes, that is possibly what he wants to do, but to be honest there are so many problems with this code, who knows?
Ian.
Richard MacCutchan at 25-Apr-13 5:00am
   
Who knows what he really wants to do? That was just the obvious error that I spotted.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 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);
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();
}
  Permalink  
v2
Comments
Toan Pham Anh at 25-Apr-13 22:14pm
   
Thank you very much , my codes is for a problem . Here is it
http://en.wikipedia.org/wiki/Longest_common_substring_problem . Thank you again, I must learn to code in a tidier way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 325
1 DamithSL 265
2 CPallini 235
3 Sergey Alexandrovich Kryukov 229
4 Maciej Los 190
0 OriginalGriff 5,455
1 DamithSL 4,422
2 Maciej Los 3,860
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,010


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 26 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100