473,396 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to realize the function ?

Hi: all:

is some mem cat function with is like "strcat" ?

Thanks

Dec 1 '05 #1
11 2433
yezi wrote:
Hi: all:

is some mem cat function with is like "strcat" ?

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.

S.
Dec 1 '05 #2
"yezi" <ye*****@hotmail.com> wrote:
is some mem cat function with is like "strcat" ?


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
Dec 1 '05 #3

Richard Bos wrote:
"yezi" <ye*****@hotmail.com> wrote:
is some mem cat function with is like "strcat" ?


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.)


Then, we shouldn't have memcmp either ? ;)

Dec 1 '05 #4
"Suman" <sk*****@gmail.com> wrote:
Richard Bos wrote:
"yezi" <ye*****@hotmail.com> wrote:
is some mem cat function with is like "strcat" ?


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.)


Then, we shouldn't have memcmp either ? ;)


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:

void *memcat(void *dest, void *src, size_t at, size_t len)
{
memcpy(dest+at, src, len);
}

Richard
Dec 1 '05 #5

Richard Bos wrote:
"Suman" <sk*****@gmail.com> wrote:
Richard Bos wrote:
[ replying to the OP]
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 ? ;)


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:


Correct. To me, the strcat/memcat comparison was a poor one.
void *memcat(void *dest, void *src, size_t at, size_t len)
{
I felt free to drop a:
return
memcpy(dest+at, src, len);
}


Dec 1 '05 #6
"yezi" <ye*****@hotmail.com> writes:
Hi: all:

is some mem cat function with is like "strcat" ?


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).

#include <string.h>

char*
my_strcat(char* dest, char* src)
{
size_t sz_d = strlen(dest);
size_t sz_s = strlen(src) + 1; /* including '\0' */

memcpy(dest + sz_d, src, sz_s);
return dest;
}

/Niklas Norrthon
Dec 1 '05 #7
"Suman" <sk*****@gmail.com> wrote:
Richard Bos wrote:
"Suman" <sk*****@gmail.com> wrote:
Richard Bos wrote:
[ replying to the OP]
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 ? ;)


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:


Correct. To me, the strcat/memcat comparison was a poor one.


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.
void *memcat(void *dest, void *src, size_t at, size_t len)
{


I felt free to drop a:
return


Er... yes... sorry.
memcpy(dest+at, src, len);
}

Richard
Dec 1 '05 #8
In article <43****************@news.xs4all.nl>,
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
"yezi" <ye*****@hotmail.com> wrote:
is some mem cat function with is like "strcat" ?


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.)


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?
Dec 2 '05 #9
Rudolf <rt*****@bigfoot.com> writes:
In article <43****************@news.xs4all.nl>,
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
"yezi" <ye*****@hotmail.com> wrote:
> is some mem cat function with is like "strcat" ?


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.)


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?


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_Keith) ks***@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.
Dec 2 '05 #10
Keith Thompson said:
I haven't seen a rigorous description in this thread of just what
memcat() is supposed to do.


Presumably it would copy an arbitrary amount of memory to the end of another
arbitrary amount of memory.

#define mem_cat(dst, dstlen, src, srclen) \
memcpy((dst)+(dstlen), src, srclen)

I'm still trying to work out what all the fuss is about.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 2 '05 #11
Rudolf <rt*****@bigfoot.com> writes:
In article <43****************@news.xs4all.nl>,
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
"yezi" <ye*****@hotmail.com> wrote:
is some mem cat function with is like "strcat" ?


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.)


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?


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_buffer[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_buffer[i] = i;
free_store_ptr[i] = i;
}

static_ptr = static_buffer + BUF_SZ / 2;
automatic_ptr = automatic_buffer + BUF_SZ / 2;

/*
* 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
*/

memcat(static_ptr, automatic_buffer);
memcat(automatic_ptr, static_ptr);
memcat(free_store_ptr, automatic_ptr);

/* and so on, with every possible combination... */
return EXIT_FAILURE;
}

/Niklas Norrthon
Dec 2 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Shugong Wang | last post by:
getch() function is not a standard C/C++ function. Thought gcc provieds ncurses.h and a getch() function is realized, I still want to know how to realize a getch() function in standard c or c++.
3
by: Davide M3xican Coppola | last post by:
Hi, I would realize an interface (interface) for a pair of concrete classes (concrete & concrete2), but I have a problem because I have to access to a third class (element) and if I try to declare...
2
by: Lee Tow | last post by:
Hello all: I want to program using asp,that it can realize those complex functions like using vc before. If it can't realize these functions using asp, I want to know what languge I should use...
3
by: liu | last post by:
how c language to realize multiple ? can anyone tell me , thank you!
10
by: cai_rongxi | last post by:
I know how to write c code to realize atoi function, but I don't know how to do the opposit. Any sample code is appreciated.
2
by: °Ë´óɽÈË | last post by:
Hi, I want to realize moving contols in a panel, just like Form Desinger in VS.NET. How shall I realize it? Thank you. Jerry
2
by: °Ë´óɽÈË | last post by:
Hi, everybody Have you every realize the transparent control by C#? I want to customer some controls to support transparent, can you tell me how to realize me? Thank you! Jerry
4
by: Just Me | last post by:
I asked this in the VB NG and didn't get a response. I think this NG is no apt to know this kind of thing so I'm trying here. Trying to display the palette that is on the clipboard. I get a...
1
by: dd | last post by:
Hi, I'm porting some matlab programs to C and wonder wether someone has tried to use C to realize some data structure like cells in Matlab? I used ***p to realize cells that are like k*m*n,...
2
by: yxq | last post by:
How to realize the "MouseClickItemChecked" event of Listview? In VS2005, Listview has not the event "MouseClickItemChecked", it means that only mouse clicks the listview item and the checked state...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.