首页 \ 问答 \ 调用ToString()陷阱(Calling ToString() gotchas)

调用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
更新时间:2023-05-19 14:05

最满意答案

这取决于你想用它做什么。

既然你在内存中有字符串,你可以通过小心你传递的参数来安全地避免缓冲区溢出。 例如,如果你正在扫描一个字符串,使用"%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.

相关问答

更多
  • %s格式说明符将停止在第一个空白字符处理。 如果是: cat / etc / passwd | cut -f1 d:| 分类 这将是第一个%s将结束的cat之后的空间。 格式说明符然后期望| 不存在,并且不填充cm2和cm3 。 您可以使用扫描集来实现此目的: if (3 == sscanf(var_text, "%99[^|]| %99[^|]| %99s", cm1, cm2, cm3)) { } 注意使用width说明符来防止缓冲区溢出并检查sscanf()的返回值以确保填充所有目标变量。 格式说明 ...
  • 的? 和=不会被消耗,所以在格式说明符中包含它们: 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; ...
  • 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 ...
  • 格式字符串解析如下: :%[^ ] %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 = " ...
  • 如果像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]); 正则表达 ...
  • 你的问题几乎不是主题,因为它需要基于意见的答案。 了解一种方法与另一种方法的比较速度的唯一方法是尝试两者并测量实际数据的生成可执行文件的性能。 凭借当今常规PC中的计算能力,需要一个非常大的文件来衡量实际的性能差异。 所以继续实施你的想法。 您似乎对潜在的性能瓶颈有了很好的理解,将这些想法转化为实际的C代码。 为这个问题提供2个不同但正确的程序以及性能分析应该会得到A +。 我作为雇主在测试中重视这种方法。 PS:恕我直言,大部分时间都将用于从文件系统获取数据。 如果文件大于可用内存,那应该是你的瓶颈。 如 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。