Heap Corruption Detected (Simulating Dynamic Array with Static Array) - c++

I wanna simulate dynamic array by using static array. However, when I delete some elements from the array and then try to add the array, I got an error saying 'Heap Corruption Detected'.
Here's my code:
#include <iostream>
#include <algorithm>
using namespace std;
int INIT_SIZE = 5;
int MAX_SIZE = INIT_SIZE;
void printArray(int *arr, int n){
cout << "n= " << n << " >> [";
for(int i = 0; i < n ;i++){
cout << arr[i];
if ( i < n-1)
cout << ",";
}
cout << "]\b\n";
}
bool deleteEl(int *&arr, int &n, int el){
int *newArr;
bool found = false;
int cnt = 0;
if( n == 0){
return false;
}
else{
for(int i = 0; i < n;i++){
if( arr[i] == el){
found = true;
cnt++;
}
}
if( !found){
return false;
}
newArr = new int[n-cnt];
int k = 0;
for(int i = 0; i < n ; i++){
if( arr[i] != el){
newArr[k++] = arr[i];
}
}
n -= cnt;
if (n <= (MAX_SIZE / 2) && (MAX_SIZE / 2) >= INIT_SIZE){
MAX_SIZE /= 2;
}
delete[] arr;
arr = newArr;
}
return true;
}
bool addEl(int *&arr, int &n, int el){
int *newArr;
if( n >= MAX_SIZE){
newArr = new int[2 * MAX_SIZE];
//std::copy(arr,arr+MAX_SIZE, newArr);
for(int i = 0 ; i < n;i++){
newArr[i] = arr[i];
}
n++;
newArr[n-1] = el;
MAX_SIZE *= 2;
delete[] arr;
arr = newArr;
}else{
arr[n++] = el;
}
return true;
}
int main(int argc, char *argv[]){
int *arr = new int[MAX_SIZE];
int n = 0;
int input = 0;
do{
cout << "1. Add Element : " << endl;
cout << "2. Del Element : " << endl;
cout << "3. Exit : " << endl;
cin >> input;
if( input == 1){
cout << "Enter new element : " << endl;
int newEl;
cin >> newEl;
if( addEl(arr, n, newEl)){
cout << "New Element Added. " << endl;
}else{
cout << "Add new element failed. " << endl;
}
}else if (input == 2){
cout << "Enter deleted element : " << endl;
int del;
cin >> del;
if( deleteEl(arr, n, del)){
cout << "Deletion OK" << endl;
}else{
cout << "Deletion Fail. Data Not found." << endl;
}
}else if(input == 3){
cout << "Program exiting ..." << endl;
}else{
cout << "Input is wrong. Enter correct input (1,2, or 3)." << endl;
}
printArray(arr,n);
}while(input != 3);
delete [] arr;
return 0;
}

Have a look at the Hinnant's stack allocator and consider using STL instead of naked arrays.

Related

I'm trying to check my array list input if its an int or string but this seems to crash and just go into infinite loop

#include <iostream>
#include <stdio.h>
#include <ctype.h>
using namespace std;
class List{
private:
int A[10];
int i;
public:
List(){
i = 0;
}
void insert(){
int v;
for(int j = 0 ; j < 10 ; j++){
cout << "\nElement you want to insert: (" << i+1 << "). ";
cin >> v;
if(i <= 9){
A[i] = v;
i++;
}
else{
cout << "\nWrong Input" << endl;
break;
}
}
if(i > 9){
cout << "\nYour List Capacity is Full.\n" << endl;
}
}
void display(){
cout << "\n{ ";
for(int j = 0 ; j < i ; j++)
cout << A[j] << " ";
cout << "}\n" << endl;
}
void remove(){
int p;
cout << "\nElement you want to remove (0 - 9): ";
cin >> p;
if (i == 0){
cout << "\nList is empty!\n" << endl;
return;
}
if (p >= 0 && p < i){
for(int j = p ; j < i-1 ; j++)
A[j] = A[j + 1];
i--;
}
}
void size(){
cout << "\nYour list size is: " << i << endl;
}
void checkcapacity(){
int arrSize = sizeof(A)/sizeof(A[0]);
cout << "\nThe Capacity of the array is: " << arrSize << endl;
}
};
int main(){
List l;
int a;
cout << "\t\t\t\t\t\tWelcome to List Program!";
while(a != 4){
int choose;
cout << "\nThe Program have following options:\n1. Insert\n2. Display\n3. Remove\n4. Check Size\n5. Check Capacity\n6. Exit\n\nNote: Your list capacity is 10!";
cout << "\n\nChoose (1 - 5): ";
cin >> choose;
if (choose == 1){
l.insert();
}
else if (choose == 2){
l.display();
}
else if (choose == 3){
l.remove();
}
else if (choose == 4){
l.size();
}
else if (choose == 5){
l.checkcapacity();
}
else if (choose == 6){
a = 4;
}
}
cout << "Thank you for using this program!";
}
I'm using this class in my main function in which I call them but when the user inputs a char or string in the insert function it goes into infinte loop. Int i is my counter and it just contains the size of my array list im just trying to put a check of character that if user input a character is should show an error.
Here's one way to write your insert function
void insert()
{
int v;
for(int j = 0 ; j < 10 ; j++)
{
cout << "\nElement you want to insert: (" << i+1 << "). ";
if (cin >> v)
{
if (i < 10)
{
A[i] = v;
i++;
}
}
else
{
cin.clear(); // clear error
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // discard any pending input
}
}
if (i >= 10)
cout << "\nYour List Capacity is Full.\n" << endl;
}
The important part is the recovery from bad input. First cin.clear() is called to clear the stream error state, secondly cin.ignore(...) is called to discard any pending input.
More details here

Dictionary with pointers

I want to write a program that adds words to a lexicon with pointers. However, I must not use strdup(), how can I do that? Below is what I tried but it gives me this error:
Exception thrown at 0x7C87EE72 (ucrtbased.dll) in (name of the file.exe): 0xC0000005: Access violation writing location 0xCDCDCDCD.
The problem is in the newStr() function.
#include <cstring>
#include <string>
#pragma warning (disable:4996)
#include <iostream>
using namespace std;
void delStr(char**&, int&, char*);
void newStr(char**&, int&, char*);
void printchar(char**, int, char);
char* searchStr(char**&, int&, char*);
void printAll(char**&, int);
enum ACTIONS { NEW, DELETE, SEARCH, PRINTLETTER, PRINTALL, EXIT };
int main() {
char** lexicon = NULL;//pointer to pointer fo the dictionary
int x = 0;
int size = 0;
char word[81];
char ch;
cout << "Enter 0-5:" << endl;
cin >> x;
while (x < 0 || x > 5) {//while loop for correct input
cout << "ERROR" << endl;
cin >> x;
}
while (x >= 0 && x <= 5) {
switch (x) {
case NEW:
cout << "Enter the word:" << endl;
ch = cin.get();
cin.getline(word, 80);
newStr(lexicon, size, word);
printAll(lexicon, size);
break;
case DELETE:
cout << "Enter the word to delete:" << endl;
ch = cin.get();
cin.getline(word, 80);
delStr(lexicon, size, word);
printAll(lexicon, size);
cout << endl;
break;
case SEARCH:
cout << "Enter the word to search for:" << endl;
ch = cin.get();
cin.getline(word, 80);
searchStr(lexicon, size, word);
break;
case PRINTLETTER:
cout << "Enter the char:" << endl;
ch = cin.get();
ch = cin.get();
printchar(lexicon, size, ch);
cout << endl;
break;
case PRINTALL:
if (size > 0 && lexicon != NULL) {
printAll(lexicon, size);
}
break;
case EXIT:
return 0;
break;
}
cout << "Enter your choice:" << endl;
cin >> x;
while (x < 0 || x > 5) {//while loop for correct input
cout << "ERROR" << endl;
cin >> x;
}
}
return 0;
}
void printAll(char**& lex, int size) {
for (int i = 0; i < size; i++) {
cout << lex[i] << " ";
}
cout << endl;
}
void newStr(char**& lex, int& size, char* word) {
int i = 0;
for (i = 0; i < size; i++) {
if (strcmp(lex[i], word) == 0) {
//cout << "Word " << word << " already exists\n";
return;
}
}
if (size == 0) {
lex = new char* [1];
for (int i = 0; i < strlen(word); i++) {
strcpy(*lex, word);
}
size++;
}
else {
char** temp = new char* [size + 1];
for (i = 0; i < size; i++) {
temp[i] = lex[i];
}
//strcpy(temp[size],word);
strcpy(temp[size], word);
delete[] lex;
lex = temp;
size++;
}
}
void delStr(char**& lex, int& size, char* word) {
int j = 0;
for (int i = 0; i < size; i++) {
if (strcmp(lex[i], word) == 0) {
for (j = i; j < size - 1; j++) {
lex[j] = lex[j + 1];
}
size--;
char** temp = new char* [size];
for (j = 0; j < size; j++) {
temp[j] = lex[j];
}
delete[] lex;
lex = temp;
}
}
}
void printchar(char** lex, int size, char ch) {
for (int i = 0; i < size; i++) {
if (lex[i][0] == ch) {
cout << lex[i] << " ";
}
}
}
char* searchStr(char**& lex, int& size, char* word) {
for (int i = 0; i < size; i++) {
if (strcmp(lex[i], word) == 0) {
cout << "Found" << endl;
//cout << "Word " << word << " already exists\n";
return lex[i];
}
}
cout << "Not found";
return NULL;
}

How to print elements from an array created with new?

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main() {
srand(time(NULL));
while (1) {
int n;
bool check_dup = false;
cout << "Please enter a number: ";
cin >> n;
if (n < 2) {
cout << "Wrong number!!!" << endl;
break;
}
cout << "Size of random array: " << n / 2 << endl;
int* arr = new int[n / 2];
cout << "[ Array ]" << endl;
for (int i = 0; i < n/2 / n; i++) {
arr[i] = rand() % n + 1;
cout << arr[i];
}
for (int i = 0; i < n/2; i++) {
for (int j = i+1; j < n/2; j++) {
if (arr[i] == arr[j])
check_dup = true;
}
}
if (check_dup) cout << "Duplicates found" << endl;
else cout << "Duplicates not found" << endl;
delete[] arr;
}
return 0;
}
After inputting n, we dynamically allocated an n / 2 size array and then stored a random number. And when I run it, the rest works fine, but it only outputs the arr value. Where is wrong?

How to fix 'struct does not refer to a value'

in my project there are 2 structures, one of which relates to a binary tree, the second to students data
in ' int main ' I can’t access the add function GETDATA
' 'ZKR' does not refer to a value . ' (xcode)
I also can’t create a counter of the number of vertices of my tree
#include <iostream>
#include <ctime>
#include <cstring>
using namespace std;
struct ZKR {
char FACULTY[30];
int ACADEMIC_DEGREE;
char FIO[30];
};
struct point
{
char *data;
point *left;
point *right;
};
point* tree(int n, point* p)
{
point *r;
int nl, nr;
if (n == 0) { p = NULL; return p; }
nl = n / 2;
nr = n - nl - 1;
r = new point;
char s[50];
cout << "Значение: ";
cin >> s;
r->data = new char[strlen(s) + 1];
strcpy(r->data,s);
r->left = tree(nl, r->left);
r->right = tree(nr, r->right);
p = r;
return p;
}
void GETDATA(ZKR*M, int N)
{
cin.ignore();
for (int i = 0; i < N; i++)
{
cout << "\n";
cout << "FACULTY: ";
cin.getline(M[i].FACULTY, 30);
cout << "\n";
cout << "FIO: ";
cin.getline(M[i].FIO, 30);
cout << "\n";
cout << "ACADEMIC DEGREE: ";
cin >> M[i].ACADEMIC_DEGREE;
cin.ignore();
}
}
void treeprint(point *p, int &count) {
if (p != NULL) {
treeprint(p->left, count);
cout << p->data << " ";
treeprint(p->right, count);
if ((p->left == NULL) && (p->right == NULL))
count = count + 1;
}
}
int main()
{
setlocale(LC_ALL, "russian");
srand(time(NULL));
int n = 0, k = 0, count = 0;
point *beg = nullptr;
cout << "Enter the number of students" << endl;
int N;
cin >> N;
ZKR*M = new ZKR[N];
do
{
cout << "1. BUILD a binary tree\n";
cout << "2. SHOW a binary tree\n";
cout << "3. GETDATA\n";
cin >> k;
switch (k)
{
case 1:
cout << "Введите количество элементов" << endl;
cin >> n;
beg = tree(n, beg);
cout << endl;
break;
case 2:
treeprint(beg, count);
cout << endl;
cout << "Листьев в дереве: " << count << endl;
break;
case 3:
GETDATA(ZKR*M, N);
break;
}
} while (k != 4);
system("pause");
return 0;
}
I will be grateful for any help
This line is not syntactically correct:
GETDATA(ZKR*M, N);
Replace it with:
GETDATA(M, N);
M is a pointer to an object of type ZKR.

How to do selection sort?

Count all the records from the file "8.dat". To read each individual recording perform dynamic memory capture.
Sort the records to different keys:
Item number (ascending);
The cost (descending);
Number of stock (descending).
Use selection sort
Total sorting will be done 12 times, each time the array is sorted in its original condition.
For each case count of comparisons to and permutations.
Below code implements insertion sort. Twice, without saying so much.
I need to use selection sort. How to do selection sort?
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
struct PRICE
{
int number;
char name[20];
int cost;
int quantity;
} *pm;
int Menu();
void PrintPRICE(PRICE);
void sort_cost(PRICE*, int);
void sort_quantity(PRICE*, int);
long file_size(const char*);
int main()
{
int count = 0;
const char *fname = "D:\8.dat";
FILE* file = fopen(fname, "r");
if (file != NULL)
{
long size = file_size(fname);
count = size / sizeof PRICE;
pm = new PRICE[count];
fread(pm, sizeof PRICE, count, file);
fclose(file);
}
for (int i=0; i<count; i++)
{
PrintPRICE(pm[i]);
cout << endl;
}
int ch = Menu();
switch (ch)
{
case 1:
{
sort_cost(pm, count);
cout << endl;
cout << " Result: " << endl;
cout << "-----------------------" << endl;
for (int i=0; i<count; i++)
{
PrintPRICE(pm[i]);
cout << endl;
}
break;
}
case 2:
{
sort_quantity(pm, count);
cout << " Result: " << endl;
cout << "-----------------------" << endl;
for (int i=0; i<count; i++)
{
PrintPRICE(pm[i]);
cout << endl;
}
break;
}
default: break;
}
delete [] pm;
_getch();
}
void PrintPRICE(PRICE price)
{
cout << " Product: " << price.name << endl;
cout << " Number of orden: " << price.number << endl;
cout << " Product cost: " << price.cost << endl;
cout << " Quantity in stock: " << price.quantity << endl;
cout << "-----------------------------------n" << endl;
}
long file_size(const char* filename)
{
FILE *Pfile = NULL;
Pfile = fopen(filename, "rb");
fseek(Pfile, 0, SEEK_END);
long size = ftell(Pfile);
fclose(Pfile);
return size;
}
void sort_cost(PRICE* array, int count)
{
int change = 0;
int comparesion = 0;
for (int i=1; i<count; i++)
{
PRICE key = array[i];
int j = i - 1;
comparesion++;
while (i>=0 && array[i].cost>key.cost)
{
array[j + 1] = array[j];
j = j - 1;
change++;
}
array[j + 1] = key;
}
cout << "n Quantity change: " << change << endl;
cout << " Quantity comparesion: " << comparesion << endl;
}
void sort_quantity(PRICE* array, int count)
{
int change = 0;
int comparesion = 0;
for (int i=1; i<count; i++)
{
PRICE key = array[i];
int j = i - 1;
comparesion++;
while (j>=0 && array[i].quantity>key.quantity)
{
array[j + 1] = array[j];
j = j - 1;
change++;
}
array[j + 1] = array[j];
}
cout << "n Quantity change: " << change << endl;
cout << " Quantity comparesion: " << comparesion << endl;
}
int Menu()
{
int n;
cout << " 1 - Sort by cost" << endl;
cout << " 2 - Sort by quantity" << endl;
cout << "n Your choice: "; cin >> n;
return n;
}
source code for the selection sort
void selectSort(int arr[], int n)
{
int pos_min,temp;
for (int i=0; i < n-1; i++)
{
pos_min = i;
for (int j=i+1; j < n; j++)
{
if (arr[j] < arr[pos_min])
pos_min=j;
}
if (pos_min != i)
{
temp = arr[i];
arr[i] = arr[pos_min];
arr[pos_min] = temp;
}
}
}