我很好奇,因为我在尝试直接访问内存后遇到内核恐慌(然后我发现了这些功能).
让我们说我的本地git log节目:
739b36d3a314483a2d4a14268612cd955c6af9fb a
...
c42fff47a257b72ab3fabaa0bcc2be9cd50d5c89 x
c4149ba120b30955a9285ed721b795cd2b82dd65 y
dce99bcc4b79622d2658208d2371ee490dff7d28 z
Run Code Online (Sandbox Code Playgroud)
我的遥控器git log显示:
c4149ba120b30955a9285ed721b795cd2b82dd65 y
dce99bcc4b79622d2658208d2371ee490dff7d28 z
Run Code Online (Sandbox Code Playgroud)
什么是最简单的方法(假设任意大量的本地提交):
527b5810cfd8f45f18ae807af1fe1e54a0312bce a ... x
c4149ba120b30955a9285ed721b795cd2b82dd65 y
dce99bcc4b79622d2658208d2371ee490dff7d28 z
Run Code Online (Sandbox Code Playgroud) 我们假设我在foo.h中定义了BAR.但是foo.h可能不存在.如果没有编译器向我抱怨,我如何包含它?
#include "foo.h"
#ifndef BAR
#define BAR 1
#endif
int main()
{
return BAR;
}
Run Code Online (Sandbox Code Playgroud)
因此,如果在foo.h中将BAR定义为2,则如果foo.h存在则程序将返回2,如果foo.h不存在则返回1.
例如,如果我想覆盖malloc(),那么最好的方法是什么?
目前我所知道的最简单的方法是:
malloc.h所
#include <stdlib.h>
#define malloc my_malloc
void* my_malloc (size_t size);
Run Code Online (Sandbox Code Playgroud)
foobar.c但是
#include "malloc.h"
void foobar(void)
{
void* leak = malloc(1024);
}
Run Code Online (Sandbox Code Playgroud)
这种方法的问题是我们现在必须使用"malloc.h"并且永远不能使用"stdlib.h".有没有解决的办法?我特别感兴趣的是导入第三方库而不修改它们,但是强迫它们调用我的自定义libc函数(比如malloc).
我知道如何执行此操作的唯一方法是将文件转换为C源文件,其中包含单个字节/ char数组,其中包含十六进制资源文件的内容.
有没有更好或更简单的方法来做到这一点?
所以这里有两个函数几乎做同样的事情。
如果您必须将两者都包含在您的项目中,您会如何命名每一个?
void strToLower1(char* str)
{
int len = strlen(str);
int i;
for (i=0; i<len; i++)
str[i] = tolower(str[i]);
}
char* strToLower2(const char* inputStr)
{
char* str = strdup(inputStr);
strToLower1(str);
return str; // must be freed
}
Run Code Online (Sandbox Code Playgroud)
编辑:我修改了上面的示例以确保代码正确性(sheesh)
char testStr[] = " trim this ";
char** pTestStr = &testStr;
trim(pTestStr);
int trim(char** pStr)
{
char* str = *pStr;
while(isspace(*str)) {
(*pStr)++;
str++;
}
if(*str == 0) {
return 0;
}
char *end = str + strlen(str) - 1;
while(end > str && isspace(*end))
end--;
*(end+1) = 0;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有这样的功能:
float_as_thousands_str_with_precision(value, precision)
Run Code Online (Sandbox Code Playgroud)
如果我像这样使用它:
float_as_thousands_str_with_precision(volts, 1)
float_as_thousands_str_with_precision(amps, 2)
float_as_thousands_str_with_precision(watts, 2)
Run Code Online (Sandbox Code Playgroud)
那些1/2的神奇数字吗?
c ×5
string ×2
embed ×1
gcc ×1
git ×1
git-rebase ×1
header-files ×1
include ×1
libc ×1
linux-kernel ×1
malloc ×1
overriding ×1
pointers ×1
squash ×1
trim ×1