473,414 Members | 1,697 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,414 software developers and data experts.

returning a string array from function on stack?

I need to write a function that takes a delimited string and produces an array of strings (just like string.Split() in C#).

Is there a way to allocate this array on the stack so that the caller does not have to worry about freeing each pointer?

I'm using GCC C and only have access to standard libraries.

I am a novice in C, but I understand that stack space is quite limited. These arrays should be on the short side as they just contain unix paths.

The reason I am trying to do this is
a) because the code would look cleaner without having the free code in the caller
b) i'm doing this for a class, and the professor specified:

parse path – split into array of strings. (if you allocate memory here, be sure to free it before you return)

which is not really a requirement, but made me wonder how this is accomplished because as I understand it to accomplish this you have to use strtok and allocate a copy of each string onto the heap, copying and enlarging your array when you reach capacity.
Dec 6 '09 #1
4 2842
Banfa
9,065 Expert Mod 8TB
OK whether stack space is limited or not is actually implementation defined. For instance on windows stack space is normally not very limited at all since it can grow. However on many platforms stack space is limited and I would say it is best practice to treat stack space as though it were limited.

You can not do what you describe on the stack using basic types (you could do it with a class) because what you describe requires a array of pointers that can increase in size (and presumably the pointers have to pointer to varying sizes of data). The size of what is allocated on the stack is generally fixed at compile time so anything that changes data size at run time can not be on the stack.
Dec 6 '09 #2
donbock
2,426 Expert 2GB
Are you using C or C++?

In C there are only a few options:

1. The function can malloc space for the fragment strings plus the array of points. It is the caller's responsibility to free this memory when done with it. Sounds like you are trying to avoid this option.

2. The caller can pass the necessary memory to the function. This can be either automatic [stack] variables or malloc'ed by the caller. If the latter then the caller is still responsible for freeing the memory. If this option is chosen then the function ought to be able to signal an error to the caller if the provided memory isn't big enough.

3. The caller and function can share access to file-scope variables (either static or extern). This approach is usually considered ... inelegant. It is also not thread-safe.

Here's an example of option 2 using automatic variables:
Expand|Select|Wrap|Line Numbers
  1. #define NSTRINGS 10
  2. #define MAX_STRING_SIZE 40
  3. ...
  4. void caller(void)
  5. {
  6.    ...
  7.    int isFailed;
  8.    char strings[NSTRINGS][MAX_STRING_SIZE];
  9.    char * const array[NSTRINGS] = {
  10.       strings[0], strings[1], strings[2], strings[3], strings[4],
  11.       strings[5], strings[6], strings[7], strings[8], strings[9] };
  12.    ...
  13.    isFailed = function(..., array, NSTRINGS, MAX_STRING_SIZE);
  14.    ...
  15. }
  16. ...
  17. int function(..., char * const array, int nstrings, int max_string_size)
  18. {
  19. ...
  20. }
Note:
  1. Macros are sometimes necessary in C; they are never necessary and usually ill-advised in C++.
  2. Variables isFailed, strings, and array are automatic variables within function 'caller'. They disappear when 'caller' returns.
  3. Two-dimensional arrays are tricky to use correctly. The only reason I used one here is that it is a concise way to allocate several char arrays all of the same length.
  4. Strictly speaking, trickier use of the two-dimensional array could eliminate the need for the explicit array variable. In my opinion you're better off not being that tricky. Refer to Arrays Revealed to see what I'm talking about.
  5. char * const means that the contents of the strings may be changed, but the string pointers in the array cannot. That's why we need an initializer.
  6. There is no way in C for the function to infer the dimension of the array argument. You should explicitly pass the number of strings in the array and the maximum length of the strings. It is inelegant for the function to refer to the macros direclty, even though they might be in scope.
Dec 7 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
Is there a way to allocate this array on the stack so that the caller does not have to worry about freeing each pointer?
Create a process private heap. Allocate from that heap rather than the CRT heap. At the end just delete the heap.
Dec 7 '09 #4
Thank you guys, that answers my question very well.
Dec 7 '09 #5

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

Similar topics

6
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
1
by: john | last post by:
Relatively new to C coding, so any help would greatly be appreciated. I'm having problems try to return my string array from my parsing function. When I do a printf I am getting the correct value...
2
by: Bnc119 | last post by:
Hello, I have written a C# COM server that has a few methods and a property called DataItems that returns an ArrayList. During the course of execution the ArrayList gets populated with several...
3
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to...
5
by: Mark Ingram | last post by:
Hi, how can i return an array of strings from an unmanaged c++ dll into a c# application? cheers Mark
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
17
by: kleary00 | last post by:
Hi, I am writing a function that needs to return an array of strings and I am having some trouble getting it right. I need some help. Here is what I consider an array of 100 strings: char...
19
by: Adam | last post by:
Hi, I'd like to return an (arbitrary length) string array from a function so that after calling the array I've got a list of strings I can access. After much perusing of the internet I found a...
9
by: =?Utf-8?B?RGFya21hbg==?= | last post by:
Hi, I am wondering how you multi-dimension an array function? My declared function looks like this: Public Function GetCustomerList(ByVal Val1 As String, ByVal Val2 As Long, ByVal Val3 As...
5
by: sarabonn | last post by:
hallo, I have a function which takes 2 values as input and i want to return that 2 values, so how should write the return statement to return 2 values of String array. I also i should...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.