我的程序cc非常简单
#include <stdio.h>
int main()
{
int ret = 0;
return ret;
}
Run Code Online (Sandbox Code Playgroud)
当我使用“clang -fsanitize=address cc ”编译它并运行 a.out 时,会显示以下消息,但是程序运行成功
a.out(40820,0x106ffe600) malloc:由于无法预分配保留的虚拟机空间,纳米区域被放弃。
代码使用(Xcode 13)Apple clang版本13.0.0(clang-1300.0.29.3)编译目标:x86_64-apple-darwin21.1.0线程模型:posix
操作系统是 macOS Monterey (12.0.1)
任何更好地理解这一点的见解都会有所帮助
https://developer.gnome.org/glib/unstable/glib-GVariant.html#g-variant-ref-sink
我已经阅读了上面的glib手册,其中说:" GVariant使用浮动引用计数系统.所有函数的名称都以g_variant_new_返回浮动引用开头. "但浮动引用计数的实际描述在哪里?我找不到它的全面描述.
特别是我想要了解什么时候需要引用一个变体,何时不需要.例如:
GVariant *a_v = g_variant_new_boolean(TRUE);
GVariant *another_v = g_variant_new("v", a_v);
Run Code Online (Sandbox Code Playgroud)
a_v它,因为它被第二个消耗掉了g_variant_new.那是对的吗?another_v(假设从那时起another_v没有传递给任何其他内容)?代码:
int a;
cin>>a;
cout<<a<<endl;
Run Code Online (Sandbox Code Playgroud)
然后我使用g++ test.cpp并运行它。然后我向变量 a 输入字母“b”。输出为0。
但是,当我测试其他代码时:
cout<<int('b')<<endl; // output: 98
Run Code Online (Sandbox Code Playgroud)
为什么?有什么不同?
我在一本关于元编程的书中找到了这个工作代码 -
template<unsigned long N>
struct binary
{
static unsigned const value = binary<N/10>::value *2 + N%10;
};
template<>
struct binary<0>
{
static unsigned const value = 0;
};
int main()
{
unsigned x = binary<101010>::value;
cout << x;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是 - value分配的内存在哪里?是否在数据段上分配?
此外,该书还说这段代码导致了一系列模板实例化,它们以类似于递归的方式计算结果.这是否意味着每个模板实例化,unsigned在数据段上分配一个新的?
当我想在字符串中使用特殊字符时,我应该使用“\”:
std::string text("item \t new_item\n")
Run Code Online (Sandbox Code Playgroud)
但是,如果我打印这个字符串,显然它会打印:
item new_item
Run Code Online (Sandbox Code Playgroud)
有没有办法设置 std::cout 来打印所有特殊字符:
item \t new_item \n
Run Code Online (Sandbox Code Playgroud) “这是代码。'.'是什么意思 里面“opendir('.')”是否指定到当前目录?
#include <stdio.h>
#include <dirent.h>
int main(void)
{
struct dirent *de; // Pointer for directory entry
// opendir() returns a pointer of DIR type.
DIR *dr = opendir(".");
if (dr == NULL) // opendir returns NULL if couldn't open directory
{
printf("Could not open current directory" );
return 0;
}
while ((de = readdir(dr)) != NULL)
printf("%s\n", de->d_name);
closedir(dr);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我需要为指针打印一个名字.但它根本不起作用.控制台停止..
也许问题出在"find_young"函数上.我不知道是什么问题以及如何解决它.我能为正确的代码做些什么.
以下是我的代码.
========
typedef struct {
char *name;
int age;
} PERSON;
PERSON s[3] = { {"ACE", 25}, {"HEART" ,28}, {"CLOVER", 40} };
void find_young(PERSON **p) {
int i;
for (i = 0; i < 1; i++) {
if (s[i].age > s[i+1].age) {
p = &s[i+1];
}
}
};
void Ex1()
{
struct PERSON *p;
p = &s[0];
find_young(&p);
printf("youngest man is %s.", p->name);
}
Run Code Online (Sandbox Code Playgroud) 文件 'hello' 的内容是hello.
$ od -tx1 -tc hello
0000000 68 65 6c 6c 6f 0a
h e l l o \n
0000006
Run Code Online (Sandbox Code Playgroud)
下面是我对文件“hello”进行一些更改的代码。
static void *task();
int main(void)
{
int *p;
pthread_t Thread;
int fd = open("hello", O_RDWR);
if (fd < 0) {
perror("open hello");
exit(1);
}
p = mmap(NULL, 6, PROT_WRITE, MAP_PRIVATE, fd, 0);
if (p == MAP_FAILED) {
perror("mmap");
exit(1);
}
close(fd);
pthread_create(&Thread, NULL, &task, p)
printf("Help");
pthread_join(Thread, 0);
munmap(p, 6);
return 0;
}
static void …Run Code Online (Sandbox Code Playgroud) 我下面的代码由二叉树数据结构组成:
#include <bits/stdc++.h>
#define DEFAULT_NODE_VALUE 0
using namespace std;
template <class T>
class node{
public:
T val;
node* right = 0;
node* left = 0;
node(T a):val(a){}
};
template <class T>
class tree{
public:
node<T>* root = new node<T>(DEFAULT_NODE_VALUE);
tree(T inp_val){
root->val = inp_val;
}
void inorder_traverse(node<T>* temp){
if (!temp)
return;
inorder_traverse(temp->left);
cout << temp->val << " -> ";
inorder_traverse(temp->right);
}
void inorder_traverse(){
inorder_traverse(root);
}
};
int main()
{
tree<string> my_tree("mantap");
my_tree.root->right = new node<string>("ok");
my_tree.root->left = new node<string>("haha");
my_tree.inorder_traverse(); …Run Code Online (Sandbox Code Playgroud) 我正在尝试了解匿名命名空间。现在我的代码的要求是这样的:
#file.c
namespace X
{
void foo(){int x = bar(2);}
} //X
namespace
{
int bar(int x);
} //namespace
Run Code Online (Sandbox Code Playgroud)
或者
#file.c
namespace X
{
void foo(){int x = bar(2);}
namespace
{
int bar(int x);
} // namespace
} // X
Run Code Online (Sandbox Code Playgroud)
我试图向前声明 foo:
namespace X {
int bar(int);
...
Run Code Online (Sandbox Code Playgroud)
但到目前为止,这只会导致链接器错误。
这是代码 a1.cpp
#include <stdio.h>
void func(void)
{
printf("hello world\n");
}
int main()
{
func();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是代码a2.cpp
#include <stdio.h>
inline void func(void)
{
printf("hello world\n");
}
int main()
{
func();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它们仅与关键字“内联”不同
然后我将它们编译成汇编
g++ -S a1.cpp
g++ -S a2.cpp
Run Code Online (Sandbox Code Playgroud)
结果是:内联不起作用。函数调用保留在 main 中。
然后我用优化器编译它们
g++ -O2 -S a1.cpp
g++ -O2 -S a2.cpp
Run Code Online (Sandbox Code Playgroud)
结果是:inline 在 a2.s 中有效,但 a1.s 也替换了函数调用。似乎内联是自动添加的。
所以在编码时需要内联。