473,320 Members | 2,107 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Return type from a sub-function

Could you please let me know whether the following return types from a sub function are valid?
  • Structure (struct <structname> subfunction_name(<data type>))
  • Pointer (<data type> * subfunction_name(<data type>))

If the above types are invalid, then which do you suggest is a better way to return data as a substitute to the methods mentioned above?

Thank you :-)
May 31 '10 #1

✓ answered by weaknessforcats

Both a structure and a point are valid return values.

However, I would not return any value from a function unless I had a requirement to use the function as an RVAL.

Then if that were the case, I would return a handle rather than a struct or a pointer. You might read: http://bytes.com/topic/c/insights/65...-smart-pointer.

If the function did not need to be an RVAL, then I would use the return for passing back completion codes. Note that if you use a handle, you won't need an argument to pass it back since handles act as references but behave like pointers. That way you can pass a handle to a function and that will allow the function access to the object managed by the handle.

6 2425
weaknessforcats
9,208 Expert Mod 8TB
Both a structure and a point are valid return values.

However, I would not return any value from a function unless I had a requirement to use the function as an RVAL.

Then if that were the case, I would return a handle rather than a struct or a pointer. You might read: http://bytes.com/topic/c/insights/65...-smart-pointer.

If the function did not need to be an RVAL, then I would use the return for passing back completion codes. Note that if you use a handle, you won't need an argument to pass it back since handles act as references but behave like pointers. That way you can pass a handle to a function and that will allow the function access to the object managed by the handle.
May 31 '10 #2
@weaknessforcats
Hi 'weaknessforcats':-)

Thanks for providing me with answers and also for providing me with an insight to 'handles'. I found it pretty useful :-)

Regards,
Anand
Jun 1 '10 #3
Banfa
9,065 Expert Mod 8TB
I'm not sure if this was a C or C++ question but in C (unlike C++) returning a structure from a function would normally be considered bad practice. Because of the way C handles structures there are some thread safety issues and other struct copying issues.

It is possible that at least some of these are legacy and don't occur any more but habits dies hard in the C community.
Jun 1 '10 #4
weaknessforcats
9,208 Expert Mod 8TB
The same is true in C++ when you return a struct. That is, a copy is made. In C++ you do have the ability to write a copy constructor to make this copy rather than be subject to arbitrary copy rules but this is a function call and making that copy will itself take even more time.

You should avoid returning structs (or classes) in C++.
Jun 1 '10 #5
Make a habit of returning return codes from API's instead of returning pointers. pointers which needs to returned should be passed as argument to API's. This will also help in making APIs thread safe.
Jun 1 '10 #6
@mayankarpit
sure will keep that in mind mayank :-) thanks for the suggestion :-)
Jun 2 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Nuno Barros | last post by:
Hello, I am writting a c++ code to crete a kind of a table in memory. Each table has an container of columns, which can be of any type. To do this i created a virtual class Column which has...
5
by: Fabio Fracassi | last post by:
Hi, I belive what i am trying to do is not possible, but I hope someone can change my mind. Here is some code i'd like to write: template <class type> class engine1 {}; template <class...
59
by: Michael C | last post by:
eg void DoIt() { int i = FromString("1"); double d = FromString("1.1"); } int FromString(string SomeValue) {
1
by: Thomas D. | last post by:
Hello all, I'm using the IXmlSerializable interface for a project and encounter some problems when testing my webservice in a client application. I know this interface is undocumented and not...
17
by: benben | last post by:
Given a class template Vector<>, I would like to overload operator +. But I have a hard time deciding whether the return type should be Vector<U> or Vector<V>, as in: template <typename U,...
8
by: WakeBdr | last post by:
I'm writing a class that will query a database for some data and return the result to the caller. I need to be able to return the result of the query in several different ways: list, xml,...
9
by: hufaunder | last post by:
I have a class "TestSuper" that implements the interface "TestBase". The interface has a property of type "ReturnType". The class "TestSuper" does not return "ReturnType" but a derivation...
3
by: BombDrop | last post by:
Can any one help I have a method that will return a List to be bound as a datasource to a combobox see code for population below. I get the following error when i try to compile Error 29 ...
5
by: Brian Withun | last post by:
I have a base class (B) and several derived classes (D1, D2, ...) I have a container class (C) with an attribute (void *d) which is intended to point to any of D1, D2,... My question is, on my...
6
by: Andrus | last post by:
I need to create method which returns my application object type as string : TypeName(typeof( List<int>)) should return "List<int>" or "System.Collections.Generic.List<int>" ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.