__attribute__ ((__packed__))嵌套结构有什么影响?例如:
// C version
struct __attribute__ ((__packed__))
{
struct
{
char c;
int i;
} bar;
char c;
int i;
} foo;
// C++ version
struct __attribute__ ((__packed__)) Foo
{
struct Bar
{
char c;
int i;
} bar;
char c;
int i;
} foo;
Run Code Online (Sandbox Code Playgroud)
我知道foo会紧紧包装,但是怎么样bar?它会紧紧包装吗?是否__attribute__ ((__packed__))使嵌套struct也包装好?
我在C99编程并在我的代码的一部分中使用可变长度数组.我知道在C89中不允许使用零长度数组,但我不确定C99和可变长度数组.
简而言之,以下是明确定义的行为吗?
int main()
{
int i = 0;
char array[i];
return 0;
}
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
#include <vector>
#include <algorithm>
template <typename Input1, typename Input2, typename Output>
void merge(Input1 begin1, Input1 end1, Input2 begin2, Input2 end2, Output out)
{
}
int main()
{
std::vector<int> a = {1, 2};
int b[] = {3, 4};
int c[4];
merge(a.begin(), a.end(), b, b + 2, c);
}
Run Code Online (Sandbox Code Playgroud)
汇总收益率:
$ clang++ -std=c++11 -stdlib=libc++ merge.cpp
merge.cpp:15:5: error: call to 'merge' is ambiguous
merge(a.begin(), a.end(), b, b + 2, c);
^~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/algorithm:4056:1: note:
candidate function [with _InputIterator1 = std::__1::__wrap_iter<int *>,
_InputIterator2 = …Run Code Online (Sandbox Code Playgroud) 我有一个单作者,多读者的情况。一个线程正在写入一个计数器,任何线程都可以读取该计数器。由于单个写入线程不必担心与其他线程竞争数据访问权限,因此以下代码安全吗?
#include <stdatomic.h>
#include <stdint.h>
_Atomic uint32_t counter;
// Only 1 thread calls this function. No other thread is allowed to.
uint32_t increment_counter() {
atomic_fetch_add_explicit(&counter, 1, memory_order_relaxed);
return counter; // This is the line in question.
}
// Any thread may call this function.
uint32_t load_counter() {
return atomic_load_explicit(&counter, memory_order_relaxed);
}
Run Code Online (Sandbox Code Playgroud)
writer线程只读取counter直接的内容,而无需调用任何atomic_load*函数。这应该是安全的(因为多个线程读取一个值是安全的),但是我不知道声明变量是否会_Atomic限制您直接使用该变量,或者是否需要始终使用其中一个atomic_load*函数读取它。
请考虑以下代码:
pub trait Trait {
type Type;
const CONST: Self::Type;
}
impl<T> Trait for T {
type Type = u8;
const CONST: u8 = 42;
}
Run Code Online (Sandbox Code Playgroud)
我(不正确?)对Rust的理解是这个代码应该工作,所有Sized类型现在应该实现Trait并且具有关联的type(Type = u8)和const(CONST = 42).因为未施胶的类型不应该实行这种特质impl<T>所隐含的假设T是Sized.
但是,当我尝试编译代码时,我收到错误消息:
error[E0277]: the size for value values of type `T` cannot be known at compilation time
--> src/main.rs:8:3
|
8 | const CONST: u8 = 42;
| ^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known …Run Code Online (Sandbox Code Playgroud) 我一直想知道是否可以使用带有一些优化标志的 GCC 进行编译以避免 .rodata 部分有两个重复的数组?因此,内存地址将是相同的。例如:
\n\nconst char str [7] = "string";\n\nconst char str1 [7] = "string";\n\n\nint printf (const char * format, ...);\n\nint main (void) {\n\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 if (str == str1)\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 printf ("Equal memory addresses");\n\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 return 0;\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n那么在上面的例子中,编译器是否有可能以某种方式使用相同的内存地址?
\n虽然C++标准不允许使用字符串文字作为模板参数,但允许这样的事情:
ISO/IEC 14882:2011
14.3.2模板非类型参数[temp.arg.nontype]
2 [注意:字符串文字(2.14.5)不满足任何这些类别的要求,因此不是可接受的模板参数.[例如:
template<class T, const char* p> class X { / ... / };
X<int, "Studebaker"> x1; // error: string literal as template-argument
const char p[] = "Vivisectionist";
X<int,p> x2; // OK- 末端示例] - 尾注]
那么为什么以下代码在所有编译器中都会出错(gcc 4.7.2,MSVC-11.0,Comeau)?
template <const char* str>
void foo() {}
int main()
{
const char str[] = "str";
foo<str>();
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用protobuf-net来序列化对象.我不确定是否支持我正在尝试继承的内容,但我想我会检查并查看它是否存在或者我是否只是做错了什么.
本质上,我正在尝试序列化一些子类,然后反序列化它,但只使用基类引用.展示:
using UnityEngine;
using System.Collections;
using ProtoBuf;
public class main : MonoBehaviour
{
// If I don't put "SkipConstructor = true" I get
// ProtoException: No parameterless constructor found for Parent
// Ideally, I wouldn't have to put "SkipConstructor = true" but I can if necessary
[ProtoContract(SkipConstructor = true)]
[ProtoInclude(1, typeof(Child))]
abstract class Parent
{
[ProtoMember(2)]
public float FloatValue
{
get;
set;
}
public virtual void Print()
{
UnityEngine.Debug.Log("Parent: " + FloatValue);
}
}
[ProtoContract]
class Child …Run Code Online (Sandbox Code Playgroud) 在以下示例中:
struct SimpleMemoryBank {
vec: Vec<Box<i32>>,
}
impl SimpleMemoryBank {
fn new() -> SimpleMemoryBank {
SimpleMemoryBank{ vec: Vec::new() }
}
fn add(&mut self, value: i32) -> &mut i32 {
self.vec.push(Box::new(value));
let last = self.vec.len() - 1;
&mut *self.vec[last]
}
}
fn main() {
let mut foo = SimpleMemoryBank::new();
// Works okay
foo.add(1);
foo.add(2);
// Doesn't work: "cannot borrow `foo` as mutable more than once at a time"
let one = foo.add(1);
let two = foo.add(2);
}
Run Code Online (Sandbox Code Playgroud)
add()可以连续多次调用,只要我不存储函数调用的结果.但是如果我存储函数(let one …
考虑以下(非法)示例:
enum Foo {
Bar { i: i32 },
Baz,
}
struct MyStruct {
field: Foo::Bar,
}
Run Code Online (Sandbox Code Playgroud)
Foo::Bar是一个类似结构的变体.我发现它们非常有用.但是,我有一个实例,我需要在另一个结构中存储结构的实例,如上面的示例MyStruct.改变MyStruct::field是一个Foo将是无效的,因为它没有为外地意义是一个Foo::Baz.它只是一个实例Foo::Bar.
rustc 告诉我上面的代码无效:
error: found value name used as a type: DefVariant(DefId { krate: 0u32, node: 4u32 }, DefId { krate: 0u32, node: 5u32 }, true)
Run Code Online (Sandbox Code Playgroud)
我只是做错了什么,或者这是不可能的?如果不可能,有没有计划呢?
我知道我可以像这样解决它,但我认为它是一个劣等选项,如果可能的话,我想避免它:
struct Bar {
i: i32,
}
enum Foo {
Bar(Bar),
Baz,
}
struct MyStruct {
field: Bar,
}
Run Code Online (Sandbox Code Playgroud)