根据http://en.cppreference.com/w/cpp/language/zero_initialization,我无法理解何时以及为什么我的班级中的某个成员是零初始化的.
考虑以下测试程序:
#include <iostream>
#include <stdio.h>
class MyTest {
private:
const static unsigned int dimension = 8;
void (* myFunctions [dimension])();
public:
MyTest() {}
void print() {
for(unsigned int i=0; i < MyTest::dimension; i++) {
printf("myFunctions[%d] = %p\n", i, this->myFunctions[i]);
}
}
};
int main() {
//We declare and initialize an object on the stack
MyTest testObj = {};
testObj.print();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我声明一个类有一个包含签名"void functionname()"的8个函数指针的数组.当我宣布和初始化类的对象main作为MyTest testObj = {};或者MyTest testObj;,我希望它是零初始化,即所有指针都是空指针.
但是,在带有g++ -m32 …
我目前正在尝试将一个shadertoy.com着色器(Atmospheric Scattering Sample,带代码的交互式演示)移植到Unity.着色器是用GLSL编写的,我必须启动编辑器C:\Program Files\Unity\Editor>Unity.exe -force-opengl以使其渲染着色器(否则出现"此着色器无法在此GPU上运行"错误),但现在这不是问题.问题是将着色器移植到Unity.
散射等功能在我的移植着色器中都是相同且"可运行"的,唯一的mainImage()功能是功能管理摄像机,光线方向和光线方向本身.必须改变这一点,以便使用Unity的摄像机位置,视图方向以及光源和方向.
原始的主要功能如下:
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// default ray dir
vec3 dir = ray_dir( 45.0, iResolution.xy, fragCoord.xy );
// default ray origin
vec3 eye = vec3( 0.0, 0.0, 2.4 );
// rotate camera
mat3 rot = rot3xy( vec2( 0.0, iGlobalTime * 0.5 ) );
dir = rot * dir;
eye = rot * eye;
// sun light dir
vec3 l = vec3( 0, …Run Code Online (Sandbox Code Playgroud) 我想保护函数免受多线程访问.为此我使用的是pthread_mutex_t互斥锁.我尝试将其锁定在函数的开头,然后执行该函数,然后再次释放它.如果使用互斥锁,它应该等待最多60秒才能使用.如果之后它仍然不可用,则该功能将失败.
我遇到的问题是它pthread_mutex_timedlock似乎完全忽略了我给它的超时值.虽然我指定了60秒的超时,但是如果进行了锁定,该函数会立即返回错误代码ETIMEDOUT- 而不是实际等待.
这是一个重现问题的最小例子.在这种情况下,我使用递归或非递归互斥体并不重要,因为我不是试图从同一个线程多次锁定它们.
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stddef.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <pthread.h>
pthread_mutex_t lock; /* exclusive lock */
//do some work to keep the processor busy..
int wut() {
int x = 0;
for(int i=0; i < 1024*1024*1024; i++)
x += 1;
return x;
}
void InitMutex(){
/*pthread_mutexattr_t Attr;
pthread_mutexattr_init(&Attr);
pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&lock, &Attr);*/
pthread_mutex_init(&lock, NULL);
}
//lock mutex, wait at maximum …Run Code Online (Sandbox Code Playgroud) 我的目的是能够捕获全局变量何时具有某个精确值。GDB 有数据观察点,可以根据这些观察点来实现。
考虑这个为 x86 Linux 编写的简单程序:
int myVar = 0;
void debug_watchpoints() {
for(int i=0; i < 2000; i++) {
myVar++;
}
}
int main() {
debug_watchpoints();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译程序
gcc -o main -ggdb3 -Og main.c
Run Code Online (Sandbox Code Playgroud)
并开始使用 GDB 进行调试:
max@PC-LT-23:~/stackoverflow$ gdb ./main
GNU gdb (Ubuntu 8.3-0ubuntu1) 8.3
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is …Run Code Online (Sandbox Code Playgroud) 我需要在Unity中创建一个无限循环而不使用主线程?我看到了一些例子,但它并没有用:
while(true){
var aa;
debug.log("print.");
}
Run Code Online (Sandbox Code Playgroud)
我想添加一些延迟,例如2秒.如果有人知道解决方案请帮助.
我有一个Register将 astd::function<void(const uint8_t* data, size_t len)>作为参数的函数。我想使用对象的成员函数作为目标。
我发现这个问题的答案是用来std::bind将第一个参数(隐式this指针)绑定到实际对象指针,然后将其用作std::function参数。
然而,这在 C++11、C++14 和 C++17 中都不再起作用了?
考虑以下测试程序。
#include <iostream>
#include <cstdint>
#include <functional>
void Register(std::function<void(const uint8_t* data, size_t len)> func) {
//Dummy - directly call into function
func(nullptr, 0);
}
class TestClass {
public:
void TestRegister() {
Register(
std::bind(&TestClass::TestTarget, this, std::placeholders::_1)
);
}
void TestTarget(const uint8_t* data, size_t len) {
(void) data;
(void) len;
std::cout << "Hello there" << std::endl;
}
}; …Run Code Online (Sandbox Code Playgroud) DirectoryInfo如果我声明的目录不存在或被更改,如何知道创建新目录的位置?因为Create()方法不带参数.
喜欢,Directory.CreateDirectory(path)路径通过的地方.所以,会DirectoryInfo.Create()和Directory.CreateDirectory("C:\\users\\Desktop")做同样的事情......使用C:\\users\\Desktop作为路径.