调用ToString()陷阱(Calling ToString() gotchas)
我已经读过ToString()使用了反射(尽管它应该在它所调用的对象周围加上引号,所以我不知道它在哪里/为什么会使用反射)。 有没有这方面的证据? 是否有任何性能损失调用.ToString()(也许我应该调用Convert.ToString()?)?
I have read that ToString() uses reflection (although it should just put quotation marks around the object it is called on so I don't know where/why it may use reflection). Is there any proof of this? And is there any performance penalty of calling .ToString() (maybe I should call Convert.ToString()?)?
原文:https://stackoverflow.com/questions/2185383
最满意答案
这取决于你想用它做什么。
既然你在内存中有字符串,你可以通过小心你传递的参数来安全地避免缓冲区溢出。 例如,如果你正在扫描一个字符串,使用
"%s"
,只需使用足够大的目标缓冲区来保存可能存在的最大字符串(并确保源缓冲区是一个有效的字符串,它是'\0'
终止)。所有
*scanf()
函数的一个危险是,如果您正在扫描一个数值,并且输入值太大而无法在目标类型中表示,则行为是未定义的。 例如,这个程序:#include <stdio.h> int main(void) { const char *s = "999999999999999999999999999"; int n; sscanf(s, "%d", &n); printf("n = %d\n", n); return 0; }
有未定义的行为(除非
INT_MAX
真的很大)。您可以使用
strto*()
函数安全地扫描数值:strtol()
,strtoll()
,strtoul()
,strtoull()
,strtof()
,strtod()
,strtold()
。 他们对错误的行为有点棘手,但至少它是明确的。It depends on what you want to do with it.
Since you have the string in memory, you can safely avoid buffer overflows by being careful about the arguments you pass. For example, if you're scanning into a string, using
"%s"
, just use a target buffer big enough to hold the biggest string that could possibly be there (and make sure that the source buffer is a valid string, i.e., that it's'\0'
-terminated).One danger of all the
*scanf()
functions is that if you're scanning a numeric value, and the input value is too big to be represented in the target type, the behavior is undefined. For example, this program:#include <stdio.h> int main(void) { const char *s = "999999999999999999999999999"; int n; sscanf(s, "%d", &n); printf("n = %d\n", n); return 0; }
has undefined behavior (unless
INT_MAX
is really really big).You can safely scan numeric values using the
strto*()
functions:strtol()
,strtoll()
,strtoul()
,strtoull()
,strtof()
,strtod()
,strtold()
. Their behavior on errors is a bit tricky, but at least it's well defined.
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
sscanf与管道C.(sscanf with pipes C)[2023-04-30]
%s格式说明符将停止在第一个空白字符处理。 如果是: cat / etc / passwd | cut -f1 d:| 分类 这将是第一个%s将结束的cat之后的空间。 格式说明符然后期望| 不存在,并且不填充cm2和cm3 。 您可以使用扫描集来实现此目的: if (3 == sscanf(var_text, "%99[^|]| %99[^|]| %99s", cm1, cm2, cm3)) { } 注意使用width说明符来防止缓冲区溢出并检查sscanf()的返回值以确保填充所有目标变量。 格式说明 ... -
C sscanf和字符串格式(C sscanf and string format)[2022-03-26]
的? 和=不会被消耗,所以在格式说明符中包含它们: sscanf(str, "%*[^?]?%[^=]=%s", key, buf); 请参阅http://ideone.com/YoRMh3上的演示。 要防止缓冲区溢出,请指定每个说明符可以读取的最大字符数,比允许空终止的目标数组少一个,并通过检查sscanf()的返回值来确保填充这两个数组: if (2 == sscanf(str, "%*[^?]?%19[^=]=%99s", key, buf)) { printf("<%s>\n", key) ... -
对于初学者,您应该使用不同的变量来存储生成的解析字符串。 void test(char *s) { int i; string s1; istringstream iss(s); iss >> s1 >> i; } For starters, you should use a different variable to store the resulting parsed string. void test(char *s) { int i; string s1; ...
-
相当于Qt sscanf(Qt sscanf equivalent)[2024-01-18]
QTextStream提供了标准库流的等价物。 不要忘记,文本流应该在字符串之前销毁。 QString mystring = "67 49 213 59"; QTextStream myteststream(&mystring); int a = 0, b = 0, c = 0, d = 0; myteststream >> a >> b >> c >> d; QTextStream provides the equivalent of the standard library's streams. Do ... -
C sscanf需要帮助理解(C sscanf Need help understanding)[2020-01-22]
格式字符串解析如下: :%[^ ] %s %s %[^\n]%*[\n\r]%n ^ ^ ^ ^ ^ ^ ^ ^ | | | | | | | | | | | | | | | +-- the number of characters read so far | | | | | | +------ \n or \r | | | | | +---------- read and igno ... -
这取决于你想用它做什么。 既然你在内存中有字符串,你可以通过小心你传递的参数来安全地避免缓冲区溢出。 例如,如果你正在扫描一个字符串,使用"%s" ,只需使用足够大的目标缓冲区来保存可能存在的最大字符串(并确保源缓冲区是一个有效的字符串,它是'\0'终止)。 所有*scanf()函数的一个危险是,如果您正在扫描一个数值,并且输入值太大而无法在目标类型中表示,则行为是未定义的。 例如,这个程序: #include
int main(void) { const char *s = " ... -
我如何在c#中执行sscanf(how do I do sscanf in c#)[2023-05-05]
如果像scannf一样,您愿意假设用户将提供完全正确的数据,那么您可以执行以下操作。 string astring = ...; string[] values = astring.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries); int a = Int32.Parse(values[0]); int b = Int32.Parse(values[1]); int c = Int32.Parse(values[2]); 正则表达 ... -
fscanf和sscanf的速度(The speed of fscanf and sscanf)[2020-04-05]
你的问题几乎不是主题,因为它需要基于意见的答案。 了解一种方法与另一种方法的比较速度的唯一方法是尝试两者并测量实际数据的生成可执行文件的性能。 凭借当今常规PC中的计算能力,需要一个非常大的文件来衡量实际的性能差异。 所以继续实施你的想法。 您似乎对潜在的性能瓶颈有了很好的理解,将这些想法转化为实际的C代码。 为这个问题提供2个不同但正确的程序以及性能分析应该会得到A +。 我作为雇主在测试中重视这种方法。 PS:恕我直言,大部分时间都将用于从文件系统获取数据。 如果文件大于可用内存,那应该是你的瓶颈。 如 ...