C++: Heap corruption detected after normal block

1.1k views Asked by At

I am assigned with a task in which I should create some functions that does some tricks on char arrays. So I created those functions and I was trying to see if they are working correctly when I got stuck with this "HEAP CORRUCPTION DETECTED" error.

Here is the code of my function

void cagir(char cDizi[], short int baslangic, short int karSayisi, char cDondur[])
{
    //Yerel Değişken
    short int index = 0;

    /*Diziyi baslangic değerinden başlayarak, NULL karakterine ulaşılmadığı ve baslangic değeri 
      baslangic + karSayisi'na eşit ya da küçük olduğu sürece oku                              */
    for (; cDizi[baslangic] != '\0' && baslangic < baslangic + karSayisi; baslangic++)
    {
    //Okunan karakteri cDöndür dizisine yerleştir
    cDondur[index] = cDizi[baslangic];
    index += 1;  //cDöndür dizisinin index'ini 1 artır
    }
}

What I am trying to do here is basicly getting the specified amount of characters from 'cDizi' and put them in the other precreated array 'cDondur'. The array 'cDizi' is an array of user input, and 'cDondur' is an array created in the related part of the program. I wanted to assign the number of characters to be called as the range of the array 'cDondur'.

Here is the part I call the function in my program, and create the 'cDondur' array

case '3':  //MOD - 3
    {

           short int indis, karSayisi;

           //Çağrılacak parçanın ilk karakterinin indisini öğren
           cout << "Almak istediğiniz ilk karakterin indisi : ";
           cin >> indis;

           //kaç karakterlik parça çağrılacağını öğren
           cout << indis << " indisli karakterden itibaren almak istediğiniz karakter sayısı : ";
           cin >> karSayisi;

           //Sınırı karSayisi olan bir dizi oluştur
           char *Dizi = new char[karSayisi];

           //Parça çağırma fonksiyonunu çağır
           cagir(cDizi, indis, karSayisi, Dizi);

           cout << endl;

           cout << "İstediğiniz karakterler çağrıldı : ";

           //cagir fonksiyonunun sonucu olan 'Dizi' dizisini ekrana yaz
           for (int i = 0; i < karSayisi; i++)
               cout << Dizi[i];

           delete Dizi;

           break;
    }

karSayisi here is the amount of characters I am going to get from the array and elemanSayisi(ARRAY) is a function that returns the amount of characters of its parameter.

I really need to find a way to fix this quickly, and I have been working on the code for like 3 hours straight to fix it but couldn't find anyway to do so.

1

There are 1 answers

1
user2970916 On BEST ANSWER

Your for loop is wrong. You are exiting when baslangic is less than baslangic + karSayisi. Then you incriment baslangic. This could be casuing that issu.

void cagir(char cDizi[], short int baslangic, short int karSayisi, char cDondur[])
{
    //Local Variable
    short int index = 0;

    /*Get all characters of the array till it reacher the NULL character and while it is between
      values baslangic and baslangic + karSayisi*/
    int exit = baslangic + karSayisi;
    for (; cDizi[baslangic] != '\0' && baslangic < exit; baslangic++)
    {
    //place the characters to cDondur array and returnn cDöndür
    cDondur[index] = cDizi[baslangic];
    index += 1;  //increment index of cDondur by 1
    }
}