Why the bejesus are you using malloc when you're clearly quite happy using new?
You've got a memory leak in there; you set c to NULL without ever calling free on the memory that was malloced. c then falls off the stack at end of scope anyway.
You've got another memory leak in that ch simply falls off the end of scope, so you've got no way to ever delete that memory.
Stop using malloc and free. Just use new and delete.
Then, build your code with debug symbols (the -g option) and run it under valgrind, and then gdb. This will tell you the exact lines that are going wrong.
Actually I know this code doesn't make much sense, this is only an amount of tries to fix the real code.
I tried to run the program with this changes you mentioned, but also this doesn't work.
So I'm trying to understand what in fact is the problem...
Just to clarify: I'm using malloc to convert from string to char*. Then, I'm copying this char* to other char, but this was only for check. I removed all of ch, but I still got an error.
1 2 3 4 5 6 7 8 9
bool contain (constchar ** array, long size, string str) {
char* c = (char*)malloc( sizeof(char)*((str.length())+1) );
strcpy(c, str.c_str());
for (int i = 0; i < size; i++) if (!strcmp(array[i],c)) returntrue;
returnfalse;
}
Above is the new function. I'll try to run valgrind, anyway.
#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <cstdlib>
usingnamespace std;
bool contain (constchar ** array, long size, string str, int pos) {
char* c = (char*)malloc( sizeof(char)*((str.length())+1) );
strcpy(c, str.c_str());
for (int i = 0; i < size; i++) if (!strcmp(array[i],c)) returntrue;
array[pos] = c;
returnfalse;
}
int main() {
char * word = newchar[1];
cin >> word;
long size = 0;
for (int i = 0; word[i] != '\0'; i++) size+=1;
long nsize = size*(size+1)/2;
constchar ** array = newconstchar*[nsize];
for (int i = 0; i < nsize; i++) array[i] = "";
int pos = 0;
for (int i = 0; i < size; i++) {
for (int j = i; j < size; j++) {
string s = "";
for (int k = i; k < j+1; k++) s+=word[k];
if (!contain(array, nsize, s, pos)) pos+=1;
}
}
int result = 0;
for (int i = 0; i < nsize; i++) if (array[i] != "") result+=1;
for (int i = 0; i < nsize; i++) array[i] = NULL;
delete [] word;
delete [] array;
cout << result << endl;
return 0;
}
Just to explain: it's a program that count the amout of substrings in a string. For example, if word = abc, then result = 6. When I pass strings with size greater than 14, the program crash.
word is a pointer to an array of size one. The array can thus hold a single char (without even a terminating zero, so it's not a properly formed c-style string). Do you mean it to be a single char? I suspect not, as when you end up on this line of code:
for (int k = i; k < j+1; k++) s+=word[k];
which valgrind reveals to be the problem line, when k is two, you're trying to read word[2] which does not exist. Of course, by entering more than one letter in your input, you've already trashed a whole bunch of memory that isn't yours.
Yes, that is wrong. A char array of size one can hold a single char. If you change that to, for example,
char * word = newchar[100];
you'll find that your 14 character word no longer makes it break, but of course a very very long word will overflow the array again.
If you insist on using a char array, but you cannot know the size of the word until runtime, you can use a string to hold it while you organise a char array of the right size, much like you already did in the function above.