Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove calls to free() #123

Closed
elliotchance opened this issue May 22, 2017 · 5 comments
Closed

Remove calls to free() #123

elliotchance opened this issue May 22, 2017 · 5 comments

Comments

@elliotchance
Copy link
Owner

free() is not required in Go because it has automatic memory management. However, this relates to a larger issue of having code removed, rather than just transpiled directly.

@Konstantin8105
Copy link
Contributor

Now, after transpile next C code:

#include <stdlib.h>

int main() {
        int * buf1, *buf2, *buf3;
        buf1 = (int*) malloc(100*sizeof(int));
        buf2 = (int*) calloc(100,sizeof(int));
        buf3 = (int*) realloc(buf2, 500*sizeof(int));
        free(buf1);
        free(buf3);
        return 0;
}

we have errors:

go run free.go
# command-line-arguments
./free.go:199:14: undefined: noarch.IntSliceToByteSlice
./free.go:200:14: undefined: noarch.IntSliceToByteSlice
./free.go:204:2: undefined: os
./free.go:206:14: undefined: os

Go code:

func main() {
        __init()
        var buf1 []int
        var buf2 []int
        var buf3 []int
        buf1 = make([]int, 100*4/4)
        buf2 = make([]int, 100*4/4)
        buf3 = make([]int, 500*4/4)
        noarch.Free(noarch.IntSliceToByteSlice(buf1))
        noarch.Free(noarch.IntSliceToByteSlice(buf3))
        return
}

@Konstantin8105
Copy link
Contributor

In according to http://www.cplusplus.com/reference/cstdlib/free/
we can change the design call function free.
C code:

free(buf1);

Go code:

_ = buf1

@elliotchance
Copy link
Owner Author

That's interesting. That would also work, but i'm thinking of completely removing the call to free(), like:

func main() {
        __init()
        var buf1 []int
        var buf2 []int
        var buf3 []int
        buf1 = make([]int, 100*4/4)
        buf2 = make([]int, 100*4/4)
        buf3 = make([]int, 500*4/4)
        return
}

@Konstantin8105
Copy link
Contributor

Konstantin8105 commented Oct 20, 2017

In your case, I see the problem - if you try for use go run on that code , then you will see 3 errors like that:

./free.go:: buf1 declared and not used
./free.go:: buf2 declared and not used
./free.go:: buf3 declared and not used

But if use _, then all is will be Ok.

I prepare the injection code for function free(), so, I can change in according to your point of view. Please clarify.

@elliotchance
Copy link
Owner Author

@Konstantin8105 I left some comments on the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants