strncpy并不那么容易使用 [英] strncpy not that easy to use

查看:84
本文介绍了strncpy并不那么容易使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何(或者应该用户使用)strncpy()为目标字符串分配足够的内存空间

s1?


我想我是在使用strncpy时遇到麻烦,因为它似乎在给我

错误。


同样,我也不知道n的价值程序运行,所以它需要动态分配



然后我再次硬编码n为数字3只是为了看看是否有效。

470 char *

471 strncpy(char * s1,const char * s2,size_t n)

472 {

473 char * os1 = s1;

474

475 n ++;

476 while(--n!= 0&&( * s1 ++ = * s2 ++)!=''\ 0'')

477;

478 if(n!= 0)

479 while(--n!= 0)

480 * s1 ++ =''\''';

481 return(os1);

482}

How does( or should user use) strncpy() to allocate enough memory space for
the destination string, s1?

I guess I am having trouble using strncpy as it seems to be giving me
errors.

And just as well, I don''t know the value of n until the program runs, so it
needs to be dynamically allocated.

Then again I hard coded n to be number like3 just to even see if that works.
470 char *
471 strncpy(char *s1, const char *s2, size_t n)
472 {
473 char *os1 = s1;
474
475 n++;
476 while (--n != 0 && (*s1++ = *s2++) != ''\0'')
477 ;
478 if (n != 0)
479 while (--n != 0)
480 *s1++ = ''\0'';
481 return (os1);
482 }

推荐答案

smnoff说:
smnoff said:

如何(或应该用户使用)strncpy()为目标字符串s1分配足够的内存空间


How does( or should user use) strncpy() to allocate enough memory space
for the destination string, s1?



strncpy没有分配任何空间。它只是复制和垫。如果你是

(正确地)担心不会溢出你的目标缓冲区,strncpy就不足以支付
了。您必须知道从源

字符串中需要多少字节的数据,并提供至少那么多字节(如果相关,则提供一个空的

终止符,它几乎总是用于strncpy用作

目的地。如果你真正的要求是复制整个字符串,那几乎总是那么多,那么你也可以使用strcpy。

strncpy doesn''t allocate any space. It just copies and pads. If you are
(rightly) concerned not to overflow your target buffer, strncpy is not
sufficient. You must know how many bytes of data you need from the source
string, and provide at least that many bytes (plus one for a null
terminator, if relevant, which it nearly always is) for strncpy to use as a
destination. If your real requirement is to copy the entire string, which
it nearly always is, at that point you might just as well use strcpy.


我想我在使用strncpy时遇到了麻烦,因为它似乎在给我

错误。
I guess I am having trouble using strncpy as it seems to be giving me
errors.



/你/给/它/错误的可能性更大。如果你真的需要使用strncpy,那几乎不需要它,我建议你

告诉我们你的调用代码,而不是你的实现尝试。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)

It is far more likely that /you/ are giving /it/ errors. If it really is
necessary for you to use strncpy, which it hardly ever is, I suggest you
show us your calling code, rather than your attempt at an implementation.

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


" smnoff" < 34 ************** @ hotmail.comwrites:
"smnoff" <34**************@hotmail.comwrites:

strncpy()如何(或应该使用)strncpy()分配足够的内存空间

目标字符串,s1?


我想我在使用strncpy时遇到了麻烦,因为它似乎在给我

错误。


同样,在程序运行之前我不知道n的值,所以

需要动态分配。


然后我再次硬编码n为数字3只是为了看看是否有效。


470 char *

471 strncpy(char * s1,const char * s2,size_t n)

472 {

473 char * os1 = s1;

474

475 n ++;

476 while(--n!= 0&&(* s1 ++ = * s2 ++)!=''\\ \\ 0'')

477;

478 if(n!= 0)

479 while(--n!= 0)

480 * s1 ++ =''\''';

481 return(os1);

482}
How does( or should user use) strncpy() to allocate enough memory space for
the destination string, s1?

I guess I am having trouble using strncpy as it seems to be giving me
errors.

And just as well, I don''t know the value of n until the program runs, so it
needs to be dynamically allocated.

Then again I hard coded n to be number like3 just to even see if that works.
470 char *
471 strncpy(char *s1, const char *s2, size_t n)
472 {
473 char *os1 = s1;
474
475 n++;
476 while (--n != 0 && (*s1++ = *s2++) != ''\0'')
477 ;
478 if (n != 0)
479 while (--n != 0)
480 *s1++ = ''\0'';
481 return (os1);
482 }



你已经引用了strncpy()的特定实现,但是如果你是

要使用它你应该根据记录的语义

函数,而不是如何在一个特定的

实现中编写。


你说这似乎是你的错误,但是你没有告诉我们

的错误,所以我们做的不多帮助。


一般情况下,strncpy()不太可能是您想要使用的。

如果目标数组也是如此小,它可能不会以''\ 0''

字符终止(即,它可能不是字符串);如果它大于它需要的价格,那么strncpy()会用额外的''\ 0''字符填充它,这通常是浪费的。 br />

在*

之后使用strcpy()或者memcpy()*通常会更好,这样可以确保目标数组足够大。


如果您有具体问题,请发布您的实际代码并告诉我们

它是如何失败的。


- -

Keith Thompson(The_Other_Keith) ks***@mib.org < http:// www .ghoti.net / ~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

You''ve quoted a particular implementation of strncpy(), but if you''re
going to use it you should base your usage on the documented semantics
of the function, not on how it happens to be written in one particular
implementation.

You say it seems to be givin you errors, but you don''t tell us what
kind of errors, so there''s not much we can do to help.

In general, though, strncpy() isn''t likely to be what you want to use.
If the target array is too small, it may not be terminated with a ''\0''
character (i.e., it may not be a string); if it''s larger than it needs
to be, strncpy() pads it with additional ''\0'' characters, which is
usually wasteful.

It''s usually better to use strcpy(), or perhaps memcpy() *after*
making absolutely sure that the target array is big enough.

If you have specific questions, post your actual code and tell us how
it fails.

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


此代码是在Visual Studio中创建的DLL的一部分,因此VS

不会总是给我错误信息。这个DLL完全由Visual Studio外部程序调用。


实际上,我真的想知道如何让这个dll代码完全运行在

Visual Studio中我可以让调试器完全工作,因为这是Visual

Studio DLL项目。


我会必须创建一个完全独立的项目来完成所有这些调试

才能工作或者我会得到同样的未知错误消息我一直在使用外部程序时获得




" Keith Thompson" < ks *** @ mib.org写信息

news:ln ************ @ nuthaus.mib.org ...
This code is part of a DLL that was created in Visual Studio and hence VS
doesn''t always give me the error message. This DLL is being called by an
external program totally outside of Visual Studio.

Actually, I really want to know how to make this dll code run entirely in
Visual Studio so I can get the debugger to work fully as this is Visual
Studio DLL project.

Would I have to create a totally separate project to get all this debugging
to work OR would I just get same unknown error messages I have been getting
when I use the external program?


"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...

" smnoff" < 34 ************** @ hotmail.comwrites:
"smnoff" <34**************@hotmail.comwrites:

> strncpy(或应该如何使用)strncpy( )为目标字符串s1分配足够的内存空间,s1?

我想我在使用strncpy时遇到了麻烦,因为它似乎给了我
错误。

同样,在程序运行之前我不知道n的值,所以
需要动态分配。
是否有效。

470 char *
471 strncpy(char * s1,const char * s2 ,size_t n)
472 {
473 char * os1 = s1;
474
475 n ++;
476 while(--n!= 0&& (* s1 ++ = * s2 ++)!=''\ 0'')
477;
478 if(n!= 0)
479 while(--n!= 0)
480 * s1 ++ =''\''';
481 return(os1);
482}
>How does( or should user use) strncpy() to allocate enough memory space
for
the destination string, s1?

I guess I am having trouble using strncpy as it seems to be giving me
errors.

And just as well, I don''t know the value of n until the program runs, so
it
needs to be dynamically allocated.

Then again I hard coded n to be number like3 just to even see if that
works.
470 char *
471 strncpy(char *s1, const char *s2, size_t n)
472 {
473 char *os1 = s1;
474
475 n++;
476 while (--n != 0 && (*s1++ = *s2++) != ''\0'')
477 ;
478 if (n != 0)
479 while (--n != 0)
480 *s1++ = ''\0'';
481 return (os1);
482 }



你''引用了一个特定的实现strncpy(),但如果你要使用它,你应该根据记录的语义

函数,而不是它是如何发生的写在一个特定的

实现中。


你说这似乎是你的错误,但你没有告诉我们什么

有点错误,所以我们无能为力。


一般来说,strncpy()不太可能是你的想要使用。

如果目标数组太小,它可能不会以''\ 0''

字符终止(也就是说,它可能不是一个字符串);如果它大于它需要的价格,那么strncpy()会用额外的''\ 0''字符填充它,这通常是浪费的。 br />

在*

之后使用strcpy()或者memcpy()*通常会更好,这样可以确保目标数组足够大。


如果您有具体问题,请发布您的实际代码并告诉我们

它是如何失败的。


- -

Keith Thompson(The_Other_Keith) ks***@mib.org

< http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *>

< http://users.sdsc .edu / ~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


You''ve quoted a particular implementation of strncpy(), but if you''re
going to use it you should base your usage on the documented semantics
of the function, not on how it happens to be written in one particular
implementation.

You say it seems to be givin you errors, but you don''t tell us what
kind of errors, so there''s not much we can do to help.

In general, though, strncpy() isn''t likely to be what you want to use.
If the target array is too small, it may not be terminated with a ''\0''
character (i.e., it may not be a string); if it''s larger than it needs
to be, strncpy() pads it with additional ''\0'' characters, which is
usually wasteful.

It''s usually better to use strcpy(), or perhaps memcpy() *after*
making absolutely sure that the target array is big enough.

If you have specific questions, post your actual code and tell us how
it fails.

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



这篇关于strncpy并不那么容易使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆