473,780 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,&s 2);
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 1645
MJL <ma**@affordabl emedicalsoftwar e.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***********@p hysik.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
10181
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 code... TCHAR myArray; DoStuff(myArray);
9
4806
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 am using vector class(STL), the compiler does not allow me to do this. I do realize there is a pitfall in this approach(size of arrays not matching etc), but I wonder how to get around this problem. I have a class hierachy with abstract base...
33
3188
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
4470
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 accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
1
3269
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 irrelavent. My syntax is surely where I am going wrong. I'd like to be able to call this routine to read different values from another device. This routine would be called quite simply as follows: void main() {
2
2441
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
3345
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 limited c & c++ experience. Maybe some of you can help. the problem: I need several instances of a class whose (non-static!) methods should serve as callbacks for a dll (which can' be manipulated/adapted in any
4
2506
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
3504
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;
0
9636
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10075
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8961
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7485
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6727
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.