B
brncrs
Hi list!
I'm writing out a program where string's are handled by opaque
structure and each string variable is allocated/deallocated
dynamically
using functions like `string_new' and `string_delete'.
In some part of this program I need variables of this type
only to let an argument to a function, and alloc/dealloc this variable
manually don't make me very happy.
Example:
typedef struct string* _Opaque_string_type;
typedef struct device* _Opaque_device_type;
...
extern _Opaque_string_type* string_new(const char *, ...);
extern void string_delete(_Opaque_string_type *);
extern _Device_type* device_open(_Opaque_string_type* p_path);
...
void foo(void)
{
_Opaque_string_type str;
_Opaque_device_type dev;
str = string_new("/dev/ttyACM0");
dev = device_open( str );
string_delete(str);
}
The best solution I got is to let to the `string_new' function the
address of a buffer where to store the variable:
Example:
typedef struct string* _Opaque_string_type;
typedef struct device* _Opaque_device_type;
...
extern _Opaque_string_type* string_new(void *, size_t, const
char *, ...);
extern _Device_type* device_open(_Opaque_string_type* p_path);
...
extern _Device_type* device_open(_Opaque_string_type* p_path);
void foo(void)
{
char buf[64];
_DeviceType *dev = device_open( string_new(buf, sizeof
(buf), "/dev/ttyACM0") );
}
So the string is stored in `buf' and I don't need to free them with
`string_delete'. Internally the string_new function organize memory in
this way:
+--------+----------------------
+-------------------------------------+
| size | data_ptr | "/dev/
ttyACM0" |
+--------+-----------+----------
+-------------------------------------+
|____________^
And cast initial address to _Opaque_string_type*.
Unfortunately, with this approach the string length is limited by the
size of the buffer, and I must declare buffer explicit.
I read about function `alloca', but I can't use it in a function
parameter list.
I will improve this method, someone can suggest a trick to me?
Excuse me for horrible English language!
Thanks in advance!
Christian.
I'm writing out a program where string's are handled by opaque
structure and each string variable is allocated/deallocated
dynamically
using functions like `string_new' and `string_delete'.
In some part of this program I need variables of this type
only to let an argument to a function, and alloc/dealloc this variable
manually don't make me very happy.
Example:
typedef struct string* _Opaque_string_type;
typedef struct device* _Opaque_device_type;
...
extern _Opaque_string_type* string_new(const char *, ...);
extern void string_delete(_Opaque_string_type *);
extern _Device_type* device_open(_Opaque_string_type* p_path);
...
void foo(void)
{
_Opaque_string_type str;
_Opaque_device_type dev;
str = string_new("/dev/ttyACM0");
dev = device_open( str );
string_delete(str);
}
The best solution I got is to let to the `string_new' function the
address of a buffer where to store the variable:
Example:
typedef struct string* _Opaque_string_type;
typedef struct device* _Opaque_device_type;
...
extern _Opaque_string_type* string_new(void *, size_t, const
char *, ...);
extern _Device_type* device_open(_Opaque_string_type* p_path);
...
extern _Device_type* device_open(_Opaque_string_type* p_path);
void foo(void)
{
char buf[64];
_DeviceType *dev = device_open( string_new(buf, sizeof
(buf), "/dev/ttyACM0") );
}
So the string is stored in `buf' and I don't need to free them with
`string_delete'. Internally the string_new function organize memory in
this way:
+--------+----------------------
+-------------------------------------+
| size | data_ptr | "/dev/
ttyACM0" |
+--------+-----------+----------
+-------------------------------------+
|____________^
And cast initial address to _Opaque_string_type*.
Unfortunately, with this approach the string length is limited by the
size of the buffer, and I must declare buffer explicit.
I read about function `alloca', but I can't use it in a function
parameter list.
I will improve this method, someone can suggest a trick to me?
Excuse me for horrible English language!
Thanks in advance!
Christian.