Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > substitute for string 0 termination

Reply
Thread Tools

substitute for string 0 termination

 
 
Felix Kater
Guest
Posts: n/a
 
      01-25-2005
The C-faq says that "The malloc/free implementation remembers the size
of each block allocated and returned, so it is not necessary to remind
it of the size when freeing."

Could that length information somehow be used as a substitute for
0-termination of strings?

Felix
 
Reply With Quote
 
 
 
 
John Valko
Guest
Posts: n/a
 
      01-25-2005
Felix Kater wrote:
> The C-faq says that "The malloc/free implementation remembers the size
> of each block allocated and returned, so it is not necessary to remind
> it of the size when freeing."
>
> Could that length information somehow be used as a substitute for
> 0-termination of strings?
>
> Felix


The length that the implementation keeps track of is the size of the
block allocated by malloc() which, assuming success, will be at least
the size you requested. Of course, you can store strings which are
shorter than this without any trouble, and the length will be different.
Furthermore, the way the implementation decides to keep track of the
size of the block returned by malloc() is up to the implementation. So
any hack which you discover for reading the actual size allocated is
completely non-portable. You could, however, use a structure where the
length (of the string) is stored, and use that instead of a terminating
0. However, since the C library functions require 0 termination for
strings, you'll have a much more difficult time passing the string to
these functions. It would make sense, though, to keep track of the
length if you anticipate repetetive calls to strlen() when the length is
not changing, thus avoiding needless O(n) operations.

Hope that helps,
--John
 
Reply With Quote
 
 
 
 
alexmdac@hotmail.com
Guest
Posts: n/a
 
      01-25-2005
Yes, you could speed up length tests by storing the length before the
first character like this. The "fast" strings are still
zero-terminated, so they work with the CRT string functions.

char * makeFastString( char *regularString )
{
size_t len, *p;
len = strlen( regularString );
p = malloc( len + 1 + sizeof( size_t ) );
if( !p )
return NULL;
*p = len;
strcpy( (char *)( p + 1 ), regularString );
return (char *)( p + 1 );
}

size_t lengthOfFastString( char *fastString )
{
size_t *p = (size_t *) fastString;
return *( p - 1 );
}

void deleteFastString( char *fastString )
{
size_t *p = (size_t *) fastString;
free( p );
}

 
Reply With Quote
 
alexmdac@hotmail.com
Guest
Posts: n/a
 
      01-25-2005
Oops typo: free( p ) should be free( p - 1 )

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      01-26-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> Yes, you could speed up length tests by storing the length before the
> first character like this. The "fast" strings are still
> zero-terminated, so they work with the CRT string functions.


> void deleteFastString( char *fastString )
> {
> size_t *p = (size_t *) fastString;
> free( p );
> }


I think

free(fastString);

is what you really want to use, instead of

deleteFastString(fastString);

--
pete
 
Reply With Quote
 
alexmdac@hotmail.com
Guest
Posts: n/a
 
      01-26-2005
Yeah there was a typo. Unfortunately you can't edit posts here.

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      01-26-2005
(E-Mail Removed) writes:

> Yeah there was a typo. Unfortunately you can't edit posts here.


Usenet has a `supersedes' feature that you could use.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
 
Reply With Quote
 
Mac
Guest
Posts: n/a
 
      01-26-2005
On Wed, 26 Jan 2005 00:11:28 +0100, Felix Kater wrote:

> The C-faq says that "The malloc/free implementation remembers the size
> of each block allocated and returned, so it is not necessary to remind
> it of the size when freeing."
>
> Could that length information somehow be used as a substitute for
> 0-termination of strings?
>
> Felix


In some hypothetical alternate universe, the committee responsible for
defining the C programming language could have opted to handle strings
differently.

In the universe we actually inhabit, the size of a block returned by
malloc can never be accessed directly by the programmer if the code is to
remain portable.

In any event, there are plenty of times when you don't want to malloc a
chunk of memory to be exactly the size of your string.

However, you could always define a struct wrapper for your strings
which remembers how big they are. See the untested fragment below.

struct my_string
{
size_t size;
char *s;
};

struct my_string *new_my_string(size_t size)
{
struct my_string *new_string;

new_string = malloc(sizeof *new_string);
if (new_string == NULL)
return NULL;
new_string->s = malloc(size);
if (new_string->s == NULL)
{ free(new_string);
return NULL;
}
new_string->size = size;
return new_string;
}

That kind of thing.

--Mac

 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      01-26-2005
Felix Kater wrote:
> The C-faq says that "The malloc/free implementation remembers the size
> of each block allocated and returned, so it is not necessary to remind
> it of the size when freeing."
>
> Could that length information somehow be used as a substitute for
> 0-termination of strings?
> ...


No.

Firstly, C standard library does not provide user with any means to
access this information.

Secondly, memory allocation functions are not guaranteed to allocate
exactly the requested amount of memory. Successful allocation requiest
might allocate more memory than was actually requested, which is
perfectly legal in C.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Servé La
Guest
Posts: n/a
 
      01-26-2005

"Andrey Tarasevich" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed)...
> Firstly, C standard library does not provide user with any means to
> access this information.
> Secondly, memory allocation functions are not guaranteed to allocate
> exactly the requested amount of memory. Successful allocation requiest
> might allocate more memory than was actually requested, which is
> perfectly legal in C.


I understand this of course, but still I'd like to know why a function like
memsize can't be introduced in the std library. Maybe more bytes are
allocated than requested, a program shouldn't invoke undefined behaviour
when those extra bytes are overwritten right.
I think a portable function like this could be easily added to C and it
would be used a lot considering how many people have asked for something
like this.


 
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
How to use python regular expression to substitute string value Allerdyce.John@gmail.com Python 7 02-26-2006 09:23 PM
Macro without string to substitute Casanova C Programming 6 11-24-2004 02:15 PM
Macro without string to substitute Casanova C Programming 1 11-21-2004 08:12 PM
Read an XQuery file, substitute a filename for a symbol and save in a string Jeff Kish Java 2 11-15-2004 11:10 AM
can string be a substitute of char s[] or char* s ? Lingyun Yang C++ 2 04-10-2004 02:41 PM



Advertisments