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

Proper modularization technique

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?
routine(){
char string[16];

called_function(string);
}

called_function(char* string){
strcpy(string,"string");
}

2) Make the string area static for the calling routine?
routine(){
char *string_pointer;
string_pointer=called_function();
}

char* called_function(char* string){
static char string[16];
strcpy(string,"string");
return string;
}

3) Make the string area global to that file?
char string[16];

calling_routine(){called_function();}
called_function(){strcpy(string,"string");}

Part 2 of this would be, I have a whole bunch of these small routines
that need less than 256 bytes of memory for storing such strings.
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?
Nov 10 '08 #1
5 1894
On Nov 10, 7:51*pm, drhowarddrfine <robbel...@gmail.comwrote:
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?
There are many variants accroding to your application design at all.
IMHO, as I understand your ideas, you can just store string within
calling function and just return pointer:

void
routine() {
char *processed = called_function(*processed);
// you can ignore return value
// char *processed;
// called_function(*processed);
...
// code goes here
...
// Of course you must free the memory
free(processed);
}

char*
called_function(char * str) {
// allocates memory
strncpy(str, "YourString", ...) ;
return str;
}
Nov 10 '08 #2
First, I mistyped
* * // called_function(*processed);
I mean // called_function(processed);

Second, I think that seperating allocation and deallocation is not a
good idea. May be you should store string within the caller:

void
routine() {
char *processed = malloc(...);
if(processed == NULL) { ... }

// Of course you may write
// char processed[123];

called_function(processed);
...
// code goes here
...

// In case of malloc
free(processed);
}

void
called_function(char * str) {
strcpy(str, "YourString") ;
}

What others say?
Nov 10 '08 #3
drhowarddrfine 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:
Much depends on your definition of "best" ...
1) Should I set aside an array in the calling routine and have the
function strcpy to that?
routine(){
char string[16];

called_function(string);
}

called_function(char* string){
strcpy(string,"string");
}
This works if `16' (which probably ought to be a #define) is
easily predictable and not over-large. The utility of returning
a pointer to the string -- something the caller already knows --
is questionable.

An important variant is to let the caller manage the storage
*and* tell the called routine how much there is:

routine() {
char string[HOWMANY];
called_function(string, sizeof string);
}

char *called_function(char *string, size_t size) {
if (strlen("string") < size) {
strcpy(string, "string");
return string;
}
return NULL; /* means "not enough room" */
}
2) Make the string area static for the calling routine?
routine(){
char *string_pointer;
string_pointer=called_function();
}

char* called_function(char* string){
Where did the argument come from? Copy-and-paste error?
static char string[16];
strcpy(string,"string");
return string;
}
This works if you never need more than one returned value
at a time (or if the value is the same every time). It would
not work well if done this way:

char *called_function(int answer){
static char string[16];
strcpy(string, answer == 42 ? "Right!" : "Rong!");
return string;
}
...
printf ("%s %s\n", called_function(42), called_function(24));
3) Make the string area global to that file?
char string[16];

calling_routine(){called_function();}
called_function(){strcpy(string,"string");}
Same drawback as (2), plus the additional drawbacks of global
variables. Globals *are* useful, sometimes, but they are very
easy to overuse and can engender maintenance nightmares. Avoid
them unless they're unavoidable.
Part 2 of this would be, I have a whole bunch of these small routines
that need less than 256 bytes of memory for storing such strings.
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?
"We follow two rules in the matter of optimization:
Rule 1. Don't do it.
Rule 2 (for experts only). Don't do it yet - that is, not
until you have a perfecly clear and unoptimized solution."
-- M.A. Jackson

It is likely that all of the approaches you mention will be faster
than managing the memory with malloc() and free(). But here's the
crucial question: Will their speed be enough greater to make up for
their other drawbacks, or even enough to notice? Keep in mind all the
other things your program is (presumably) doing, and ask yourself
whether there might be something more important to fret about ...

Giving your car a fresh coat of wax might reduce air resistance
and improve the fuel economy, but taking the boat trailer off the
back will probably improve it more.

--
Er*********@sun.com

Nov 10 '08 #4
On Nov 10, 11:51*am, drhowarddrfine <ro*******@gmail.comwrote:
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

Nov 10 '08 #5
Thanks guys. You verified what I was thinking. My sample code above
was just to give a view of what I was trying to say though I may have
confused a little with some omissions.
Nov 10 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Peter Rohleder | last post by:
Hi, I have a few simple questions in order to use modularized xhtml and getting it to work. A simple example may make this obviouse: Lets say we want to create a simple xml-file to reflect...
171
by: Raman | last post by:
Hi All, Here is a small Code, int main(void) { char *p=(char *) malloc(100); strcpy(p,"Test1234567890"); p=p+10; free(p);
0
by: C.W.Holeman II | last post by:
As K&R state the hardest part is getting a first instance to work. So I am looking for a "hello, world!" example for adding an additional element to an XHTML file. <html> <head><title>Hello,...
0
by: mk189 | last post by:
Hi, I am trying to create XML schema of custom markup language, enriched by XHTML. In simplified version, the XML documet could look like that: <a:alarm-manual xmlns:a="alarm-manual"...
2
by: Bob Alston | last post by:
Recently I have been helping a nonprofit modify a system built in Access, that they acquired from another nonprofit. I am doing this as a volunteer. I would like your perspective on two...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.