PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > memcat fn
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
memcat fn

Réponse
 
LinkBack Outils de la discussion
Vieux 23/11/2007, 13h28   #1
aarklon@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut memcat fn

Hi all,

is it possible to write a function named memcat, which offers
functionality similar to that of the strcat fn, i.e i mean a function
on the following lines:-

void * memcat(void *s1, void *s2);

now s1 should point to the beginning of the concatenated memory
region. now as in strcat how to determine the terminating memory
location...???

  Réponse avec citation
Vieux 23/11/2007, 13h32   #2
Mark Bluemel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: memcat fn

aarklon@gmail.com wrote:
> Hi all,
>
> is it possible to write a function named memcat, which offers
> functionality similar to that of the strcat fn, i.e i mean a function
> on the following lines:-
>
> void * memcat(void *s1, void *s2);


You need to define the functionality you expect from such a function.

> now s1 should point to the beginning of the concatenated memory
> region. now as in strcat how to determine the terminating memory
> location...???


That, as the man said so memorably, is the question.
  Réponse avec citation
Vieux 23/11/2007, 14h10   #3
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: memcat fn

aarklon@gmail.com wrote:
> Hi all,
>
> is it possible to write a function named memcat, which offers
> functionality similar to that of the strcat fn, i.e i mean a function
> on the following lines:-
>
> void * memcat(void *s1, void *s2);
>
> now s1 should point to the beginning of the concatenated memory
> region. now as in strcat how to determine the terminating memory
> location...???


Let's begin with the last question: How to determine
the end. Two ends, really, because you need to find the
end of the existing piece that begins at s1 and the end
of the added piece that begins at s2. The other memxxx
function (memset, memchr, ...) use byte counts for this:
the caller provides an extra argument giving the number
of bytes in the memory area. For memcat there are two
memory areas, hence two counts, and the function looks like

void *memcat(void *s1, size_t n1, void *s2, size_t n2);

Let's not stop there, though. Think for a minute about
what memcat will do, internally. All the area 1 bytes will
remain as they are, untouched, and the new material will be
added right after them. So memcat probably begins with

void *target = (char*)s1 + n1;

.... to get a pointer the the spot where the new material
will go. (The (char*) cast is needed because you can't do
arithmetic on a void* pointer.) What next? The rest of the
job is just copying the area 2 material to its new position.
So the complete implementation of memcat might look like

void *memcat(void *s1, size_t n1, void *s2, size_t n2) {
void *target = (char*)s1 + n1;
memcpy(target, s2, n2);
return s1;
}

.... or, with some abbreviation
void *memcat(void *s1, size_t n1, void *s2, size_t n2) {
memcpy((char*)s1 + n1, s2, n2);
return s1;
}

(An "industrial-strength" version would probably use const
on s2, and a version for C99 compilers would also use the
restrict qualifier, but the outline would be the same.)

In other words, memcat is just memcpy with a different
starting point! And that's probably why it doesn't exist in
the Standard library: it's a trivial variation on a function
that's already provided. Adding it would be a little bit like
adding a sqrt_half function that computed the square root of
one-half its argument: A task that's easily done by calling
the usual sqrt function with a halved argument to begin with.

--
Eric Sosman
esosman@ieee-dot-org.invalid
  Réponse avec citation
Vieux 23/11/2007, 21h43   #4
Martien Verbruggen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: memcat fn

On Fri, 23 Nov 2007 09:10:52 -0500,
Eric Sosman <esosman@ieee-dot-org.invalid> wrote:
> aarklon@gmail.com wrote:
>> Hi all,
>>
>> is it possible to write a function named memcat, which offers
>> functionality similar to that of the strcat fn, i.e i mean a function
>> on the following lines:-
>>
>> void * memcat(void *s1, void *s2);
>>
>> now s1 should point to the beginning of the concatenated memory
>> region. now as in strcat how to determine the terminating memory
>> location...???


[snip]
>
> void *memcat(void *s1, size_t n1, void *s2, size_t n2) {
> memcpy((char*)s1 + n1, s2, n2);
> return s1;
> }


of course, if you wrote it like this, you'd have to include string.h, in
which case you shouldn't be naming your function memcat()i as long as it
has external linkage, as that would be a reserved identifier.

Martien
--
|
Martien Verbruggen | Computers in the future may weigh no more
| than 1.5 tons. -- Popular Mechanics, 1949
|
  Réponse avec citation
Vieux 23/11/2007, 22h40   #5
$)CHarald van D)&k
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: memcat fn

On Sat, 24 Nov 2007 08:43:57 +1100, Martien Verbruggen wrote:
> On Fri, 23 Nov 2007 09:10:52 -0500,
> Eric Sosman <esosman@ieee-dot-org.invalid> wrote:
>>
>> void *memcat(void *s1, size_t n1, void *s2, size_t n2) {
>> memcpy((char*)s1 + n1, s2, n2);
>> return s1;
>> }

>
> of course, if you wrote it like this, you'd have to include string.h, in
> which case you shouldn't be naming your function memcat()i as long as it
> has external linkage, as that would be a reserved identifier.


memcat is a reserved external name even if you don't include string.h.
Including string.h means that you're also not allowed to define static
void *memcat(...), or typedef void memcat, since it becomes reserved as a
file scope identifier.
  Réponse avec citation
Vieux 23/11/2007, 23h25   #6
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: memcat fn

aarklon@gmail.com wrote:
>
> is it possible to write a function named memcat, which offers
> functionality similar to that of the strcat fn, i.e i mean a
> function on the following lines:-
>
> void * memcat(void *s1, void *s2);
>
> now s1 should point to the beginning of the concatenated memory
> region. now as in strcat how to determine the terminating memory
> location...???


Strings are terminated by the final '\0'. Memory blocks have no
such delimiter. Thus you have to have, and supply, the sizes of
the memory blocks. Therefore the prototype could be:

int memcat(char *s1, size_t sz1, /* input block & destination
*/
char *s2, size_t sz2, /* block to add to it */
size_t szout); /* maximum size of *s1 */

and it can return an error indicator, such as non-zero for failure
to fit everything.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
Vieux 24/11/2007, 04h13   #7
aarklon@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: memcat fn

On Nov 23, 4:43 pm, Martien Verbruggen <m...@tradingpost.com.au>
wrote:
> On Fri, 23 Nov 2007 09:10:52 -0500,
> Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
>
> > aark...@gmail.com wrote:
> >> Hi all,

>
> >> is it possible to write a function named memcat, which offers
> >> functionality similar to that of the strcat fn, i.e i mean a function
> >> on the following lines:-

>
> >> void * memcat(void *s1, void *s2);

>
> >> now s1 should point to the beginning of the concatenated memory
> >> region. now as in strcat how to determine the terminating memory
> >> location...???

>
> [snip]
>
>
>
> > void *memcat(void *s1, size_t n1, void *s2, size_t n2) {
> > memcpy((char*)s1 + n1, s2, n2);
> > return s1;
> > }

>
> of course, if you wrote it like this, you'd have to include string.h, in
> which case you shouldn't be naming your function memcat()i as long as it
> has external linkage, as that would be a reserved identifier.
>
> Martien
> --
> |
> Martien Verbruggen | Computers in the future may weigh no more
> | than 1.5 tons. -- Popular Mechanics, 1949
> |


I searched my /usr/include/string.h file for memcat but i didn't find
any. I use gcc v 4.01
  Réponse avec citation
Vieux 24/11/2007, 09h03   #8
Flash Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: memcat fn

aarklon@gmail.com wrote, On 24/11/07 04:13:
> On Nov 23, 4:43 pm, Martien Verbruggen <m...@tradingpost.com.au>
> wrote:


<snip>

>> of course, if you wrote it like this, you'd have to include string.h, in
>> which case you shouldn't be naming your function memcat()i as long as it
>> has external linkage, as that would be a reserved identifier.
>>
>> Martien
>> --
>> |
>> Martien Verbruggen | Computers in the future may weigh no more
>> | than 1.5 tons. -- Popular Mechanics, 1949
>> |


Please don't quote peoples signatures, the bit typically after the "-- "
or anything else not relevant to your reply.

> I searched my /usr/include/string.h file for memcat but i didn't find
> any. I use gcc v 4.01


Martien did not say it was defined, he said it was reserved. I.e. you
are *not* allowed to use that name whether it is currently used or not.
The GNU people could decide to use it in the next minor release thus
breaking your code, or they might be using some "compiler magic" which
well break your code, or they could perfectly legally just detect that
you have used it and deliberately generate code that generates random
insults.
--
Flash Gordon
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h46.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13897 seconds with 16 queries