Annoying error of Uninitialized Memory Access

While analyzing a huge project at the mi4 level of Intel® Parallel Inspector, a user may get a lot of errors referred as 'Uninitialized Memory Access' which might considered as a false positives. In some cases these errors do not reflect the real problem in the application and a bunch of such errors might be annoying. Before getting rid of these errors with the Suppressions mechanism let’s consider the problem. The result of a mi4 level memory analysis of simple program is represented on the picture below. Inspector fires the Uninitialized Memory Access error blaming memcpy() function which reads the uninitialized memory pointed by foo. 

u1.JPG


Looking at the sample code, we can conclude that the flagged read operation does not affect correctness of the whole application, although it is not good engineering practice. 

      
      
1int main() {
2    char *foo = (char*)malloc(100);
3    char *bar = (char*)malloc(100);
4    memcpy(bar, foo, 100);
5     
6    free(bar);
7    free(foo);
8    return 0;
9}


It should be mentioned that Inspector does not report the error on the mi2 or mi3 levels (and not at mi1). However, a user might want to hide such reports for the sake of not littering the real errors list. There is an easy way to do that with the help of suppressions. 
In the Details view of the results, select 'Read' observation for this problem, right-click the mouse for the context menu and choose 'Suppress...'. 

u2.JPG


In the private suppression dialog create a filter with 'Uninitialized memory access' problem and 'Read' description. For all other columns (module, function, etc.) you may set * (all) depending of scope of interest. The setting will be saved in the .sup file which can be reused with any other project if made public (Tools > Options > Intel Parallel Inspector > General > Suppressions). 

u3.JPG


This can also be set by selecting 'Private Suppression: Delete problems' in the Configure Analysis dialog box before you click on 'Run Analysis'.

u4.JPG


The error will not appear after level mi4 analysis is completed. 

u5.JPG


Where an instruction is added to code that uses the uninitialized memory in a way that might affect correctness, Inspector should report the error regardless if 'Uninitialized memory access' is suppressed. Consider the following sample code. A printf() instruction sending the content of uninitialized memory to the output is added to the initial sample. 

      
      
1int main() {
2    char *foo = (char*)malloc(100);
3    char *bar = (char*)malloc(100);
4    memcpy(bar, foo, 100);
5    printf("%cn", bar[100]);//referencing uninitialized mem
6    free(bar);
7    free(foo);
8    return 0;
9}


Inspector will report ‘Invalid memory Access’ error on each level mi2-mi4 and flag the source code line containing the prinf() call. 

u6.JPG

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用c++和segment tree解决下述问题Doing Exercises 描述 As we all know, the lines of students doing exercises between classes are always unsatisfactory to teachers. Today, a teacher wants to require something new. Firstly, he lets some students of N classes correspondingly form n lines. Then, he randomly selects a class to add some of its remaining students to its line, or selects a class to let some students leave its line, or lets the monitors from some adjacent classes report the total number of students in all these classes. This is very annoying for the monitors. Can you write a program to help them complete the reporting task? 输入 The first line is an integer T (T<50), the number of test cases. For each test case, its first line is an integer N (1<=N<=50000), representing the number of classes, and its second line include N integers (a1, a2, a3, ... , an), and ai (1<=ai<=100) means the number of students in the line of class i at the beginning. Then, each next line will be an order. There are 4 kinds of orders: (1) "Add x i" means adding x students to the line of class i; (2) "Sub x i" means that x students leave the line of class i; (3) "Query i j" means that the monitors from class i to class j report the teacher the total number (sum) of students in their classes at that moment (i<j); (4) "End" means ending the exercises, which will only appear at the end of each test case. The number of orders will not exceed 40000. The number of students in any line will never below 0. 输出 For each test case, you must print "Case i:" in the first line. Then for each Query, print the result in one line.
最新发布
06-09
以下是使用 C++ 和线段树解决此问题的示例代码: ```c++ #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn = 50010; int n, m; int a[maxn]; struct node { int l, r; int sum; } tree[maxn * 4]; void build(int p, int l, int r) { tree[p].l = l; tree[p].r = r; if (l == r) { tree[p].sum = a[l]; return; } int mid = (l + r) / 2; build(p * 2, l, mid); build(p * 2 + 1, mid + 1, r); tree[p].sum = tree[p * 2].sum + tree[p * 2 + 1].sum; } void update(int p, int x, int v) { if (tree[p].l == x && tree[p].r == x) { tree[p].sum += v; return; } int mid = (tree[p].l + tree[p].r) / 2; if (x <= mid) update(p * 2, x, v); else update(p * 2 + 1, x, v); tree[p].sum = tree[p * 2].sum + tree[p * 2 + 1].sum; } int query(int p, int l, int r) { if (tree[p].l >= l && tree[p].r <= r) return tree[p].sum; int mid = (tree[p].l + tree[p].r) / 2, ans = 0; if (l <= mid) ans += query(p * 2, l, r); if (r > mid) ans += query(p * 2 + 1, l, r); return ans; } int main() { int T; scanf("%d", &T); for (int kase = 1; kase <= T; kase++) { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } build(1, 1, n); char op[10]; printf("Case %d:\n", kase); while (~scanf("%s", op)) { if (op[0] == 'E') break; int x, y; scanf("%d%d", &x, &y); if (op[0] == 'Q') { printf("%d\n", query(1, x, y)); } else if (op[0] == 'A') { update(1, x, y); } else if (op[0] == 'S') { update(1, x, -y); } } } return 0; } ``` 首先,我们定义了线段树节点结构体 `node`,包含线段树节点的左、右端点和区间和。`build` 函数用于建立线段树,`update` 函数用于更新某个节点的值,`query` 函数用于查询某个区间的和。 在主函数中,我们先输入测试用例的数量 `T`,然后对于每个测试用例,输入班级数量 `n` 和每个班级的学生数量。之后,我们不断读入命令,如果是查询命令,则调用 `query` 函数查询区间和并输出,如果是加操作,则调用 `update` 函数增加一个节点的值,如果是减操作,则调用 `update` 函数减少一个节点的值,如果是结束命令,则退出循环。 最后,我们输出每个测试用例的查询结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值