为什么sizeof操作员返回的结构尺寸大于结构成员的总尺寸?
如果在C中有字符串,则可以在其中添加直接十六进制代码.
char str[] = "abcde"; // 'a', 'b', 'c', 'd', 'e', 0x00
char str2[] = "abc\x12\x34"; // 'a', 'b', 'c', 0x12, 0x34, 0x00
Run Code Online (Sandbox Code Playgroud)
两个示例在内存中都有6个字节.现在,如果要[a-fA-F0-9]在十六进制输入后添加值,则存在问题.
//I want: 'a', 'b', 'c', 0x12, 'e', 0x00
//Error, hex is too big because last e is treated as part of hex thus becoming 0x12e
char problem[] = "abc\x12e";
Run Code Online (Sandbox Code Playgroud)
可能的解决方案是在定义后替换.
//This will work, bad idea
char solution[6] = "abcde";
solution[3] = 0x12;
Run Code Online (Sandbox Code Playgroud)
这可以工作,但如果你把它作为,它将失败const.
//This will not work
const char solution[6] = "abcde"; …Run Code Online (Sandbox Code Playgroud) 考虑一个你有一些代码的库.例如,让我们做一些X和Y点操作.
然后你构建你的库,你不希望允许用户访问你的struct变量,到目前为止我使用这种方法,它似乎工作正常.
lib.h:
#ifndef __LIB_H
#define __LIB_H
#ifdef __LIB_INTERNAL
//Structure for single point
typedef struct {
int x, y;
} Point;
//Casted pointer
#define _P(in) ((Point *)(in))
#endif
//Define pointer for public use as void pointer
typedef void* Point_p;
//Create point
Point_p createPoint(int x, int y);
#endif
Run Code Online (Sandbox Code Playgroud)
lib.c:
//Define LIB_INTERNAL to allow visible access
#define __LIB_INTERNAL
#include "lib.h"
#include "stdlib.h"
Point_p createPoint(int x, int y) {
Point_p p = malloc(sizeof(Point));
_P(p)->x = x; //_P is visible in this …Run Code Online (Sandbox Code Playgroud) 虽然没有官方支持的方法来做到这一点.有没有办法(在现代系统上)检测指针是否来自堆栈(例如调用者的堆栈).
即使这不能作为实际代码逻辑的一部分工作,它也可以帮助避免可以检测到它的配置中的错误,例如:
void my_function(void *arg) {
/* Only some configurations can do this (depending on compiler & arch). */
#if THE_MOONS_ALIGN
assert(not_stack_memory(arg));
#endif
/* ... actual logic ... */
}
Run Code Online (Sandbox Code Playgroud) 如何使用单个命令获取和合并主存储库并对所有子模块执行相同操作?
我有一个包含子模块B的存储库A。子模块B经常更新,更改并不总是最新的。
我可以通过执行以下操作来更新和合并新更改
cd A/B <-- enter submodule directory
git pull <-- run pull to fetch and !!merge!! submodule changes
Run Code Online (Sandbox Code Playgroud)
我想要做的是运行单个命令来 pull&merge 主存储库,并对所有子模块执行相同的操作。
cd A <-- Now enter to parent directory
git pull --recurse-submodules <-- Fetch&merge changes on main repository
<-- and pull changes on all submodules
Run Code Online (Sandbox Code Playgroud)
实际发生的是:
目前的解决方案是:
cd A
git pull --recurse-submodules
cd B
git merge <-- Now merge fetched changes …Run Code Online (Sandbox Code Playgroud) 有人可以解释以下代码中发生的事情吗?(摘自GeeksForGeeks)
int main(){
int a = 10;
++a = 20; // works
printf("a = %d", a);
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
执行语句++ a = 20时究竟发生了什么?另外,请澄清为什么此代码执行失败?
int main(){
int a = 10;
a++ = 20; // error
printf("a = %d", a);
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是main.c:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int size = atoi(argv[1]);
int int_array[10][10];
int *before = NULL;
int *val[size];
int *after = NULL;
printf("before:%p, after:%p, sizeof val:%lu\n", before, after, sizeof val);
int i = 0;
/*for (; i < 10; i++) {
val[i] = int_array[i];
}*/
val[0] = int_array[0];
val[1] = int_array[3];
printf("int_array:%p, int_array[0]:%p, int_array[1]:%p, int_array[2]:%p, int_array[3]:%p\n", int_array, int_array[0], int_array[1], int_array[2], int_array[3]);
printf("val:%p, val[0]:%p, val[1]:%p, val[2]:%p, val[3]:%p\n", val, val[0], val[1], val[2], val[3]);
printf("before:%p, after:%p\n", before, after); …Run Code Online (Sandbox Code Playgroud)