memset()声明返回void*,它始终与传递给函数的地址相同.
返回值的用途是什么?为什么不回来void?
当我解决以下示例算法问题时,我很好奇理解字符串比较在python中如何工作的内部:
给定两个字符串,返回最长公共前缀的长度
我的直觉告诉我,最佳解决方案是在两个单词的开头用一个光标开始并向前迭代,直到前缀不再匹配.就像是
def charByChar(smaller, bigger):
assert len(smaller) <= len(bigger)
for p in range(len(smaller)):
if smaller[p] != bigger[p]:
return p
return len(smaller)
Run Code Online (Sandbox Code Playgroud)
为了简化代码,该函数假定第一个字符串的长度smaller始终小于或等于第二个字符串的长度bigger.
另一种方法是将两个字符串平分以创建两个前缀子字符串.如果前缀相等,我们知道公共前缀点至少与中点一样长.否则,公共前缀点至少不大于中点.然后我们可以递归以找到前缀长度.
阿卡二进制搜索.
def binarySearch(smaller, bigger):
assert len(smaller) <= len(bigger)
lo = 0
hi = len(smaller)
# binary search for prefix
while lo < hi:
# +1 for even lengths
mid = ((hi - lo + 1) // 2) + lo
if smaller[:mid] == bigger[:mid]:
# prefixes equal
lo = mid
else: …Run Code Online (Sandbox Code Playgroud) 它总是让我感到奇怪,C函数"fopen"将"const char*"作为第二个参数.如果在stdio.h中定义了位掩码,比如"IO_READ"等,我认为读取代码和实现库代码会更容易,所以你可以这样做:
FILE* myFile = fopen("file.txt", IO_READ | IO_WRITE);
Run Code Online (Sandbox Code Playgroud)
它的实际存在方式是否存在程序化原因,还是仅仅是历史性的?(即"这就是它的方式.")
我们的内部程序是用 C 编写的,并广泛使用了snprintf()许多部分,我注意到在使用性能记录/报告进行调试期间,它在以下方面花费了大量时间:
\xe2\x94\x82 _IO_vfprintf_internal(): \xe2\x96\x92\n \xe2\x94\x82 mov -0x510(%rbp),%rdx \xe2\x96\x92\n \xe2\x94\x82 mov %r12,%rsi \xe2\x96\x92\n \xe2\x94\x82 mov %r15,%rdi \xe2\x96\x92\n \xe2\x94\x82 \xe2\x86\x92 callq *0x38(%rax) \xe2\x96\x92\n \xe2\x94\x82 cmp %rax,-0x510(%rbp) \xe2\x96\x92\n \xe2\x94\x82 mov -0x530(%rbp),%r9 \xe2\x96\x92\n \xe2\x94\x82 \xe2\x86\x91 jne 91a \xe2\x96\x92\n \xe2\x94\x82 mov -0x4d0(%rbp),%esi \xe2\x96\x92\n \xe2\x94\x82 mov -0x540(%rbp),%ecx \xe2\x96\x92\n \xe2\x94\x82 mov $0x7fffffff,%eax \xe2\x96\x92\n \xe2\x94\x82 sub %esi,%eax \xe2\x96\x92\n \xe2\x94\x82 add %esi,%ecx \xe2\x96\x92\n \xe2\x94\x82 cltq \xe2\x96\x92\n \xe2\x94\x82 cmp %rax,-0x510(%rbp) \xe2\x96\x92\n \xe2\x94\x82 \xe2\x86\x91 jbe 252b \xe2\x96\x92\n \xe2\x94\x82 \xe2\x86\x91 jmpq 28f0 \xe2\x96\x92\n \xe2\x94\x824a70: xor %eax,%eax \xe2\x96\x92\n …Run Code Online (Sandbox Code Playgroud) 我应该如何编写一个高效的异常类来显示可以通过在运行时修复源代码错误来防止的错误?
这就是我选择的原因std::invalid_argument。
我的异常类(显然不起作用):
class Foo_Exception : public std::invalid_argument
{
private:
std::string Exception_Msg;
public:
explicit Foo_Exception( const std::string& what_arg );
virtual const char* what( ) const throw( );
};
explicit Foo_Exception::Foo_Exception( const std::string& what_arg ) // how should I write this function???
{
Exception_Msg.reserve( 130000 );
Exception_Msg = "A string literal to be appended, ";
Exception_Msg += std::to_string( /* a constexpr int */ );
Exception_Msg += /* a char from a const unordered_set<char> */;
}
const char* Foo_Exception::what( ) …Run Code Online (Sandbox Code Playgroud) 我在C中编写一些代码:
int main(){
char guess[15];
guess = helloValidation(*guess);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的功能是:
char[] helloValidation(char* des) {
do {
printf("Type 'hello' : ");
scanf("%s", &des);
}while (strcmp(des, "hello") != 0);
return des
}
Run Code Online (Sandbox Code Playgroud)
但它给了我这个错误:
incompatible types in assignment
Run Code Online (Sandbox Code Playgroud) 我尝试打印出strcpy() 的返回值,它给了我一个“访问冲突读取位置”异常。
char ind[15];
printf("%s\n", strcpy(ind, "source text"));
Run Code Online (Sandbox Code Playgroud)
或使用调试语句:
char* ins = "source text";
char* dst;
dst = strcpy(ind, ins);
printf("dst = %s", dst);
Run Code Online (Sandbox Code Playgroud)
Visual Studio 显示 ind = 0x00000024ec71f448“源文本”,而 dst = 0xffffffffec71f448<读取字符串字符时出错。> <无法读取内存> 。
dst不应该= ind吗?指向同一个地址?
Great answers, all!
如果没有 <string.h>,当我将鼠标悬停在 strcpy 上时,它默认为“int strcpy()”。包含<string.h>后,它显示“char *strcpy()”。
关于地址值,我的电脑运行的是 64 位 Windows 10。由于“int”在大多数 64 位平台上通常为 32 位,因此 dst 的上限为 32 位,并具有伪造的高 32 位。
一个问题是,为了使用 Visual Studio 进行构建,您必须在所有 #include .h 之上添加#define _CRT_SECURE_NO_WARNINGS以避免编译器错误。原因是VS建议使用errno_t strcpy_s(...)而不是strcpy()。但如果成功,strcpy_s() 将返回值。这使得我们的 dst …