I'm a Python veteran, but haven't dabbled much in C. After half a day of not finding anything on the internet that works for me, I thought I would ask here and get the help I need.
What I want to do is write a simple C function that accepts a string and returns a different string. I plan to bind this function in several languages (Java, Obj-C, Python, etc.) so I think it has to be pure C?
Here's what I have so far. Notice I get a segfault when trying to retrieve the value in Python.
hello.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
const char* hello(char* name) {
static char greeting[100] = "Hello, ";
strcat(greeting, name);
strcat(greeting, "!\n");
printf("%s\n", greeting);
return greeting;
}
main.py
import ctypes
hello = ctypes.cdll.LoadLibrary('./hello.so')
name = "Frank"
c_name = ctypes.c_char_p(name)
foo = hello.hello(c_name)
print c_name.value # this comes back fine
print ctypes.c_char_p(foo).value # segfault
I've read that the segfault is caused by C releasing the memory that was initially allocated for the returned string. Maybe I'm just barking up the wrong tree?
What's the proper way to accomplish what I want?
foo.restypeappropriately. Do you really want to usestatic? Not threadsafe. Wouldn't you be better allocating memory in Python and letting the C code populate it with content? Or allocate in the C code, and export a deallocator too. - David Heffernanstrdupormallocfor that. But really, if you want to do this kind of things in C, then invest in a C book. C is quite different from higher-level languages such as Python. - Fred Foostatic, so there's only one for all calls, so the next call would change what the first return value points at. Keeping it local and notstaticmeans its lifetime ends when the function returns, which makes it unsuitable. That's not even touching on the buffer overflow vulnerability! - user395760staticgcc gives me a warning. What's the proper way to allocate the memory for return? I'm just looking for something safe and straightforward. - Thane Brimhall