yezi wrote:[color=blue]
> Hi: all:
>
> is some mem cat function with is like "strcat" ?
>[/color]
No. You can realloc() one memory region, then memcpy() the other at the end;
or just malloc() a new one and copy both. You must know the size of both
regions, obviously.
"yezi" <ye_line@hotmai l.com> wrote:
[color=blue]
> is some mem cat function with is like "strcat" ?[/color]
No, and there cannot be. Consider: how does strcat() know where the
first string ends? How would a hypothetical memcat() do the same? (Hint:
it cannot.)
Richard Bos wrote:[color=blue]
> "yezi" <ye_line@hotmai l.com> wrote:
>[color=green]
> > is some mem cat function with is like "strcat" ?[/color]
>
> No, and there cannot be. Consider: how does strcat() know where the
> first string ends? How would a hypothetical memcat() do the same? (Hint:
> it cannot.)[/color]
"Suman" <skarpio@gmail. com> wrote:
[color=blue]
> Richard Bos wrote:[color=green]
> > "yezi" <ye_line@hotmai l.com> wrote:
> >[color=darkred]
> > > is some mem cat function with is like "strcat" ?[/color]
> >
> > No, and there cannot be. Consider: how does strcat() know where the
> > first string ends? How would a hypothetical memcat() do the same? (Hint:
> > it cannot.)[/color]
>
> Then, we shouldn't have memcmp either ? ;)[/color]
memcmp() doesn't have to figure out for itself where its data ends. You
tell it. You _could_ write a memcat() that you also tell yourself where
its first data block ends. It would be rather pointless, though:
Richard Bos wrote:[color=blue]
> "Suman" <skarpio@gmail. com> wrote:
>[color=green]
> > Richard Bos wrote:[/color][/color]
[ replying to the OP]
[color=blue][color=green][color=darkred]
> > > Consider: how does strcat() know where the
> > > first string ends? How would a hypothetical memcat() do the same? (Hint:
> > > it cannot.)[/color]
> >
> > Then, we shouldn't have memcmp either ? ;)[/color]
>
> memcmp() doesn't have to figure out for itself where its data ends. You
> tell it. You _could_ write a memcat() that you also tell yourself where
> its first data block ends. It would be rather pointless, though:[/color]
Correct. To me, the strcat/memcat comparison was a poor one.
[color=blue]
> void *memcat(void *dest, void *src, size_t at, size_t len)
> {[/color]
I felt free to drop a:
return
[color=blue]
> memcpy(dest+at, src, len);
> }[/color]
"yezi" <ye_line@hotmai l.com> writes:
[color=blue]
> Hi: all:
>
> is some mem cat function with is like "strcat" ?[/color]
Not in the sense that it appends one region of memory to
another, since there is no way to know where a memory
region ends. But if you know how large your regions are
you could just use memcpy or memmove from <string.h>
If is quite trivial to implement strcat in terms of strlen
to get the sizes, and memcpy to append. I don't know what
you want to accomplish, but you could take this code as
a starting point:
(Original strcat is probably more efficient, since it doesn't
have to iterate through the src string twice).
"Suman" <skarpio@gmail. com> wrote:
[color=blue]
> Richard Bos wrote:[color=green]
> > "Suman" <skarpio@gmail. com> wrote:
> >[color=darkred]
> > > Richard Bos wrote:[/color][/color]
>
> [ replying to the OP]
>[color=green][color=darkred]
> > > > Consider: how does strcat() know where the
> > > > first string ends? How would a hypothetical memcat() do the same? (Hint:
> > > > it cannot.)
> > >
> > > Then, we shouldn't have memcmp either ? ;)[/color]
> >
> > memcmp() doesn't have to figure out for itself where its data ends. You
> > tell it. You _could_ write a memcat() that you also tell yourself where
> > its first data block ends. It would be rather pointless, though:[/color]
>
> Correct. To me, the strcat/memcat comparison was a poor one.[/color]
I agree, but that's what the OP wanted: a function that does for the
mem*() functions what strcat() does for str*(). Since that comparison
does indeed not work, the answer is: there isn't one.
[color=blue][color=green]
> > void *memcat(void *dest, void *src, size_t at, size_t len)
> > {[/color]
>
> I felt free to drop a:
> return[/color]
In article <438eac39.14500 2778@news.xs4al l.nl>, rlb@hoekstra-uitgeverij.nl (Richard Bos) wrote:
[color=blue]
> "yezi" <ye_line@hotmai l.com> wrote:
>[color=green]
> > is some mem cat function with is like "strcat" ?[/color]
>
> No, and there cannot be. Consider: how does strcat() know where the
> first string ends? How would a hypothetical memcat() do the same? (Hint:
> it cannot.)[/color]
Since functions like free() and realloc() know how big a memory region
is, then why can't the hypothetical memcat() know how big it is?
Rudolf <rthered@bigfoo t.com> writes:[color=blue]
> In article <438eac39.14500 2778@news.xs4al l.nl>,
> rlb@hoekstra-uitgeverij.nl (Richard Bos) wrote:
>[color=green]
>> "yezi" <ye_line@hotmai l.com> wrote:
>>[color=darkred]
>> > is some mem cat function with is like "strcat" ?[/color]
>>
>> No, and there cannot be. Consider: how does strcat() know where the
>> first string ends? How would a hypothetical memcat() do the same? (Hint:
>> it cannot.)[/color]
>
> Since functions like free() and realloc() know how big a memory region
> is, then why can't the hypothetical memcat() know how big it is?[/color]
Not really. The existing mem* functions work on arbitrary blocks of
memory, which could be allocated in any of several ways (stack, heap,
global). Whatever hidden information exists for malloc()ed objects
doesn't apply to the others.
If the hypothetical memcat() applied only to malloc()ed objects, it
could work, but then it probably shouldn't have name starting with
"mem".
I haven't seen a rigorous description in this thread of just what
memcat() is supposed to do.
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Rudolf <rthered@bigfoo t.com> writes:
[color=blue]
> In article <438eac39.14500 2778@news.xs4al l.nl>,
> rlb@hoekstra-uitgeverij.nl (Richard Bos) wrote:
>[color=green]
> > "yezi" <ye_line@hotmai l.com> wrote:
> >[color=darkred]
> > > is some mem cat function with is like "strcat" ?[/color]
> >
> > No, and there cannot be. Consider: how does strcat() know where the
> > first string ends? How would a hypothetical memcat() do the same? (Hint:
> > it cannot.)[/color]
>
> Since functions like free() and realloc() know how big a memory region
> is, then why can't the hypothetical memcat() know how big it is?[/color]
What would be the semanics of the following program with your
suggested memcat?
#inlcude <stdlib.h>
#include <memcat_header. h> /* whatever that is */
#define BUF_SZ 100
int static_buffer[BUF_SZ];
int* static_ptr;
int main(void)
{
int automatic_buffe r[BUF_SZ];
int* automatic_ptr;
int* free_store_ptr = malloc(BUF_SZ * sizeof *free_store_ptr );
/* check for malloc failure */
for (i = 0; i < sizeof static_buffer; ++i) {
static_buffer[i] = i;
automatic_buffe r[i] = i;
free_store_ptr[i] = i;
}
/*
* In the following calls to memcat:
* how much memory would be copied,
* and where would the destiantion be?
* Explain that, and I'll tell you why
* memcat as your proposal can't exist
*/
Comment