-7

Code:

#include<cstdio>
#include<cstdlib>
#include<string>
#include<iostream>
using namespace std;

class KMP { 
    public:     
        int * prefix_array(string);
        bool kmp_search(string,string,int *);       
};

int * KMP::prefix_array(string pattern) {
    int m,q,k;
    int *a;
    string p;
    p=pattern;
    m = p.length();
    a = (int*)malloc(sizeof(int)*m);    
    a[0] = -1;
    k=-1;
    for(q=1;q<m;q++) {
        while(k>-1 && p[k+1] != p[q]) {
            k=a[k];
        }
        if(p[k+1]==p[q]) {
            k=k+1;
        }
        a[q]=k;
    }
    /*
    for(int i=0;i<m;i++) {
        printf("%d ",a[i]);
    }   
    printf("\n");
    */
    return a;
}

bool KMP::kmp_search(string str, string pattern,int *a) {
    int n,m,q,i;
    string S,p;
    S = str;
    p = pattern;

    n = str.length();
    m = pattern.length();
    q = -1;

    for(i=0;i<n;i++) {
        while(q>-1 && p[q+1] != S[i]) {
            q=a[q];
        }
        if(p[q+1] == S[i]) {
            q=q+1;
        }
        if(q==m-1) {
            q=a[q];         
            return true;
        }
    }
    return false;
}

int main() {
    //freopen("in.txt","r",stdin);

    int testCase,num;
    char ch;
    string str,pattern;
    char str2[250];
    bool check;
    int *arr;

    KMP *obj = new KMP();
    scanf("%d\n",&testCase);    

    while(testCase--) {
        getline(cin,str);
        //cout<<str<<endl;
        scanf("%d\n",&num);
        //printf("%d\n",num);
        while(num--) {
            getline(cin,pattern);                       
            arr=obj->prefix_array(pattern);             
            check = obj->kmp_search(str,pattern,arr);
            if(check) {
                printf("y\n");
            } else {
                printf("n\n");
            }           
        }
    }
    delete obj;
    return 0;
}

in.txt (input):

2
abcdefghABCDEFGH
3
ababaca
abc
abAB
xyz
1
xyz

I'm trying to free memory in int * KMP::prefix_array(string pattern) { . It's allocating memory with this: a = (int*)malloc(sizeof(int)*m); , this function returns the pointer. So how can i free that memory?

Any answer will be highly appreciated. Thanks in advance.

Fredrik Savje
  • 565
  • 2
  • 14
cola
  • 12,198
  • 36
  • 105
  • 165
  • Possible duplicate of [How do I free memory in C?](http://stackoverflow.com/questions/9069205/how-do-i-free-memory-in-c) – Morrison Chang Mar 06 '16 at 06:04
  • 1
    Call `free` on the pointer when you are done with the memory. Even better use `vector`. – jxh Mar 06 '16 at 06:04
  • 2
    This is **not** C. Don't tag indiscriminately, and don't mention irrelevancies in your title. As it is C++, you should use `new` and `delete`, not `malloc()` and `free()`. – user207421 Mar 06 '16 at 06:06
  • 2
    2.5K rep and you don't know how to ask a question? How does that happen? – juanchopanza Mar 06 '16 at 06:08
  • @jxh, I know `free(a);` frees memory, but when would i write this? It's returning that pointer. – cola Mar 06 '16 at 06:12
  • @EJP, If i use new, then when would i write delete? It's returning that pointer. – cola Mar 06 '16 at 06:14
  • @EJP, malloc and free c are statements, that's why i added c tag. – cola Mar 06 '16 at 06:15
  • @MorrisonChang, the answer is not in that link. – cola Mar 06 '16 at 06:16
  • 1
    You would write `free(a)` when you are done with `a`. – jxh Mar 06 '16 at 06:28
  • Upon review I agree that the original dup suggest wasn't the right answer. However I agree with @jxh to just free the alloc when you are done with it. Your class as written exposes the int array directly and doesn't try to encapsulate it anyway or wrap in it some lifecycle. – Morrison Chang Mar 06 '16 at 06:36
  • @shibly They are *not* statements. They are functions in the standard C and C++ libraries. Your code is C++ and that is therefore what your question is about. – user207421 Mar 06 '16 at 11:26

1 Answers1

1

A function that returns an allocated pointer means that ownership of that memory is being transferred to the caller of that function. It is up to the code that receives the returned pointer to deallocate the memory.

While using malloc() and free() in C++ is not entirely without precedent, it should generally be avoided. You could avoid the problem entirely by changing your code to use std::vector<int> instead of an int *.

jxh
  • 69,070
  • 8
  • 110
  • 193
  • If i do `delete[] arr;` at the end in main function, will it deallocate array "`a`" created in `prefix_array` function (`a = (int*)malloc(sizeof(int)*m);`)? – cola Mar 07 '16 at 09:51
  • @What would happen if i use `free(arr);` at the end in main function ? – cola Mar 07 '16 at 12:14
  • What would happen if you call `arr = new int[5]; arr = new int[100]; delete[] arr;`? – jxh Mar 07 '16 at 12:26
  • It will deallocate arr. My question is if i use `free(arr);` will it deallocate array "a" in prefix_array function? – cola Mar 07 '16 at 12:31
  • Are you sure it will deallocate `arr`? How many allocations have been assigned to `arr`? How many deallocations should be done to release all the allocated memory? – jxh Mar 07 '16 at 12:39
  • I didn't do: `arr=new int[5];` i did: `arr=obj->prefix_array(pattern);` , then if i do `free(arr);` will it free "a" , `a = (int*)malloc(sizeof(int)*m);` is in prefix_array function? – cola Mar 07 '16 at 12:48
  • If `malloc()` returns the pointer value `0xabcdabcd`, then the code `free(0xabcdabcd)` will free the memory. – jxh Mar 07 '16 at 15:47