How delete int* dynamic array? - c++

I get a problem
"free(): invalid pointer
Process finished with exit code 6"
when I am trying to delete dr[1].
Please say why there is the error in current input (str1, str2)?
'''
#include <iostream>
using namespace std;
int64_t myMin(int64_t first, int64_t second) {
return first > second ? first : second;
}
int main() {
string str1, str2;
str1 = "ABRA", str2 = "CADABRA";
int64_t n1 = static_cast<int64_t>(str1.length()), n2 = static_cast<int64_t>(str2.length());
int64_t **dp = new int64_t *[n2 + 1];
for (int i = 0; i <= n2; ++i) {
dp[i] = new int64_t[n1 + 1];
dp[i][0] = 0;
dp[0][i] = 0;
}
for (int64_t i = 1; i <= n2; ++i) {
for (int j = 1; j <= n1; ++j) {
if (str2[i - 1] == str1[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = myMin(dp[i - 1][j], dp[i][j - 1]);
}
}
}
cout << dp[n2][n1];
cout << '\n' << n2 << "\t" << n1 << '\n';
for (int i = n2; i >= 0; --i) {
// There is no problem with uncommented below line. Why?
// if (i != 1)
delete[] dp[i];
}
delete[] dp;
return 0;
}
'''
I use CLion framework.

dp[0][i] = 0;
When n2 > n1, this will write over the array dp[0] as i grows.
Adding a bound-check will fix this problem:
if (i <= n1)
dp[0][i] = 0;
In addition, I'd strongly recommend to learn how to debug program.

Related

Running into unexpected error while running C++ code on CLion IDE

below is my c++ code
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int trials = 0;
cin >> trials;
int c = 1;
while (trials--) {
string grid[51];
int r = 0;
cin >> r;
int n = r;
int m = 0;
for (int i = 0; i < r; i++) {
string s;
cin >> s;
m = s.size();
grid[i] = s;
}
if (n < 3 || m < 3) {
cout << "Case #" << c++ << ": " << 0 << endl;
continue;
}
int rowCount[51][51] = {0};
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
rowCount[i + 1][j + 1] = rowCount[i + 1][j];
if (grid[i].at(j) == '0')
rowCount[i + 1][j + 1] += 1;
}
}
int columnCount[51][51] = {0};
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
columnCount[i + 1][j + 1] = columnCount[i][j + 1];
if (grid[i].at(j) == '0')
columnCount[i + 1][j + 1] += 1;
}
}
int table[51][51][51][51] = {0};
for (int h = 3; h <= n; h++) {
for (int w = 3; w <= m; w++) {
for (int i = 1; i + h - 1 <= n; i++) {
for (int j = 1; j + w - 1 <= m; j++) {
int i1 = i + h - 1;
int j1 = j + w - 1;
int vals[] = {table[i][j][i1][j1],
table[i + 1][j][i1][j1],
table[i][j + 1][i1][j1],
table[i][j][i1 - 1][j1],
table[i][j][i1][j1 - 1]};
table[i][j][i1][j1] = *max_element(vals, vals + 5);
// if (grid[i - 1].substr(j - 1, j1).find('.') == string::npos ||
// grid[i1 - 1].substr(j - 1, j1).find('.') == string::npos) {
// continue;
// }
if (rowCount[i][j1] - rowCount[i][j - 1] != j1 - j + 1 ||
rowCount[i1][j1] - rowCount[i1][j - 1] != j1 - j + 1) {
continue;
}
if (columnCount[i1][j] - columnCount[i - 1][j] != i1 - i + 1 ||
columnCount[i1][j1] - columnCount[i - 1][j1] != i1 - i + 1) {
continue;
}
if (table[i + 1][j + 1][i1 - 1][j1 - 1] + 1 > table[i][j][i1][j1]) {
table[i][j][i1][j1] = table[i + 1][j + 1][i1 - 1][j1 - 1] + 1;
}
}
}
}
}
cout << "Case #" << c++ << ": " << table[1][1][n][m] << endl;
}
return 0;
}
On running this code in Clion IDE (on my windows 11 system) I get the following error
Process finished with exit code -1073741571 (0xC00000FD)
This is basically a code for solving a competitive programming question. As soon as I execute the code I get the error. It doesn't even let me input trials.
I am not sure what the reason is for this. I believe it is to do with the memory available for execution. I have tried to change the memory settings for the CLion IDE but I still get the same error. The code above is long but you can just copy/paste it into your own environment and maybe have a look? It works alright on ideone (online compiler). Could anyone help with this?

C++ getting segfaults on an array I already assigned values to

I was trying to do a problem on HackerEarth, and I am getting Segmentation Faults for this for loop:
for (int index = 0; index < 18; index++){
cout << arr_list[arr_index][index];
}
Even though I assigned values to arr_list[arr_index][index] in the loop right before (so I'm guessing the values are somehow not being saved, but I don't know how the values aren't being saved).
When I remove this for loop, I don't get any segfaults, and the cout information prints what's expected (the numbers I've inputted, with each digit twice for each cout inside the loop).
#include <iostream>
#include <string>
using namespace std;
void step(int arr_list[1000000][18], int cs, int N){
/**
int freq[100000] = {0};
for (int i = 0; i < N; i++){
int cur_arr[18];
for (int index = 0; index < 18; index++){
cur_arr[index] = arr_list[i][index];
}
if (cs == 4){
freq[cur_arr[0]*100 + cur_arr[1] * 10 + cur_arr[2]] += 1;
} else{
freq[cur_arr[18 - cs*5] * 10000 + cur_arr[18 - cs*5 + 1] * 1000 + cur_arr[18 - cs*5 + 2]*100 + cur_arr[18 - cs*5 + 3] * 10 + cur_arr[18 - cs*5 + 4]] += 1;
}
}
for (int i = 1; i < 100000; i++){
freq[i] += freq[i-1];
}
int new_arr_list[1000000][18];
for (int i = N-1; i >= 0; i--){
int pos;
int cur_arr[18];
for (int index = 0; index < 18; index++){
cur_arr[index] = arr_list[i][index];
}
if (cs == 4){
pos = cur_arr[0]*100 + cur_arr[1] * 10 + cur_arr[2];
} else{
pos = cur_arr[18 - cs*5] * 10000 + cur_arr[18 - cs*5 + 1] * 1000 + cur_arr[18 - cs*5 + 2]*100 + cur_arr[18 - cs*5 + 3] * 10 + cur_arr[18 - cs*5 + 4];
}
for (int index = 0; index < 18; index++){
new_arr_list[freq[pos] - 1][index] = arr_list[i][index];
}
freq[pos] --;
}
for (int i = 0; i < N; i++){
for (int index = 0; index < 18; index++){
arr_list[i][index] = new_arr_list[i][index];
}
}
**/
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T;
int arr_index = 0;
cin >> T;
int arr_list[1000000][18];
int max_len = 0;
for (int testcase = 0; testcase < T; testcase ++){
string a;
cin >> a;
int a_len = a.length();
if (a_len > max_len){
max_len = a_len;
}
int arr_entry[18];
for (int i = 0; i < a_len ; i++){
arr_entry[18 - a_len + i] = a[i] - 48;
}
for (int i = 0; i < 18 - a_len; i++){
arr_entry[i] = 0;
}
for (int index = 0; index < 18; index++){
arr_list[arr_index][index] = arr_entry[index];
cout << arr_entry[index];
cout << arr_list[arr_index][index];
}
for (int index = 0; index < 18; index++){
cout << arr_list[arr_index][index];
}
arr_index ++;
}
/**
for (int c = 1; c < 5; c++){
if (max_len > (c-1)*5){
step(arr_list, c, T);
for (int i = 0; i < T; i++){
int is_leading_zero = 1;
for (int j = 0; j < 18; j++){
if (is_leading_zero == 0){
cout << arr_list[i][j];
}else{
if (arr_list[i][j] != 0){
is_leading_zero = 0;
cout << arr_list[i][j];
}
}
}
cout << " ";
}
cout << "\n";
}
}
**/
}
I'm assuming this is a common error, and that I'm missing something simple that gives me segfaults for values I already assigned data to.
Does anyone know why this is happening?
You are allocating 72 MB on the stack:
int main()
{
[...]
int arr_list[1000000][18];
[...]
}
This is probably causing a stack overflow.
On the Microsoft Windows platform, the maximum stack size is, by default, 1 MB. On Linux, it is typically 8 MB.
When allocating such large amounts of memory, I recommend that you instead either use
dynamic memory allocation, or
a global variable, or
a static local variable.
This ensures that the array is not stored on the stack.

C++ - Output always pushes to the most left?

The fiboEncoding() function below is to read an integer then return the Fibonacci encoding.
When I test it in the main function, it always pushes itself into the most left part of the output. How can I solve this problem? What did I do wrong to cause this problem?
#include <iostream>
#include <vector>
using namespace std;
string fiboEncoding(int n) {
string word;
int fib[1000];
fib[0] = 1;
fib[1] = 2;
int i = 0;
for(i = 2; fib[i-1] <= n; i++) {
fib[i] = fib[i-1] + fib[i-2];
}
int r = i - 2;
int index = r;
vector<char> v(r+3);
while(n > 0) {
v[index] = '1';
n = n - fib[index];
index = index - 1;
while (index >= 0 && fib[index] > n) {
v[index] = '0';
index = index - 1;
}
}
v[r + 1] = '1';
for (int j = 0; j < v.size() - 1; j++) {
cout << v[j];
}
return word;
}
int main() {
int n;
string fibo;
cin >> n;
fibo = fiboEncoding(n);
cout << "code: " << fibo << endl;
}
Your function returns an empty string word. You forgot to copy the result into word string.
What you see in the console is the result of executing the following part not cout.
for (int j = 0; j < v.size() - 1; j++) {
cout << v[j];
}
To fix replace the above for loop by
for (int j = 0; j < v.size() - 1; j++) {
//cout << v[j];
word += v[j];
}

How to resolve the segmentation fault in this code?

I am trying to make an infix calculator for which I am currently trying to convert numbers entered in a character array to double.
here's my code:
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
char exp[500];
const int SIZE = 100;
char temp[SIZE];
char op;
int strLen = 0, k, l, num = 0, fnum = 0;
double number = 0;
cin.getline(exp, 500,'\n');
int i = 0, j = 0, fpoint=0;
cout << exp;
for (i = 0, j = 0; exp[j] != 0; i++)
{
if (i % 2 == 0)
{
for (int m = 0; exp[m] != ','; m++) //stopped working
temp[m] = exp[m];
cout << temp;
for (k = 0; k < SIZE && temp[k] != 0; k++)
{
strLen = k;
if (temp[k] == '.')
fpoint = k + 1;
}
cout << fpoint<<endl;
cout << "strLen" << strLen;
for (k = 0; k <= fpoint; k++)
{
num = num + ((temp[fpoint - k] - '0') * pow(10, k));
}
for (k = fpoint + 1, l = 0; k <= strLen; k++, l++)
{
fnum = fnum + ((temp[strLen - l] - '0') * pow(10, l));
}
number = num + (fnum / pow(10, strLen - fpoint + 1));
cout << number;
j = j + strLen + 1;
}
else
{
char op = temp[j];
cout << op;
}
}
system("pause");
return 0;
}
sample input
2.5*3
It stops working and gives segmentation fault as an error on the marked position.
This line for (int m = 0; exp[m] != ','; m++) //stopped working will always fail if there are no , characters since exp[m] != ',' will always be equal to true and so will reach beyond the end of the array of exp which triggers the "segmentation fault".

why is the zero changing in c++

so i'm new to c++. i have to make a program where you enter a number and it displays the largest possible number and the smallest one, with the same digits as the entered number. But when the entered number has 0 it changes to 4310160
sorry for my english;{
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int m[10], i, l, x, c, ok, r;
cout << "x= ";
cin >> x;
l = 0;
while (x > 0) {
c = x % 10;
m[l] = c;
l++;
x = x / 10;
}
do {
ok = 1;
for (i = 0; i < l; i++)
if (m[i] < m[i + 1]) {
r = m[i];
m[i] = m[i + 1];
m[i + 1] = r;
ok = 0;
}
for (i = 0; i < l; i++) {
cout << m[i] << ", ";
}
cout << endl;
} while (ok != 1);
cout << "largest= ";
for (i = 0; i < l; i++) {
cout << m[i];
}
cout << endl;
do {
ok = 1;
for (i = 0; i < l; i++)
if (m[i] > m[i + 1]) {
r = m[i];
m[i] = m[i + 1];
m[i + 1] = r;
ok = 0;
}
} while (ok != 1);
if (m[0] == 0) {
r = m[0];
m[0] = m[1];
m[1] = r;
}
cout << "smallest= ";
for (i = 0; i < l; i++) {
cout << m[i];
}
return 0;
}
if x == 0, you do
if (m[0] == 0) {
r = m[0];
m[0] = m[1];
m[1] = r;
}
With uninitialized m, leading to undefined behavior.
The int m[10] array in main is stored in the stack and its elements initialized to indeterminate values as explained in array_initialization and initialization.
So if x == 0 , the program should print nothing as l will be 0 and it won't enter in any loop.
If you see 4310160 because you are debugging, it is because m[10] elements are initialized to indeterminate values (normally, the value at the stack in that moment.)
You may see here that local, non static data is stored in the stack.