[Asterisk-Dev] newbie C programmer, problems with Malloc in Asterisk application

Moises Silva moises.silva at gmail.com
Tue Dec 13 09:50:19 MST 2005


well, I have 2 years in PHP scripting and im starting lo learn C. I have
started to write an asterisk application to try to apply what ive read from
C. But im getting stuck with a malloc call. Im not even sure if is the right
path, but since i Need a dynamic string, I guess malloc is the way to go.
The application is supposed to display channel debugging information, such
channel vars. Im having troubles creating the string that will display all
the channel vars and its values. I have this:

 static int dumpchan_exec(*struct* ast_channel *chan, void *data)
{
	int res=0;
	*struct* localuser *u;
	LOCAL_USER_ADD(u);

	*/* dumpchan exec stuff */*
	*struct* ast_var_t *current_var;
	char *vars_buffer = "Channel Variables: \n\t ";
	ast_verbose("starting bucle...\n");
	AST_LIST_TRAVERSE (&chan->varshead, current_var, entries) {
		char *variable_name    = ast_var_name(current_var);
		char *variable_value   = ast_var_value(current_var);
		size_t new_string_size = ( strlen(variable_name)  +
		                           strlen(variable_value) +
		                           strlen(vars_buffer) ) * *sizeof*(char);
		new_string_size       += 5; */* 5 chars, the ending null char, a
tab, 2 spaces and : */*
		ast_verbose("Allocating %d bytes of  memory...\n", (int)new_string_size);
		char *temp_vars        = (char *)malloc(new_string_size);
		ast_verbose("Allocated!");
		*if* ( !temp_vars ) {
			ast_log(LOG_ERROR, "Out of memory.\n");
			*return* -1;
		}

............ more code

--
the application just dies when the malloc() function is called. Any help and
suggestions will be appreciated.

Best Regards.

"Su nombre es GNU/Linux, no solamente Linux, mas info en http://www.gnu.org"
the
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.digium.com/pipermail/asterisk-dev/attachments/20051213/21bccb2e/attachment.htm


More information about the asterisk-dev mailing list