Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: Advice on h...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 21 Topic 24491 of 28006
Post > Topic >>

Re: Advice on how to return a list of values

by Ian Collins <ian-news@[EMAIL PROTECTED] > Feb 3, 2008 at 08:01 PM

Remo D. wrote:
> Hi! I'm writing a function that returns an array of (at maximum) 64
> pointers to char. I have thought of three possibility:
> 
> - The caller p***** a pointer to a previously allocated array of 64
> pointers. Similarly to sprintf(), the caller is entirely responsible for
> handling the memory. The drawback is that my function will have to rely
> on the correctness of such pointer to work properly.
> 
> - My function allocates the array and returns it. This is similar to
> strdup(). Here I will have to rely on the caller function to properly
> free the array, which is something I'm not very comfortable with.
> 
> - I'll have a  "static char *ret[64]" in my function and will return
> ret. I've not been able to think of a library function that behaves this
> way, so I guess it's not reccomended. The good is that I'm free from
> allocation/freeing problem.   The bad is that the return values will be
> overwritten at each call; if the user wants to keep the return values
> for subsequent use he has to copy and store them somewhere.  Another
> drawback is that it consumes memory even if the function will never be
> called.
> 
> In my specific case, I was leaning toward the third option but I'd like
> to hear your opinion on pitfalls, things that I've missed or alternative
> approaches that could work better.
> 
The first would be my choice.

There are standard C (time functions for example) and many POISX
networking functions return a pointer to shared internal data.  The
biggest drawback with this is it can cause chaos in threaded
applications.  So they (POSIX) also provide another version which accept
the data to be written as a parameter.

-- 
Ian Collins.
 




 21 Posts in Topic:
Advice on how to return a list of values
"Remo D." <r  2008-02-03 07:47:21 
Re: Advice on how to return a list of values
Ian Collins <ian-news@  2008-02-03 20:01:14 
Re: Advice on how to return a list of values
"Remo D." <r  2008-02-03 08:38:57 
Re: Advice on how to return a list of values
pete <pfiland@[EMAIL P  2008-02-03 06:32:31 
Re: Advice on how to return a list of values
Keith Thompson <kst-u@  2008-02-02 23:57:26 
Re: Advice on how to return a list of values
"Remo D." <r  2008-02-03 11:35:07 
Re: Advice on how to return a list of values
Paul Hsieh <websnarf@[  2008-02-03 00:33:45 
Re: Advice on how to return a list of values
"Remo D." <r  2008-02-03 11:47:21 
Re: Advice on how to return a list of values
"Bartc" <bc@  2008-02-03 12:27:46 
Re: Advice on how to return a list of values
"christian.bau"  2008-02-03 05:44:30 
Re: Advice on how to return a list of values
richard@[EMAIL PROTECTED]  2008-02-03 18:41:47 
Re: Advice on how to return a list of values
cri@[EMAIL PROTECTED] (R  2008-02-03 19:08:08 
Re: Advice on how to return a list of values
CBFalconer <cbfalconer  2008-02-03 20:55:39 
Re: Advice on how to return a list of values
Ian Collins <ian-news@  2008-02-04 16:41:32 
Re: Advice on how to return a list of values
Tim Smith <reply_in_gr  2008-02-03 23:59:00 
Re: Advice on how to return a list of values
Ian Collins <ian-news@  2008-02-04 21:53:30 
Re: Advice on how to return a list of values
"Remo D." <r  2008-02-04 12:01:22 
Re: Advice on how to return a list of values
SM Ryan <wyrmwif@[EMAI  2008-02-04 20:21:34 
Re: Advice on how to return a list of values
CBFalconer <cbfalconer  2008-02-04 17:51:38 
Re: Advice on how to return a list of values
SM Ryan <wyrmwif@[EMAI  2008-02-05 21:24:07 
Re: Advice on how to return a list of values
Michal Nazarewicz <min  2008-02-06 21:21:38 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Nov 21 12:36:43 CST 2008.