LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-04-2008, 07:43 AM   #1
ocularb0b
LQ Newbie
 
Registered: Nov 2007
Distribution: gentoo, deb
Posts: 16

Rep: Reputation: 0
c++ problem setting "const char *str" from function returning a string


i hope my post title is not totally stupid...

so my program needs to read a main config file that will specify the name of another config file.
i have done this:

Code:
const char *sysConfig = "main.conf";

int read_sys_config()
{
	cfg_opt_t sys_opts[] =
	{
		CFG_STR("mechConf", "defconfig.conf", CFGF_NONE),
		CFG_END()
	};
	cfg_t *sys_cfg;
	sys_cfg = cfg_init(sys_opts, CFGF_NONE);
	if(cfg_parse(sys_cfg, sysConfig) == CFG_PARSE_ERROR)
		return 1;
	const char *mechConfig = cfg_getstr(sys_cfg, "mechConf");
	cfg_free(sys_cfg);
	cfg_opt_t mech_opts[] =
	{
		CFG_INT("thing", 0, CFGF_NONE),       
		CFG_END()
	};
	cfg_t *mech_cfg;
	mech_cfg = cfg_init(mech_opts, CFGF_NONE);
	if(cfg_parse(mech_cfg, mechConfig) == CFG_PARSE_ERROR)
		return 1;
	thing = cfg_getint(mech_cfg, "thing");
	cfg_free(mech_cfg);
	return 0;
}
and the config file has:
Code:
mechConf = "other.conf"
i get no compile errors and other integer values from main.conf(not shown in posted code) get set correctly but it doesn't seem to put the string from main.conf into *mechConfig.
what am i missing?
thanks in advance
 
Old 03-04-2008, 01:10 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Does cfg_getstr allocate its own memory? I think the problem lies in that function.
 
Old 03-04-2008, 01:23 PM   #3
ocularb0b
LQ Newbie
 
Registered: Nov 2007
Distribution: gentoo, deb
Posts: 16

Original Poster
Rep: Reputation: 0
i have no idea or how to find out exactly. it is part of libconfuse which i have used for a while and seems reasonably solid.
for what its worth this works fine.

printf("mechConf = %s\n", cfg_getstr(sys_cfg, "mechConf"));
 
Old 03-04-2008, 09:36 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Well, I think I see the problem now (and it does have to do with memory management). All char arrays for the first config file are allocated in “sys_cfg = cfg_init(sys_opts, CFGF_NONE)” and “cfg_parse(sys_cfg, sysConfig)” (and the call to cfg_getstr(sys_cfg, "mechConf") gets you a pointer to the interally-allocated string). All this data will be deallocated by the call to cfg_free(sys_cfg). But you try to use mechConfig after this call, in which case you have a pointer to freed data.

The solution would be to move down the call to cfg_free(sys_cfg) to a line after you are done with using mechConfig.

E.g.,
Code:
const char *sysConfig = "main.conf";

int read_sys_config()
{
	cfg_opt_t sys_opts[] =
	{
		CFG_STR("mechConf", "defconfig.conf", CFGF_NONE),
		CFG_END()
	};
	cfg_t *sys_cfg;
	sys_cfg = cfg_init(sys_opts, CFGF_NONE);
	if(cfg_parse(sys_cfg, sysConfig) == CFG_PARSE_ERROR)
		return 1;
	const char *mechConfig = cfg_getstr(sys_cfg, "mechConf");
	//cfg_free(sys_cfg);
	cfg_opt_t mech_opts[] =
	{
		CFG_INT("thing", 0, CFGF_NONE),       
		CFG_END()
	};
	cfg_t *mech_cfg;
	mech_cfg = cfg_init(mech_opts, CFGF_NONE);
	if(cfg_parse(mech_cfg, mechConfig) == CFG_PARSE_ERROR)
		return 1;
	thing = cfg_getint(mech_cfg, "thing");
	cfg_free(mech_cfg);
	cfg_free(sys_cfg);
	return 0;
}
See if that works.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert "string" type to "char * " in c++ pranavojha Programming 18 06-05-2008 03:10 PM
Perl: Check if "$str" is of type [A-Z]<char string>[0-9][0-9] introuble Programming 2 06-02-2006 10:33 AM
bash cripting function to "identify" any char in a string? minike Programming 8 02-01-2006 09:45 AM
"int const" working problem. gpagedar Linux - General 0 10-08-2004 01:04 AM
using java to find the location of the "\" char in a string caged Programming 8 02-03-2004 10:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration