C Wrapper Function, crashing Python?
Java and Swing
codecraig at gmail.com
Wed Oct 12 12:55:11 EDT 2005
Bernhard Herzog wrote:
> "Java and Swing" <codecraig at gmail.com> writes:
>
> > char *foo(const char *in) {
> > char *tmp;
> > tmp = (char *) malloc((strlen(in) * sizeof(char)) + 1);
> > strcpy(tmp, in);
> > ...
> > ...
> > free(tmp);
> > return someValue;
> > }
> >
> > Is that appropriate? I was under the impression that when you malloc
> > memory, you free it when done.
>
> Looks fine. I hope someValue does not point somewhere into the tmp
> buffer, though.
someValue doesn't.
>
> > I also have things like...
> >
> > char *bar(char *in) {
> > char *results, *finalResults;
> > results = (char *) malloc(...);
> > finalResults = results;
> >
> > while (...) { *results++ = some_val; }
> >
> > return finalResults;
> > }
>
> Seems OK, too, assuming the results buffer is big enough.
results = (char *) malloc((sizeof(char) * strlen(in) + 1);
..should be big enough. At any rate, it works four times in a row
before it fails.
I wonder what else or how else to solve this. Why is malloc'ing
the memory crashing it!?
More information about the Python-list
mailing list