Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C string functions

Reply
Thread Tools

C string functions

 
 
gc260262@belgacom.net
Guest
Posts: n/a
 
      06-01-2005
Hi,

I need functions that would return null terminated strings for me, for
example:

char *HTMLi(const char *string)

This function receives a pointer to a null terminated string, and
should return <i>This was the string</i>.

To make it more complex, I could even return the result of the above
function to this:

char *HTMLb(const char *string)

As a result, this is what I expect to have <b><i>This was the
string</i></b>

How about allocating memory; I assume if I allocate memory in these
functions, and never free it, this is a so called memory leak.

Does somebody has a sample code for me having the same logic? Your
feedback is really appreciated!


Thanks,

 
Reply With Quote
 
 
 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      06-01-2005
wrote on 01/06/05 :
> Hi,
>
> I need functions that would return null terminated strings for me, for
> example:
>
> char *HTMLi(const char *string)
>
> This function receives a pointer to a null terminated string, and
> should return <i>This was the string</i>.
>
> To make it more complex, I could even return the result of the above
> function to this:
>
> char *HTMLb(const char *string)
>
> As a result, this is what I expect to have <b><i>This was the
> string</i></b>
>
> How about allocating memory; I assume if I allocate memory in these
> functions, and never free it, this is a so called memory leak.
>
> Does somebody has a sample code for me having the same logic? Your
> feedback is really appreciated!
>
> Thanks,


The obvious way : the function returns an allocated bloc that you have
to free after use.

Another solution could fit.

define a size:

size_t size = 128;
define a string
char *s = malloc(size + 1);

Maintain the size and allocated memory through the converter functions
:

s = HTMLi (s, &size, "Hello world");
printf ("%s", s);
fflush (stdout);

s = HTMLb (s, &size, s);
printf ("%s", s);
fflush (stdout);

Use memmove() to make the copies and realloc() on request (and update
size accordingly). On realloc() error, just exit(), or free() and
return NULL, but the application would have to check against NULL each
time... (can be done simply with some embedded goto...)

#define CHK\
do {if (s==NULL) goto err;} while (0)


s = HTMLi (s, &size, "Hello world");
CHK;
printf ("%s", s);
fflush (stdout);

s = HTMLb (s, &size, s);
CHK;
printf ("%s", s);
fflush (stdout);

err:

Once finished, just free the allocated bloc.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.

 
Reply With Quote
 
 
 
 
Paul Mesken
Guest
Posts: n/a
 
      06-01-2005
On 1 Jun 2005 12:31:42 -0700, wrote:

>Does somebody has a sample code for me having the same logic? Your
>feedback is really appreciated!


There are several ways to do it in C but I'd like to point out that C
is not exactly a language of choice for string operations (always been
a bit of a weak point of C) so, perhaps, you should consider another
programming language for your application if it contains lots of
string handling (Perl, for example).

 
Reply With Quote
 
gc260262@belgacom.net
Guest
Posts: n/a
 
      06-01-2005
Salut Emmanuel!

Thank you for your clear explanation!

I have only one question; if I need to do a realloc in a function,
won't this cost a lot in performance?


Regards
Dirk

 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      06-01-2005
wrote:
> Salut Emmanuel!
>
> Thank you for your clear explanation!
>
> I have only one question; if I need to do a realloc in a function,
> won't this cost a lot in performance?


Please quote enough context so that everyone can see what
you are responding to -- the original message may for example
not have made it to some newsserver (yet).

As for performance issues: The C standard does not tell us
anything about relative performance.
A good rule of thumb for many, but not all systems and
implementations is to start out with a sensible buffer size which
makes realloc()ating a rare event; on realloc()ation, rather
increase the buffer size by a certain factor (1.5 or 2, for example)
than by a fixed amount in order to keep the number of times low.

BTW: It is possible that your implementation gives you a larger
chunk of memory than you requested, to the effect that at reallocation
only administrative information has to be updated.

If you implement a solution using realloc() which is too slow,
be sure to measure carefully to find out where the time loss
happens, i.e. whether this is really the fault of realloc().

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
indiangeek@gmail.com
Guest
Posts: n/a
 
      06-01-2005
wrote:

> Salut Emmanuel!
>
> Thank you for your clear explanation!
>
> I have only one question; if I need to do a realloc in a function,
> won't this cost a lot in performance?
>
>
> Regards
> Dirk


Hi Dirk,
The problem is, your destination string is larger then your source string.
So, you need to realloc/malloc if you want to maintain the prototype you
specified. However, you can take the approach to make the situation a
little better where you allocate the maximum chunk of memory to build the
full HTML upfront. Then, use the following function to modify it in-place.

I have modified the prototype a little and you don't need to define HTMLi
HTMLb indipendently. For example the following program...

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define BOLD "b"
#define ITALICS "i"

int HTMLx(char *str,int max_size, char *type)
{
int prefix_len;
int orig_len;

prefix_len = strlen(type) + 2;
orig_len = strlen(str);
if (max_size < (prefix_len+1)*2)
return -1; /* Failure... not enough space */

memmove(str+prefix_len,str,orig_len+1);

/* Build the prefix */
str[0]='<';
memcpy(str+1,type,strlen(type));
str[prefix_len - 1]='>';

/* Build the suffix */
str[prefix_len + orig_len] = '<';
str[prefix_len + orig_len + 1] = '/';
memcpy(str+prefix_len+orig_len+2,type,strlen(type) );
str[prefix_len + orig_len + strlen(type) + 2] = '>';
}

int main()
{
char *str = malloc(1000);
sprintf(str,"Hello Worldie");
HTMLx(str,1000,ITALICS);
printf("%s\n",str);
HTMLx(str,1000,BOLD);
printf("%s\n",str);
return 0;
}



Will give you output of :

<i>Hello Worldie</i>
<b><i>Hello Worldie</i></b>

Also, you can make it more robust where, when you run out of memory, it
reallocs another chunk. That will improve performance in terms of using
reduced number of reallocs.

Hope that helps.
- IG
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      06-01-2005

<> wrote
>
> I have only one question; if I need to do a realloc in a function,
> won't this cost a lot in performance?
>

Yes.
calls to malloc() or realloc() are always expensive in C terms, though not
always in terms of other languages. Precise execution times do of course
vary from platform to platform.

A good method is to call malloc() for as much memory as you will ever
realistically need, keeping realloc() in reserve so that formally the
function is unbounded. Then call realloc() once at the end to shrink the
block to the required size.


 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      06-02-2005
wrote on 02/06/05 :
> int main()
> {
> char *str = malloc(1000);
> sprintf(str,"Hello Worldie");
> HTMLx(str,1000,ITALICS);
> printf("%s\n",str);
> HTMLx(str,1000,BOLD);
> printf("%s\n",str);
> return 0;
> }


You forgot to free() the block after use and to check against NULL...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-02-2005
wrote:
>
> I need functions that would return null terminated strings for me, for
> example:
>
> char *HTMLi(const char *string)
>
> This function receives a pointer to a null terminated string, and
> should return <i>This was the string</i>.


I suggest you build your code around strlcpy and strlcat. Source,
documentation, and references are available at:

<http://cbfalconer.home.att.net/download/strlcpy.zip>

--
"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare


 
Reply With Quote
 
Michael Knaup
Guest
Posts: n/a
 
      06-02-2005
wrote:

> Hi,
>
> I need functions that would return null terminated strings for me, for
> example:
>
> char *HTMLi(const char *string)
>
> This function receives a pointer to a null terminated string, and
> should return <i>This was the string</i>.
>
> To make it more complex, I could even return the result of the above
> function to this:
>
> char *HTMLb(const char *string)
>
> As a result, this is what I expect to have <b><i>This was the
> string</i></b>
>
> How about allocating memory; I assume if I allocate memory in these
> functions, and never free it, this is a so called memory leak.
>
> Does somebody has a sample code for me having the same logic? Your
> feedback is really appreciated!
>
>
> Thanks,


Working directly with strings is not a good idea for your problem i think.
Maybe you shhould use a stack of strings. Were each element has two strings
(the opening tag and the closing tag or NULL). All you have to do then is
pushing your data onto the stack, print the stack or convert it to a flat
string and freeing the stack.


--
Michael Knaup
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using the string functions (ex. find()) on a multi-symbol string korean_dave Python 2 06-17-2008 10:12 PM
searching an array, string compare functions, string sorting ckirchho@directmedia.de Javascript 2 10-10-2007 11:23 PM
String functions run on empty string Tim Slattery ASP General 1 07-11-2007 06:10 PM
Shared functions vs Non-Shared Functions tshad ASP .Net 11 05-27-2005 05:53 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57