小编Bil*_*nch的帖子

drd和helgrind的当前状态支持std :: thread

当我将代码转换为C++ 11时,我非常希望将我的pthread代码转换为std :: thread.但是,我似乎在drd和helgrind中的非常简单的程序中获得了错误的竞争条件.

#include <thread>

int main(int argc, char** argv)
{
    std::thread t( []() { } );
    t.join();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Helgrind输出片段 - 我在drd中也遇到类似的错误,在Ubuntu 11.11 amd64上使用gcc 4.6.1,valgrind 3.7.0.

我的问题是:

  • 理智检查:我做错了什么?其他人是否在简单的std :: thread程序上得到类似的错误报告?
  • 用于检测竞争条件的std :: thread的当前用户是什么?

我不愿意将大量代码从pthread移植到std::threadhelgrind/drd等一些关键工具.

==19347== ---Thread-Announcement------------------------------------------
==19347== 
==19347== Thread #1 is the program's root thread
==19347== 
==19347== ---Thread-Announcement------------------------------------------
==19347== 
==19347== Thread #2 was created
==19347==    at 0x564C85E: clone (clone.S:77)
==19347==    by 0x4E37E7F: do_clone.constprop.3 (createthread.c:75)
==19347==    by 0x4E39604: pthread_create@@GLIBC_2.2.5 (createthread.c:256)
==19347==    by 0x4C2B3DA: pthread_create_WRK (hg_intercepts.c:255)
==19347== …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading race-condition c++11

11
推荐指数
1
解决办法
2151
查看次数

运行junit test时是否可以进入android的调试模式?

通常我会使用junit测试adb shell am instrument -w com.android.contacts.tests/android.test.InstrumentationTestRunner.它确实有效,它将运行我所有的测试.

但是当我在运行junit时制作断点并希望进入调试模式时,它失败了.当我调试普通的Android应用程序时,我的断点的方式工作.

所以我搜索网络,尝试类似的东西adb shell am instrument -e debug true -e class com.android.contacts.AndroidUtilsTest -w com.android.contacts.tests/android.test.InstrumentationTestRunner,但仍然没有运气.你如何解决这个问题?我不仅想调试junit类,还想调试普通项目中的代码.

tdd junit android android-testing

11
推荐指数
2
解决办法
7104
查看次数

为什么正则表达式总是返回1?

以下函数检查变量名称是否以字母开头,并且可能具有字母/数字的前面的字符.无论输入是什么,为什么返回值始终为1?

#include <regex.h>
#include <stdio.h>

int validate_var(char *str)
{
    regex_t reg;
    regcomp(&reg, "^[a-zA-Z]+[a-zA-Z0-9]*$", 0);
    int r = regexec(&reg, str, 0, NULL, 0);
    regfree(&reg);

    return r;
}

int main() {
    printf("%d\n", validate_var("abc")); // Reports 1, This makes sense
    printf("%d\n", validate_var("17"));  // Reports 1, This doesn't make sense
}
Run Code Online (Sandbox Code Playgroud)

c regex

11
推荐指数
1
解决办法
121
查看次数

如何在C++中初始化嵌套结构?

我在程序中创建了几个不同的结构.我现在有一个嵌套结构的结构,但我无法弄清楚如何正确初始化它们.结构如下所列.

/***POINT STRUCTURE***/
struct Point{
    float x;                    //x coord of point
    float y;                    //y coord of point
};

/***Bounding Box STRUCTURE***/
struct BoundingBox{
    Point ymax, ymin, xmax, xmin;
};

/***PLAYER STRUCTURE***/
struct Player{
    vector<float> x;            //players xcoords
    vector<float> y;            //players ycoords
    BoundingBox box;
    float red,green,blue;       //red, green, blue colour values
    float r_leg, l_leg;         //velocity of players right and left legs
    int poly[3];                //number of points per polygon (3 polygons)
    bool up,down;               
};
Run Code Online (Sandbox Code Playgroud)

然后我尝试初始化一个名为player的新创建的Player结构.

//Creates player, usings vectors copy and iterator …
Run Code Online (Sandbox Code Playgroud)

c++ nested structure initialization data-structures

10
推荐指数
1
解决办法
2万
查看次数

使用最小括号的漂亮打印AST

我正在为一个JavaScript AST实现一个漂亮的打印机,我想问一下是否有人知道一个"正确的"算法,根据运算符优先级和关联性自动用最小括号括起表达式.我没有在谷歌上找到任何有用的材料.

显而易见的是,父级具有更高优先级的运算符应该用括号括起来,例如:

(x + y) * z // x + y has lower precedence
Run Code Online (Sandbox Code Playgroud)

但是,也有一些运算符不是关联的,在这种情况下仍需要括号,例如:

x - (y - z) // both operators have the same precedence
Run Code Online (Sandbox Code Playgroud)

我想知道后一种情况的最佳规则是什么.对于除法和减法,是否足以说明,如果rhs子表达式的优先级小于或等于,则应将其括起来.

javascript compiler-construction pretty-print abstract-syntax-tree parentheses

10
推荐指数
2
解决办法
1716
查看次数

将高级语言编译为机器代码

在从网站上阅读了一些答案并查看了一些资源后,我认为编译器会将高级语言(例如C++)转换为机器代码,因为计算机本身不需要将其转换为汇编,它只会转换它组装以供用户查看代码,并且可以根据需要对代码进行更多控制.

但这可以在我的一个演讲表中找到,所以如果有人能够进一步解释并纠正我,如果我错了,或者下面的截图,我会感激.

滑动

c++ compiler-construction assembly machine-code

10
推荐指数
2
解决办法
4462
查看次数

MAC OSX Intel LLVM汇编程序错误(导致Vorbis OGG加载程序崩溃)

我在Mac OSX上加载Vorbis Ogg文件时遇到了神秘的错误.第一个文件正确加载,第二个文件在某些​​代码中崩溃,表明文件已损坏,即使我加载相同的文件两次,也会发生相同的情况.

在Vorbis内部进行了长时间的深度调试后,我发现该错误是由系统函数"pow"(双倍功率)引起的,它返回一个(nan)表示完全有效的输入,并且仅在第二次调用时发生(ov_read ),在第一次调用时,传递给"pow"的相同精确值返回有效结果.

8小时后,很多英特尔x87文档读取我发现了问题.长话短说,在vorbis"vorbis_ftoi"内部有一个函数,它使用这个汇编代码:

__asm__("fistl %0": "=m"(i) : "t"(f));
Run Code Online (Sandbox Code Playgroud)

哪个应该在英特尔FPU堆栈上推送和弹出.但是在LLVM上它会生成以下代码:

fld    QWORD PTR [ebp-0x20]
fist   DWORD PTR [ebp-0x14]
Run Code Online (Sandbox Code Playgroud)

它推送堆栈但从不弹出导致FPU堆栈溢出.这显然是LLVM中的一个错误

GCC生成的正确代码如下所示:

fld    QWORD PTR [ebp-0x20]
fist   DWORD PTR [ebp-0xc]
fstp   st(0)        // pops off the stack
Run Code Online (Sandbox Code Playgroud)

我浪费了一天半的时间和我的brian的一些字节学习了一些垃圾(x87指令集和寄存器),所以我会分享它.

奥代

macos assembly intel llvm vorbis

9
推荐指数
1
解决办法
714
查看次数

对签名数据进行逻辑右移

在此之前,这不是我的作业,它是由一本名为"计算机系统程序员的视角"的书给出的实验室(优秀的书btw)

我需要在有符号整数上执行逻辑移位,而不使用以下任何内容:

  • 铸件
  • if,while,select,for,do-while,?:
  • 任何类型的指针

允许的运营商是:!+〜| >> << ^

到目前为止我尝试了什么?

/* 
 * logicalShift - shift x to the right by n, using a logical shift
 *   Can assume that 0 <= n <= 31
 *   Examples: logicalShift(0x87654321,4) = 0x08765432
 *   Legal ops: ~ & ^ | + << >>
 *   Max ops: 20
 *   Rating: 3 
 */
int logicalShift(int x, int n) {
    int mask = ~0;
    int shiftAmount = 31 + ((~n)+1);//this evaluates to 31 - n on two's …
Run Code Online (Sandbox Code Playgroud)

c c++ x86 bit-manipulation bit-shift

9
推荐指数
2
解决办法
6785
查看次数

std :: string :: clear和std :: list :: clear是否擦除了内存中的数据

在string :: clear函数的描述中,它说:

clear:删除字符串的内容,该字符串变为空字符串(长度为0个字符).

在list :: clear函数的描述中,它说:

clear:从列表容器中删除所有元素(已销毁),并将容器的大小保留为0.

clear是否会覆盖字符串和列表的内存或者只是释放它们?

c++ string list

9
推荐指数
2
解决办法
2871
查看次数

clang 3.5中不支持-finline-functions?

我正在使用他们分发的clang 3.5.我正在使用以下命令行在我的travis vm中安装它:

sudo apt-add-repository 'deb http://llvm.org/apt/precise/ llvm-toolchain-precise-3.5 main'
sudo apt-add-repository 'deb http://llvm.org/apt/precise/ llvm-toolchain-precise-3.5 main'
Run Code Online (Sandbox Code Playgroud)

当我在启用优化的情况下运行我的测试版本时,我收到此错误:

clang: error: optimization flag '-finline-functions' is not supported
  "clang++" -c -x c++ -std=c++1y -Werror -O3 -finline-functions -Wno-inline -Wall -Werror -pthread -fPIC -std=c++1y -DBOOST_ALL_DYN_LINK -DNDEBUG -I"." -I"gamgee" -I"lib/htslib" -o "test/bin/run.test/clang-linux-3.5.0/release/threading-multi/sam_builder_test.o" "test/sam_builder_test.cpp"
Run Code Online (Sandbox Code Playgroud)

我在我的mac上运行较旧的3.4版本的clang时没有得到相同的错误.

clang是否削减了对3.5中的-finline-functions的支持?这是关于这个包构建的具体内容吗?如何用clang-3.5 +替换-finline-functions选项来优化构建?

c++ inline clang c++11

9
推荐指数
1
解决办法
2388
查看次数