English 中文(简体)
避免CRT
原标题:Avoiding the CRT
  • 时间:2012-05-23 17:08:02
  •  标签:
  • c++
  • c
  • stl
  • crt

写入 C++ 应用程序时, 我通常仅限于 C++ 特定语言功能。 在大多数情况下, 这意味着尽可能使用 STL 而不是 CRT 。

对我来说,STL比CRT更流畅、更能维持。

std::string str( "Hello" );
if( str == "Hello" ) { ... }

C-Run时间等值为:

char const* str = "Hello";
if( strcmp( str, "Hello" ) == 0 ) { ... }

个人来说,我发现前一个例子更容易看到。我更清楚地知道到底发生了什么。 当我写出第一个密码时,我的第一件事就是总是以最自然的方式写出代码。

我的团队对前一个例子的担忧是动态分配。 如果字符串是静态的OR已经分配到其他地方,他们认为造成分裂的潜在可能性或在这里的浪费性分配是没有道理的。 我反对这个论点的理由是首先以最自然的方式写出代码,然后在得到代码造成问题的证明后再回去修改代码。

另一个我不喜欢后一个例子的原因是它使用 C 图书馆。 通常情况下,我不惜一切代价避免它,只是因为它不是 C++,不易读,更易出错,而且更具有安全风险。

所以,我的问题是,我是否正确避免C运行时间?我是否真的关心这步的编码中的额外分配?我很难判断在这种情形下我是否正确。

最佳回答
问题回答

你在做C++或C?这些语言完全不同, 有着完全不同的思维方式。

如果 C++ :

std::string str( "Hello" );
if( str == "Hello" ) { ... }

如果C:

char const* str = "Hello";
if( strcmp( str, "Hello" ) == 0 ) { ... }

别把两者混为一谈

使用一个执行 < a href=" https://stackoverflow.com/ questions/10315041/missues/10315041/ meaning-of-acronym-sso- in- in-the-context- of-string" >Small String 优化 的汇编器,我得到这个结果:

main    PROC                        ; COMDAT

; 6    : {

$LN124:
  00000 48 83 ec 48       sub    rsp, 72            ; 00000048H

; 7    :    std::string str( "Hello" );

  00004 8b 05 00 00 00
        00                mov    eax, DWORD PTR ??_C@_05COLMCDPH@Hello?$AA@

; 8    : 
; 9    :    if( str == "Hello" )

  0000a 48 8d 15 00 00
        00 00            lea     rdx, OFFSET FLAT:??_C@_05COLMCDPH@Hello?$AA@
  00011 48 8d 4c 24 20   lea     rcx, QWORD PTR str$[rsp]
  00016 89 44 24 20      mov     DWORD PTR str$[rsp], eax
  0001a 0f b6 05 04 00
        00 00            movzx   eax, BYTE PTR ??_C@_05COLMCDPH@Hello?$AA@+4
  00021 41 b8 05 00 00
        00               mov     r8d, 5
  00027 c6 44 24 37 00   mov     BYTE PTR str$[rsp+23], 0
  0002c 48 c7 44 24 38
        05 00 00 00      mov     QWORD PTR str$[rsp+24], 5
  00035 c6 44 24 25 00   mov     BYTE PTR str$[rsp+5], 0
  0003a 88 44 24 24      mov     BYTE PTR str$[rsp+4], al
  0003e e8 00 00 00 00   call    memcmp
  00043 85 c0            test    eax, eax
  00045 75 1d            jne     SHORT $LN123@main

; 10   :    { printf("Yes!
"); }

  00047 48 8d 0d 00 00
        00 00            lea     rcx, OFFSET FLAT:??_C@_05IOIEDEHB@Yes?$CB?6?$AA@
  0004e e8 00 00 00 00   call    printf

; 11   : 
; 12   : }

连一个记忆都看不到!

标题下, std : 字符串 :: 操作者 : http:// operat或 = public to call strcmp 。 说实话, 如果对您来说不成体系并不是一个问题, 您喜欢利用 stl 更易读的语法, 请去使用 stl 。 如果性能是一个问题, 您会描述代码, 并且看到 std : 字符串内部数据是热点/ 瓶颈, 最优化 。 如果您不喜欢不一致的编码样式混合操作器 : () () 和 strcmp, 请写这样的东西 :

inline bool str_eq(const char* const lhs, const char* const rhs)
{
    return strcmp(lhs, rhs) == 0;
}
inline bool str_eq(const std::string& lhs, const char* const rhs)
{
    return str_eq(lhs.c_str(), rhs);
}
inline bool str_eq(const char* const lhs, const std::string& rhs)
{
    return str_eq(lhs, rhs.c_str());
}
inline bool str_eq(const std::string& lhs, const std::string& rhs)
{
    return lhs == rhs;
}

这不应该是宗教对话,两者都一样,如果你看到有人在写作

std::string str( "Hello" );
if( strcmp(str.c_str(), "Hello") == 0 ) { ... }

std::string str( "Hello" );
if( str.compare( "Hello" ) == 0) { ... }

then you can have a debate on mixing styles because both of those obviously would been clearer using operat或==

如果您的团队在 C++ 中编码, 您应该使用它提供的所有功能 。 当然, C++ 使用得当的注意记忆分配( 构件和破坏器) 和 更自然的语法( =, +) 。

您可能会认为 OOP 样式可能比较慢。 但是您必须首先测量瓶颈是字符串操作。 在大多数情况下, 它不太可能。 过早优化是所有邪恶的根源。 设计得当的 C++ 类不会丢失为手写 C 代码 。

回到你的问题, 最糟糕的变体可以混合图书馆。 您可以用 OOP 库取代 C 字符串, 但是仍然使用老式的 IO 常规和数学 。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签