intel® VTune™ Amplifier XE 2013 Unknown source file - intel-vtune

I have the unknown source file in my report which make me impossible to fine the correct loop to analyze my report
I want to show the source code with the vtune analyze, however if i compile the c and get the execute file. When i try to analyze the behavior, i can only check the assembly code! Any one got idea how to show the source code? also if i want to do itt_pause() and itt_resum() how am i suppose to do that with out the icc compiler
i compile the file as
gcc -c -std=c99 -O2 app.c
gcc -o app app.o
source code that i need to analyze:
#include <stdio.h>
#include <stdlib.h>
int foo_data_collected1()
{
int sum = 0;
for (int i = 0; i < 100; i++)
{
if ((i & 0xffffffff) == 0)
{
sum++;
printf("T");
}
printf("F");
}
return sum;
}
int foo_data_collected2()
{
int sum = 0;
for (int i = 0; i < 10000000L; i++)
{
if ((i & 2) == 0)
{
sum++;
}
}
return sum;
}
int foo_data_collected3()
{
int sum = 0;
for (int i = 0; i < 10000000L; i++)
{
if ((i & 0x80000000) == 0)
{
sum++;
}
}
return sum;
}
int foo_data_collected4()
{
int sum = 0;
for (int i = 0; i < 10000000L; i++)
{
if ((i & 4) == 0)
{
sum++;
}
}
return sum;
}
int foo_data_collected5()
{
int sum = 0;
for (int i = 0; i < 10000000L; i++)
{
if ((i & 3) == 0)
{
sum++;
}
}
return sum;
}
int main()
{
int sum = 0;
sum = foo_data_collected1();
printf("\nSum = %d\n", sum);
return 0;
}

Use -g compiler option when you are compiling your sources to add debug information.
gcc -g -c -std=c99 -O2 app.c
gcc -o app app.o

Related

Segmentation Fault - malloc 2D array

Trying to create a map for a little game. When initialising the map with 2D arrays using malloc, the main function will run okay when the printMap function is commented out, however when trying to display the map with printMap, it returns a Segmentation fault. Totally lost at why this isn't working. Any help appreciated.
This is work for University, who insist the code is in C89 and I compile with -ansi -pedantic -Wall -Werror.
GAME.C file
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include"random.h"
void createMap(char*** map, int xCoord, int yCoord) {
int i, j;
xCoord += 2;
yCoord += 2;
char** mapArray;
mapArray = (char**)malloc(yCoord * sizeof(char*));
for (i = 0; i < yCoord; i++) {
mapArray[i] = (char*)malloc(xCoord * sizeof(char));
}
for (i = 0; i < yCoord; i++) {
for (j = 0; j < xCoord; j++) {
mapArray[i][j] = "0";
}
}
*map = mapArray;
}
void printMap(char** map, int xCoord, int yCoord) {
xCoord += 2;
yCoord += 2;
printf("%d, %d", xCoord, yCoord);
int i, j;
for (i = 0; i < yCoord; i++) {
for (j = 0; j < xCoord; i++) {
printf("%d %d", i, j);
printf("%c", map[i][j]);
}
printf("\n");
}
}
MAIN.C file
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include "random.h"
#include "game.h"
int main(void) {
int xCoord = 5;
int yCoord = 5;
char** map;
createMap(&map, xCoord, yCoord);
printMap(map, xCoord, yCoord);
return 0;
}
The function createMap is incorrectly initializing objects of the type char with pointers of the type char * to which the string literal "0" is implicitly converted in these for loops
for (i = 0; i < yCoord; i++) {
for (j = 0; j < xCoord; j++) {
mapArray[i][j] = "0";
}
}
Instead of the string literal you need to use integer character constant '0' as for example
for (i = 0; i < yCoord; i++) {
for (j = 0; j < xCoord; j++) {
mapArray[i][j] = '0';
}
}
Another problem is a typo in this loop within the function printMap
for (j = 0; j < xCoord; i++) {
^^^^
You need to write
for (j = 0; j < xCoord; j++) {
^^^^

Error: 'for' loop initial declarations are only allowed in c99 mode

I've got this problem where I can only compile using the gcc -std=c99 but however, i need it to compile using c89 aka gcc -Wall. This is part of my code where i use the 'for' loop. Please see if you can help me out thank you in advance.
#include<stdio.h>
int main()
{
int arr[100],i=0,ch;
int n = 1, sum = 0;
printf("Check out our selection! \n");
printf("Airhead - 25 cents\n");
printf("Fun Dip - 40 cents\n");
printf("Gummi Bears - 20 cents\n");
while (n != 0)
{
printf("Insert Coins: ");
scanf("%d",&n);
arr[i++] = n;
}
for(int j=0;j<i;j++)
{ sum = sum + arr[j];
}
......
This is wrong:
for (int j = 0; j < i; j++) {
sum = sum + arr[j];
}
You have to initialize j in beginning of function.
int main() {
int j;
...
for (j = 0; j < i; j++) {
sum = sum + arr[j];
}
}

What's wrong with my Checkprime.c program?

I'm trying to make a program that checks whether a given number is a prime number or not. However, my program just gives me the 2 time table, and I don't know why.
Here is my main class:
#include <stdio.h>
#include "defs.h"
#include "checkprime.c"
int Prime[MaxPrimes];
int main()
{
int UpperBound;
int N;
int *ba = &UpperBound;
printf("enter upper bound\n");
scanf("%d",ba);
Prime[2] = 1;
for (N = 3; N <= *ba; N+= 2)
{
CheckPrime(N);
if (Prime[N]== 1) printf("%d is a prime\n",N);
}
}
Here is my checkprime.c
#include "defs.h"
#include "externs.h"
int CheckPrime(int K)
{
int J;
J = 2;
while (1)
{
if (Prime[J] == 1)
{
if (K % J == 0)
{
Prime[K] = 0;
return 0;
}
J++;
}
break;
}
Prime[K] = 1;
}
There are some problems in CheckPrime with the loop exit conditions. Use the following instead:
int CheckPrime(int K)
{
int J;
for (J=2; J*J <= K; J++) {
if (Prime[J] == 1) {
if (K % J == 0) {
Prime[K] = 0;
return 0;
}
}
}
Prime[K] = 1;
return 1;
}
The rest of it should work with this change.

Is there something different with C in Hackerrank(getting different output)?

I've been trying to do the Love-Letter Mystery Challenge on Hackerrank.
Here are the rules: https://www.hackerrank.com/challenges/the-love-letter-mystery
And here's my solution:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAX 1000
int check_palindrome(char *A)
{
int i = 0, j;
while(A[i])
i++;
i -= 1;
for(j = 0; j <= i; j++)
{
if(A[j] != A[i - j])
return 0;
}
return 1;
}
int love_letter(char *A)
{
int i = 0;
int j;
int times;
while(A[i])
i++;
i -= 1;
if(i == 0)
return 0;
if(check_palindrome(A))
return 0;
for(j = i; j >= 0; j--)
{
while(A[j] != 'a')
{
if(check_palindrome(A))
return times;
else
{
A[j] -= 1;
times += 1;
}
}
}
return times;
}
int main() {
int t, i;
char a[MAX];
scanf("%d", &t);
for(i = 0; i < t; i++)
{
scanf("%s", a);
printf("%d\n", love_letter(a));
}
return 0;
}
While testing in on my computer, I get the right output. But, when I try to run the code on Hackerrank, it tell's that my program always gives an output of:
0
0
0
0
That's wrong of course, and it fails the testcase. But why is that? Is there something different about C or something? Or is it just a problem with the site? Or with my code?
At a minimum, you seem to have forgotten to initialize the variable "times".
In terms of the actual algorithm, keep in mind that to make the letters match, you can decrement either (or both) of them. I don't think you handle all cases properly.

Inside a "for" loop, how can I increment another variable every time the indexer counts "x"units?

Title says it all,
i think there is no need for the code as the problem is finding the algorithm itself.
int x=10; //Just suppose
int b=0;
for(int i=0;i<10000;i++){
if(i%x == 0){
b++;
}
// Rest of loop code
}
That should about do it, I think.
Try,
for(i=0;i<MAX;i++)
{
if(i%x==0)
{
counter++;
}
/* Loop Body */
}
For an integer variable x, the expression x % y == 0 will be 1 if x is evenly divisible by y and 0 otherwise.
for (i=0; i < N; i++) {
counter += (i % interval == 0);
}
This one maybe?
#include <stdio.h>
int main()
{
int i;
int multiple = 40;
int j = 0;
for (i = 0; i < 1200; i++)
{
if (i % multiple == 0)
j++;
}
printf("%d, %d, %d\n", i, multiple, j);
return 0;
}
After compiling it and running it I get the following:
$ gcc test.c
$ ./a.out
1200, 40, 30
$
i=0;
while(1)
{
i=(i+1)%x ;
if(i==0)
counter++;
}
for( int i=0; i < 1200 ;i++)
{
j += i/40;
}

Resources