我正在阅读"C++ primer plus".在第9章中,它讨论了处理const修饰符时C++和C之间的区别:
"在C++(但不是C)中,const修饰符稍微改变了默认存储类.默认情况下,全局变量具有外部链接,而const全局变量默认具有内部链接.
...
如果全局const声明具有外部链接作为常规变量do,则这将是一个错误,因为您只能在一个文件中定义全局变量.也就是说,只有一个文件可以包含程序声明,其他文件必须使用extern关键字提供引用声明."
我尝试使用以下程序测试此声明:
file.h:
using namespace std;
const char *constant = "Magic";
Run Code Online (Sandbox Code Playgroud)
file1.cpp
#include <iostream>
#include "file.h"
extern void file2();
int main(){
cout << "constant = " << constant << endl;
file2();
}
Run Code Online (Sandbox Code Playgroud)
file2.cpp
#include <iostream>
#include "file.h"
void file2(){
cout << "file2 constant = " << constant << endl;
}
Run Code Online (Sandbox Code Playgroud)
Makefile文件:
CFLAGS = -Wall -g
INCLUDE = file.h
src = file2.cpp file1.cpp
all: $(src) $(INCLUDE)
g++ $(CFLAGS) -o file $(src)
clean:
rm -f file
Run Code Online (Sandbox Code Playgroud)
当我做,我收到以下错误信息: …
我用root权限运行程序,但它一直在抱怨mmap无法分配内存.代码段如下:
#define PROTECTION (PROT_READ | PROT_WRITE)
#define LENGTH (4*1024)
#ifndef MAP_HUGETLB
#define MAP_HUGETLB 0x40000
#endif
#define ADDR (void *) (0x0UL)
#define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB)
int main (int argc, char *argv[]){
...
// allocate a buffer with the same size as the LLC using huge pages
buf = mmap(ADDR, LENGTH, PROTECTION, FLAGS, 0, 0);
if (buf == MAP_FAILED) {
perror("mmap");
exit(1);
}
...}
Run Code Online (Sandbox Code Playgroud)
硬件:我有8G内存.处理器是ivybridge
Uname输出:
Linux mymachine 3.13.0-43-generic #72-Ubuntu SMP Mon Dec 8 19:35:06 UTC 2014 x86_64 x86_64 …Run Code Online (Sandbox Code Playgroud) 这是一个protobuf消息定义:
message People {
enum PeopleName {
Alice = 100;
Bob = 101;
Cathy = 102;
}
optional PeopleName name = 1;
}
Run Code Online (Sandbox Code Playgroud)
我想根据我创建的一些字符串填充名称字段.例如在golang:
str := "Cathy"
Run Code Online (Sandbox Code Playgroud)
如何填充protobuf消息中的"名称"?
这是我的场景:
这个问题是关于骗子和真相讲述者的问题,但它在确定复杂系统的哪些组件是好的(正常运行)和哪些是错误方面具有实际应用.假设我们有一个n人社区,我们知道一个整数t <n/2,它具有n个人中大多数人都是骗子的属性.这并不是说实际上有说谎者,而只是说最多只有说谎者.
我认为真相讲述者总是真实和正确,而骗子可能会说出错误答案或正确答案.
我们将通过连续挑选一对人来识别社区中的骗子,(X,Y)说,并问X:Y是骗子吗?回答是"是"或"否";
找到所有骗子的最佳算法(最小步数)是多少?
我对有关有限现场操作的SAGE文档感到非常沮丧。我想做的是以下几点:
在具有不可约多项式x ^ 8 + x ^ 4 + x ^ 3 + x + 1的GF(2 ^ 8)中,我想找到元素x ^ 8 + 1的逆。如何在SAGE中做到这一点?
我想将参数字符串复制到程序中的数组中.所以我写了下面的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]){
// go through each string in argv
int i = 0;
while(i < argc){
printf("arg %d: %s\n", i, argv[i]);
i++;
}
//let's make our own array of strings
char *states[] = {"California", "Oregon", "Washington", "Texas"};
int num_states = 4;
i = 0; //watch for this
while(i < num_states) {
printf("state %d: %s\n", i, states[i]);
i++;
}
//copy argv into strings
char **dst;
dst = (char**)malloc(sizeof(char*)*argc);
for(i = 0; …Run Code Online (Sandbox Code Playgroud) 我想将我的数据包发送到UDP dst端口号并通过eth0接口发送(如果scapy直接处理我的layer2,那么eth0不需要作为参数提供)
我编写了一个程序集,该程序集与:
$as --32 -o hello.o hello.s
Run Code Online (Sandbox Code Playgroud)
然后,我尝试生成具有以下内容的可执行文件:
$ld -o hello hello.o
Run Code Online (Sandbox Code Playgroud)
它给我一个错误:
ld: i386 architecture of input file `ConditionalBranching.o' is incompatible with i386:x86-64 output
Run Code Online (Sandbox Code Playgroud)
我尝试使用标志-m32或--32,但ld不要使用它们。我无法通过阅读ld的手册页找到解决方案。如何从32位共享库生成32位二进制文件?
我有一个如下数组,
unsigned char A[16]
我使用此数组来表示128位硬件寄存器.现在我想用这个长寄存器实现一个线性反馈移位寄存器(LFSR,Fibonacci实现).连接到该LFSR的反馈xnor门的多项式(或抽头)是[128,29,27,2,1].
可以从维基百科获得16位LFSR([16,14,13,11]处的抽头)的实现,如下所示.
unsigned short lfsr = 0xACE1u;
unsigned bit;
unsigned rand()
{
bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 5) ) & 1;
return lfsr = (lfsr >> 1) | (bit << 15);
}
Run Code Online (Sandbox Code Playgroud)
然而,在我的情况下,我需要将位从一个字节元素移位到另一个字节元素,例如msb或A [0]需要移位到A 1的lsb .这种转变的最小编码是什么?谢谢!