我有一个缓冲地带,我做了很多事情。 我想确保我永远不会超越缓冲规模。
char buff[64];
strcpy(buff, "String 1");
strncat(buff, "String 2", sizeof(buff));
strncat(buff, "String 3", sizeof(buff));
我不想说一小.,而是想说什么。 我想确保我永远不会凌驾于缓冲之上。
我有一个缓冲地带,我做了很多事情。 我想确保我永远不会超越缓冲规模。
char buff[64];
strcpy(buff, "String 1");
strncat(buff, "String 2", sizeof(buff));
strncat(buff, "String 3", sizeof(buff));
我不想说一小.,而是想说什么。 我想确保我永远不会凌驾于缓冲之上。
考虑现有方位和无效方的大小
#define BUFFER_SIZE 64
char buff[BUFFER_SIZE];
//Use strncpy
strncpy(buff, "String 1", BUFFER_SIZE - 1);
buff[BUFFER_SIZE - 1] = ;
strncat(buff, "String 2", BUFFER_SIZE - strlen(buff) - 1);
strncat(buff, "String 3", BUFFER_SIZE - strlen(buff) - 1);
为什么不使用<代码>snprintf? 与<代码>strncat不同的是,它期望缓冲区的规模,但更重要的是,没有隐藏的O(n)。
强硬需要寻找每一座车上的无效者,每一次都要通过整个缓冲站找到最终结果。 每当扼杀时间越长,str就放慢了。 另一方面,印本可以跟踪最终情况。 页: 1
snprintf(buf, sizeof buf, "%s%s%s", "String1", "String2", "String3");
常常是一个更快和更可读的州。
您使用<代码>strncat在贵国或外国代码中的功能,实际上适合另一功能:strlcat
功能并非标准功能,而是供大众使用的<代码>strncat替换功能。 <代码>strlcat预期整个目的地缓冲总尺寸是其最后论点。(脚注
,而不是n
)。 <代码>strlcat
Meanwhile, strncat
expects the size of the remaining unused portion of the target buffer as its third argument. For this reason, your original code is incorrect.
I would suggest that instead of doing that horrible abuse of strncpy
and making explicit rescans with those strlen
calls (both issues present in Joe s answer), you either use an implementation-provided strlcat
or implement one yourself (if your implementation provides no strlcat
).
这是这样做的最佳方式。 <>代码/代码>,如果你不在当地分配数据,就只给你数据点的大小(在这种情况下,你确实在当地分配数据,但最好以这种方式这样做,如果重新计算该代码,它将发挥作用)。
#define MAXBUFFSIZE 64
char buff[MAXBUFFSIZE];
buff[0] = 0; // or some string
strncat(buff, "String x",MAXBUFFSIZE - strlen(buff) - 1);
Hogan已经回答了问题;然而,如果你担心在<条形码>中出现缓冲溢流,那么你也应同样担心在所有其他扼杀职能中的缓冲溢流。
使用<代码>strnlen(......)和strncpy(......)
,以确保你确实留在你的缓冲之内。 如果没有<代码>strnlen(......)功能,请予以书写。
此处使用<代码>memccpy,而不是strncat
-使之更安全、更快。 (《<>snprintf上述:
/**
* Returns the number of bytes copied (not including terminating ).
* Always terminates @buf with .
*/
int add_strings(char *buf, int len)
{
char *p = buf;
if (len <= 0)
return 0;
p[len - 1] = ; /* always terminate */
p = memccpy(buf, "String 1", , len - 1);
if (p == NULL)
return len - 1;
p = memccpy(p - 1, "String 2", , len - 1 - (p - buf));
if (p == NULL)
return len - 1;
p = memccpy(p - 1, "String 3", , len - 1 - (p - buf));
return (p == NULL ? len : p - buf) - 1;
}
I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...
The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?
I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...
I tried to print all the possible combination of members of several vectors. Why the function below doesn t return the string as I expected? #include <iostream> #include <vector> #...
I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...
I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??
Is there a PHP string function that transforms a multi-line string into a single-line string? I m getting some data back from an API that contains multiple lines. For example: <p>Some Data</...
I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...