Re: PString question....

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, 24 Jan 2005 00:34:53 -0200
Sergio Lenzi <enigma@xxxxxxxxxxxxxxxxx> wrote:

> Hello...
> May seems a silly question but...
> 
> after reading the PWLIB doc and PString....
> 
> 
> Is there a way (must be to get a PString s)
> and return a char *  (C char *)?
> 
> seems that you can convert char * to PString....
> 
> why???
> 
> I must make a call to an external function that
> accepts char* and returns char *
> 
> if s is PString....
> and  f1 is defined as:   char *f1(char *);
> 
> than....
> 
> p=f1(s) does not work....
>
> p=f1(s.ToLiteral());    -> results error....
> 
> Any help????

There is a const operator on PString that returns a "const char *", so
you can do the folloiwng

	PString str("hello, world")
	const char * ptr = str;

If you want a "char *", then use the non-const GetPointer function, as
follows:

	PString str("hello, world")
	char * ptr = str.GetPointer();

In your code, you should either change your function to accept a "const
char *", or use

	p = f1(s.GetPointer())

Be careful when using these operators/functions, as you may end up
taking with a pointer to a temporary or automatic variable that becomes
invalid. Consider the following:

	const char * global_ptr = NULL;

	void set_global(const char * v)
	{
	  global_ptr  = v;
	}

	void func1()
	{
	  PString str("hello, world);
	  set_global(str);
	}

	int main(int, char **)
	{
	  func1();
	  cout << "The global is " << global_ptr << endl;
	}

This code assigns the global variables from a PString, but then the
pointer becomes invalid when the function "func1" exits and destroys the
PString (and the data it contains). So, the cout is actually using an
invalid ptr that may be hard to find as it could work on a system that
is not very busy.

This function can be made safe by changing set_global to be:

	void set_global(const char * v)
	{
	  global_ptr = strdup(v);
	}

and then freeind the pointer after the cout with:

	free(global_ptr)

Of course, if the PString remains valid for the lifetime of the const char
* or char* pointer, then there is no problem. For example, the following
code is OK:

	void func()
	{
	  PString str("hello world");
	  printf("This is a string -> %s\n", (const char *)str);
	}


  Craig

-----------------------------------------------------------------------
 Craig Southeren      craigs@xxxxxxxxxxxxxxxxx / craigs@xxxxxxxxxxxxx

 Phone:  +61 243654666      ICQ: #86852844
 Fax:    +61 243673140      MSN: craig_southeren@xxxxxxxxxxx
 Mobile: +61 417231046   Jabber: craigs@xxxxxxxxxxxxxxxxxxxx

 Post Increment - Consulting & Services    http://www.postincrement.com
 Vox Gratia - The Open Source VoIP portal  http://www.voxgratia.org
 Raving Of A Strange Mind - the VoIP blog  http://www.southeren.com/blog



-------------------------------------------------------
This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting
Tool for open source databases. Create drag-&-drop reports. Save time
by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc.
Download a FREE copy at http://www.intelliview.com/go/osdn_nl

_______________________________________________________

List: Openh323gk-users@xxxxxxxxxxxxxxxxxxxxx
Archive: http://sourceforge.net/mailarchive/forum.php?forum_id=8549
Homepage: http://www.gnugk.org/

[Index of Archives]     [SIP]     [Open H.323]     [Gnu Gatekeeper]     [Asterisk PBX]     [ISDN Cause Codes]     [Yosemite News]

  Powered by Linux