473,408 Members | 2,477 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,408 software developers and data experts.

passing address of pointer for more useful functions

MJL
I am working on a small project that involves the manipulation of
dynamically allocated memory for strings. I was wondering why the
string.h functions are the way they are and why not as follows:

mystrcat(char** s1,char** s2);

where s1's memory allocation can now be handled inside of the function
and s2 can be freed and pointed to NULL inside the function.

I'm not saying the built in functions are not useful as they are, but
this seems to be a simple way to hide away some of the messy memory
management statements.

char *s1,*s2;
assignstrings(&s1,&s2);
mystrcat(&s1,&s2);
dostuff(s1);
free s1;

see, not a single visible malloc statement and only one string to free
at end. Of course the functions would have to be well documented for
the use of others.
Nov 14 '05 #1
2 1627
MJL <ma**@affordablemedicalsoftware.com> wrote:
I am working on a small project that involves the manipulation of
dynamically allocated memory for strings. I was wondering why the
string.h functions are the way they are and why not as follows: mystrcat(char** s1,char** s2); where s1's memory allocation can now be handled inside of the function
and s2 can be freed and pointed to NULL inside the function.


Probably because such functions wouldn't be of general usefulness
and also would open up some nasty cans of worms. Take for example
the string pointed to by s2. There's nothing that would tell you
within your function that this string is actually pointing to
malloc()ed memory - it could also be a pointer to a string literal
or a char array. In that case you wouldn't be able to call free()
on it. Moreove, there are lots of cases where you don't want to
free() the source string.

The same problem would exist for s1. It could also be a pointer
to a char array. And you wouldn't even have any information how
much memory s1 is already pointing to. So how do you decide if
you can realloc() memory at all and if yes if you need to? More-
over, the caller would have to be aware that the address of s1
can change and if there are several other pointers around
pointing to the original string all of them would have to be
updated.

All in all I would say that you gain only a minimal amount in
functionality while making it rather difficult to use that kind
of functions correctly (since they can't be used with all kinds
of strings but only those for which memory has been obtained
dynamically). So they may be very useful for the project of yours
where you know exactly what you do but as general purpose
functions there would be much too many pitfalls the casual user
would have to be aware of.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
>I am working on a small project that involves the manipulation of
dynamically allocated memory for strings. I was wondering why the
string.h functions are the way they are and why not as follows:

mystrcat(char** s1,char** s2);

where s1's memory allocation can now be handled inside of the function
and s2 can be freed and pointed to NULL inside the function.
All strings are NOT necessarily dynamically allocated strings.
I'm not saying the built in functions are not useful as they are, but
this seems to be a simple way to hide away some of the messy memory
management statements.
The memory management details still need to be visible, among
other things, to prevent memory leaks.
char *s1,*s2;
assignstrings(&s1,&s2);
mystrcat(&s1,&s2);
dostuff(s1);
free s1;

see, not a single visible malloc statement and only one string to free
at end.
mystrcat() frees its second argument!!!???!!! This is an unobvious
behavior that is likely to lead to bugs. Frequently I want to use
a string without destroying it. I'd be interested in seeing what
you intend to do with functions like printf(), strchr(), strtol(),
and strtok().

Another extremely unobvious feature is that mystrcat() may MOVE
where the string points, thereby invalidating any pointers *INTO*
the string.

Your proposed functions do have their uses (although I disagree
strongly with the idea that a read-only string argument to a function
like mystrcat() should be freed by it), but it gets much messier
when you try to deal with strings as *BOTH* strings and sequences
of characters. What's the replacement for s1[2]? Call a function
which allocates a string of length 1 and copies the character (and
adds a terminator)? (Sounds really inefficient, and when do I free
this short string?) How does one iterate over the characters in a
string, for, say, parsing it? It sounds like this would involve a
lot of copying and allocating and freeing.
Of course the functions would have to be well documented for
the use of others.


It seems to me you'd have to know these functions really, really,
really well in order to assure that you don't have memory leaks.
*ANY* function you pass one of these char **'s to might allocate,
free, or reallocate memory. Auditing a program for memory leaks
just got much, much messier. Does the new version of fopen() free()
its first argument? I usually want to keep that around for error
messages.

Gordon L. Burditt
Nov 14 '05 #3

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

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
1
by: Shawn | last post by:
As if it won't be clear enough from my code, I'm pretty new to C programming. This code is being compiled with an ANSI-C compatible compiler for a microcontroller. That part, I believe, will be...
2
by: jeniffer | last post by:
gcc -c test.c gcc -o test test.c ./test i=6 0 14 22 42 48 e6 014224248ffffffe6 ea =
9
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.