Re: Proper modularization technique



On Nov 10, 11:51 am, drhowarddrfine <robbelics@xxxxxxxxx> wrote:
I'm unsure the best way to do this.  I want to call a function that
will generate a string to be used by the calling routine.  Where is
the best place to store that string?  iow:
1) Should I set aside an array in the calling routine and have the
function strcpy to that?

No, because

- the way you wrote it, called_function() will have no way to know
how large the passed array is, and thus is likely to write pass the
end of it. Of course, this is easily fixed by having the function
accept an extra argument that represents the array size, but then
there's the possibility that
- the function might need to store more characters than the array
can hold, thus causing truncation. If truncation isn't a problem
(e.g., you can still get the remaining data in a future call, as with
fgets()), then this is a good way to go.

2) Make the string area static for the calling routine?

No, because

- if your application is multi-threaded, this will break things;
- you can't have the function generate two or more strings and hold
them at the same time (in other words, calling the function will
modify what was previously on the array, even if you still need it);
- there's still the issue of truncation.

3) Make the string area global to that file?

No, because

- "global variables are evil." :-)

Would it be better to just malloc to acquire the memory for that and
then release it?  Does that cause relative program slow down doing all
that acquire/releasing?

That has the advantage that you can call realloc() to make the buffer
as large as necessary. And yes, allocating on the heap is generally
slower than allocating on the stack, but you probably don't need to
worry about that unless you're doing micro-optimization.

Sebastian

.



Relevant Pages

  • Re: Data From CREATEOBJECT
    ... the first column and the "key" in the second. ... The array is public so that all of the objects can find it and the main program can find all of the objects. ... So there is no need to store it once more in the array. ... If it is a variable set of properties, then it might be better to build a long string of the property names and the respective values and store this string in a memo field. ...
    (microsoft.public.fox.programmer.exchange)
  • Re: what is the best datatype for..
    ... If you are only going to store it, then use a string. ... each value in the array, check to see if it already exists in the ... If it does, then just increment the count, otherwise, add ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Is there such a thing as dynamic arrays in classic rexx?
    ... my program but now I have a need for a dynamic array. ... String length of 0000006A000005F2 is 16 ... just store the word-numbers elsewhere. ...
    (comp.lang.rexx)
  • Re: some bit math help
    ... >> settings as a VB string. ... > I would suggest you store it as a byte array, ...
    (microsoft.public.vb.general.discussion)
  • Re: Proper modularization technique
    ... will generate a string to be used by the calling routine. ... IMHO, as I understand your ideas, you can just store string within ... called_function(char * str) { ...
    (comp.lang.c)