- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我一直在努力理解为什么 valgrind 会为这个使用 ucontexts 的小测试程序提示“使用大小为 8 的未初始化值”。它基本上是一个创建“n_ucs”ucontext 并切换它们“max_switch”次的程序。
我理解“警告:客户端切换堆栈?” (这基本上就是程序的全部内容),但我无法真正理解所有“使用大小为 8 的未初始化值”
如果 Valgrind 错误是误报,或者这个程序是否存在根本性错误,我想获得一些帮助。(我在使用相同机制的更大的程序中看到了很多,但我已将其提炼到最低限度以在此处发布)。
感谢任何帮助。
谢谢,
jack
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <ucontext.h>
#define STACK_SIZE (8*1024)
int n_ucs = 1;
int max_switchs = 10;
int n_switchs = 0;
int tid = 0;
ucontext_t *ucs;
static ucontext_t engine_uc;
static void func(int arg)
{
while (n_switchs < max_switchs) {
int c_tid = tid;
int n_tid = (tid + 1) % n_ucs;
n_switchs++;
tid = n_tid;
swapcontext(&ucs[c_tid], &ucs[n_tid]);
}
}
int main(int argc, char **argv)
{
if (argc > 1)
n_ucs = atoi(argv[1]);
if (argc > 2)
max_switchs = atoi(argv[2]);
ucs = malloc(sizeof(ucontext_t) * n_ucs);
int i;
for (i = 0; i < n_ucs; i++) {
/* Create initial ucontext_t, including stack */
getcontext(&ucs[i]);
ucs[i].uc_stack.ss_sp = malloc(STACK_SIZE);
ucs[i].uc_stack.ss_size = STACK_SIZE;
ucs[i].uc_stack.ss_flags = 0;
ucs[i].uc_link = &engine_uc;
makecontext(&ucs[i], (void (*)())func, 1, i);
}
/* jump to the first uc */
swapcontext(&engine_uc, &ucs[tid]);
/* destroy stacks */
for (i = 0; i < n_ucs; i++)
free(ucs[i].uc_stack.ss_sp);
free(ucs);
return 0;
}
用 gcc main.c 编译并用 ./a.out 2 2 运行
gcc -v
Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.2-19ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1)
ldd --version
ldd (Ubuntu EGLIBC 2.19-0ubuntu6.3) 2.19 Copyright (C) 2014 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Written by Roland McGrath and Ulrich Drepper.
valgrind --track-origins=yes ./a.out 2 2
==21949== Memcheck, a memory error detector
==21949== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==21949== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==21949== Command: ./a.out 2 2
==21949==
==21949== Warning: client switching stacks? SP change: 0xffefffdd8 --> 0x51ff7b8
==21949== to suppress, use: --max-stackframe=68616717856 or greater
==21949== Use of uninitialised value of size 8
==21949== at 0x400738: func (main.c:25)
==21949== by 0x4E58EC4: (below main) (libc-start.c:287)
==21949== Uninitialised value was created by a stack allocation
==21949== at 0x4E7E445: swapcontext (swapcontext.S:92)
==21949==
==21949== Conditional jump or move depends on uninitialised value(s)
==21949== at 0x4E807A7: __start_context (__start_context.S:37)
==21949== by 0x4E58EC4: (below main) (libc-start.c:287)
==21949== Uninitialised value was created by a stack allocation
==21949== at 0x4E7E445: swapcontext (swapcontext.S:92)
==21949==
==21949== Syscall param rt_sigprocmask(set) contains uninitialised byte(s)
==21949== at 0x4E7E0EC: setcontext (setcontext.S:47)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== Uninitialised value was created by a stack allocation
==21949== at 0x4E7E445: swapcontext (swapcontext.S:92)
==21949==
==21949== Use of uninitialised value of size 8
==21949== at 0x4E7E0F5: setcontext (setcontext.S:54)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== Uninitialised value was created by a stack allocation
==21949== at 0x4E7E445: swapcontext (swapcontext.S:92)
==21949==
==21949== Use of uninitialised value of size 8
==21949== at 0x4E7E0FE: setcontext (setcontext.S:56)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== by 0x4E807AD: __start_context (__start_context.S:39)
==21949== Uninitialised value was created by a stack allocation
==21949== at 0x4E7E445: swapcontext (swapcontext.S:92)
==21949==
==21949== Warning: client switching stacks? SP change: 0x51ff7c0 --> 0xffefffde0
==21949== to suppress, use: --max-stackframe=68616717856 or greater
==21949==
==21949== HEAP SUMMARY:
==21949== in use at exit: 0 bytes in 0 blocks
==21949== total heap usage: 3 allocs, 3 frees, 18,256 bytes allocated
==21949==
==21949== All heap blocks were freed -- no leaks are possible
==21949==
==21949== For counts of detected and suppressed errors, rerun with: -v
==21949== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 0 from 0)
最佳答案
您必须将堆栈的更改通知 valgrind。在此处阅读示例 https://github.com/lu-zero/valgrind/blob/master/memcheck/tests/linux/stack_changes.c
这是正确的代码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <ucontext.h>
#include <valgrind/valgrind.h>
#define STACK_SIZE (8*1024)
int n_ucs = 1;
int max_switchs = 10;
int n_switchs = 0;
int tid = 0;
ucontext_t *ucs;
static ucontext_t engine_uc;
void func(int arg)
{
while (n_switchs < max_switchs) {
int c_tid = tid;
int n_tid = (tid + 1) % n_ucs;
n_switchs++;
tid = n_tid;
swapcontext(&ucs[c_tid], &ucs[n_tid]);
}
}
int main(int argc, char **argv)
{
if (argc > 1)
n_ucs = atoi(argv[1]);
if (argc > 2)
max_switchs = atoi(argv[2]);
ucs = malloc(sizeof(ucontext_t) * n_ucs);
//store the VALGRIND_STACK_REGISTER return values
int* valgrind_ret = malloc(n_ucs*sizeof(int));
int i;
for (i = 0; i < n_ucs; i++) {
/* Create initial ucontext_t, including stack */
getcontext(&ucs[i]);
//pass stack to valgrind
void* mystack = malloc(STACK_SIZE);
VALGRIND_STACK_REGISTER(mystack, mystack + STACK_SIZE);
ucs[i].uc_stack.ss_sp = mystack;
ucs[i].uc_stack.ss_size = STACK_SIZE;
ucs[i].uc_stack.ss_flags = 0;
ucs[i].uc_link = &engine_uc;
makecontext(&ucs[i], (void (*)())func, 1, i);
}
/* jump to the first uc */
swapcontext(&engine_uc, &ucs[tid]);
/* destroy stacks */
for (i = 0; i < n_ucs; i++) {
//valgrind stack deregister
VALGRIND_STACK_DEREGISTER(valgrind_ret[i]);
free(ucs[i].uc_stack.ss_sp);
}
free(ucs);
return 0;
}
关于c - valgrind 错误和 ucontext。为什么是 "Use of uninitialised value of size 8"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25703245/
我已经使用 vue-cli 两个星期了,直到今天一切正常。我在本地建立这个项目。 https://drive.google.com/open?id=0BwGw1zyyKjW7S3RYWXRaX24tQ
您好,我正在尝试使用 python 库 pytesseract 从图像中提取文本。请找到代码: from PIL import Image from pytesseract import image_
我的错误 /usr/bin/ld: errno: TLS definition in /lib/libc.so.6 section .tbss mismatches non-TLS reference
我已经训练了一个模型,我正在尝试使用 predict函数但它返回以下错误。 Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]])
根据Microsoft DataConnectors的信息我想通过 this ODBC driver 创建一个从 PowerBi 到 PostgreSQL 的连接器使用直接查询。我重用了 Micros
我已经为 SoundManagement 创建了一个包,其中有一个扩展 MediaPlayer 的类。我希望全局控制这个变量。这是我的代码: package soundmanagement; impo
我在Heroku上部署了一个应用程序。我正在使用免费服务。 我经常收到以下错误消息。 PG::Error: ERROR: out of memory 如果刷新浏览器,就可以了。但是随后,它又随机发生
我正在运行 LAMP 服务器,这个 .htaccess 给我一个 500 错误。其作用是过滤关键字并重定向到相应的域名。 Options +FollowSymLinks RewriteEngine
我有两个驱动器 A 和 B。使用 python 脚本,我在“A”驱动器中创建一些文件,并运行 powerscript,该脚本以 1 秒的间隔将驱动器 A 中的所有文件复制到驱动器 B。 我在 powe
下面的函数一直返回这个错误信息。我认为可能是 double_precision 字段类型导致了这种情况,我尝试使用 CAST,但要么不是这样,要么我没有做对...帮助? 这是错误: ERROR: i
这个问题已经有答案了: Syntax error due to using a reserved word as a table or column name in MySQL (1 个回答) 已关闭
我的数据库有这个小问题。 我创建了一个表“articoli”,其中包含商品的品牌、型号和价格。 每篇文章都由一个 id (ID_ARTICOLO)` 定义,它是一个自动递增字段。 好吧,现在当我尝试插
我是新来的。我目前正在 DeVry 在线学习中级 C++ 编程。我们正在使用 C++ Primer Plus 这本书,到目前为止我一直做得很好。我的老师最近向我们扔了一个曲线球。我目前的任务是这样的:
这个问题在这里已经有了答案: What is an undefined reference/unresolved external symbol error and how do I fix it?
我的网站中有一段代码有问题;此错误仅发生在 Internet Explorer 7 中。 我没有在这里发布我所有的 HTML/CSS 标记,而是发布了网站的一个版本 here . 如您所见,我在列中有
如果尝试在 USB 设备上构建 node.js 应用程序时在我的树莓派上使用 npm 时遇到一些问题。 package.json 看起来像这样: { "name" : "node-todo",
在 Python 中,您有 None单例,在某些情况下表现得很奇怪: >>> a = None >>> type(a) >>> isinstance(a,None) Traceback (most
这是我的 build.gradle (Module:app) 文件: apply plugin: 'com.android.application' android { compileSdkV
我是 android 的新手,我的项目刚才编译和运行正常,但在我尝试实现抽屉导航后,它给了我这个错误 FAILURE: Build failed with an exception. What wen
谁能解释一下?我想我正在做一些非常愚蠢的事情,并且急切地等待着启蒙。 我得到这个输出: phpversion() == 7.2.25-1+0~20191128.32+debian8~1.gbp108
我是一名优秀的程序员,十分优秀!