Compare source files on function level - string-comparison

I would like to compare two source files. While the output will return the functions that were changed. And not the lines.
My question is, what is the best why to find the start of a function and the end of it. If you know the row number.
Source files are in c.
Example
File Old
#include <stdio.h>
void checkPrimeNumber();
int main()
{
checkPrimeNumber(); // no argument is passed to prime()
return 0;
}
void checkPrimeNumber()
{
int flag=0;
printf("Enter a positive integer: ");
}
file New
#include <stdio.h>
void checkPrimeNumber();
int main()
{
checkPrimeNumber(); // no argument is passed to prime()
return 0;
}
void checkPrimeNumber()
{
int flag=1;
printf("Enter a positive integer: ");
}
Change is in fileNew flag=1;
Result will be
void checkPrimeNumber()
{
int flag=1;
printf("Enter a positive integer: ");
}

Related

Data Structures Simple Text Editor

I'm really new to DSA or basically just finished learning about Data Structures.
Tried this Hacker Rank problem (https://www.hackerrank.com/challenges/simple-text-editor/problem?isFullScreen=true) after solving a few of them already.
The problem here is after pushing a the first element in Stack, All the elements after that first element gets changed to the last added element.For e.g->
Push-> abcde,
Push-> de,
Push-> c,
Push-> ab
All the elements ab,c,de after abcde change to ab for some unknown reason.
I know there are/will be many simpler approaches than this one but I just wanna know what is wrong here.Any help would be appreciated since I'm a beginner. Thank you :)
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct Stack
{
char *arr[100][100];
int tos;
};
void push(struct Stack*,char*);
char *pop(struct Stack*);
int main() {
int i,place,j,Q,c;
char warr[100],Str[1000]="";
struct Stack S1,S2;
S1.tos=S2.tos=-1;
scanf("%d",&Q);
for(i=0;i<Q;i++)
{
scanf("%d",&c);
if(c==1)
{
fflush(stdin);
scanf("%s",warr);
strcat(Str,warr);
push(&S2,"1");
push(&S1,warr);
}
else if(c==2)
{
int k=0;
char warr[100]="";
scanf("%d",&place);
for(j=strlen(Str)-place;j<strlen(Str);j++)
{
warr[k]=Str[j];
k++;
}
warr[k]='\0';
push(&S2,"2");
push(&S1,warr);
Str[strlen(Str)-place]='\0';
}
else if(c==3)
{
scanf("%d",&place);
printf("%c\n",Str[place-1]);
}
else
{
if(strcmp(pop(&S2),"1")==0)
{
Str[strlen(Str)-strlen(pop(&S1))]='\0';
}
else
{
char *ch;
ch=pop(&S1);
strcat(Str,ch);
}
}
printf("string=%s\n",Str);
}
return 0;
}
void push(struct Stack *S,char *arr)
{
S->tos=S->tos+1;
S->arr[S->tos][99]=arr;
printf("pushed=%s\n",S->arr[S->tos][99]);
}
char* pop(struct Stack *S)
{
char* arr;
arr=S->arr[S->tos][99];
printf("popped=%s\n",S->arr[S->tos][99]);
S->tos=S->tos-1;
return arr;
}

Why is this code breaking on string input?

#include <iostream>
using namespace std;
string flip();
int x;
int main()
{
for(int i=0;i<10;++i){
cout<<"Press 1 and enter"<<endl;
cin>>x;
if(isalpha(x)==true){
cout<<"int pls"<<endl;
}
else if(x==1){
flip();
cout<<"flipped"<<endl;
}
else if(x!=1){
cout<<"try again"<<endl;
}
}
system("pause>0");
return 0;
}
string flip(){
string ans;
int y=rand()%2;
if(y==0){
string ans = "Heads";
cout<<ans<<endl;
}
else{
string ans = "Tails";
cout<<ans<<endl;
}
return ans;
}
whenever I put 2 instead of 1 it works and says try again but when i write some string like "fa"
the code closes instead of writing try again
if I change x to int and then if I try to input some string it just prints press 1 and enter 10 times instead of asking for an input again
The type of variable 'x'(=The argument of isalpha()) has to be char. If the type of 'x' is integer, the function(=isalpha()) recognizes 'x' as an ASCII value. How about trying this code?
char x;
int main()
{
for(int i=0;i<10;++i){
cout<<"Press 1 and enter"<<endl;
cin>>x;
if((bool)isalpha(x)==true){
cout<<"int pls"<<endl;
}
else if(x=='1'){
flip();
cout<<"flipped"<<endl;
}
else {
cout<<"try again"<<endl;
}
}
system("pause>0");
return 0;
}

Document function Transfer to calculate the row of text file

I want to transfer a function to calculate the row of a text file.
The compile can pass but the function can not be transferred. I want to know what happens.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int getLine( const char *filename)
{
ifstream infile(filename,ios::in);
if(!infile){
cout<<"can not open"<<filename<<'\n';
return 0;
}
int count=0;
infile.unsetf(ios::skipws);
char buff[300];
while(infile.getline(buff,300))
count++;
cout<<"the total line:"<<count<<endl;
infile.close();
return 0;
}
int getLineNoEmpty(const char* filename)
{
ifstream infile(filename,ios::in);
if(!infile){
cout<<"can not open"<<filename<<'\n';
return 0;
}
int count=0;
char buff[300];
while(infile.getline(buff,300))
{
if(sizeof(buff)==0)
continue;
else
count++;
}
cout<<"the total line without null string:"<<count<<endl;
return 0;
}
int main()
{
char filename[256];
cout<<"input filename:";
cin>>filename;
int getLine(const char &filename);
int getLineNoEmpty(const char &filename);
return 0;
}
The compile can pass but the function can not be transferred. I want to know what happens about it. It can output the result I want. And I don't know how to
realize the goal of calculating the total line without null string.
Firstly, You are just declaring 2 functions in main() without using them. Change
int main()
{
char filename[256];
cout<<"input filename:";
cin>>filename;
int getLine(const char &filename);
int getLineNoEmpty(const char &filename);
return 0;
}
to
int main()
{
char filename[256];
cout<<"input filename:";
cin>>filename;
getLine(filename);
getLineNoEmpty(filename);
return 0;
}

C++ pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug

I keep getting this error. I know what function causes it, but don't know how to fix it. Looking up online from this post saying:
You need to pass a pointer to a dynamically allocated object, or make your own insde your chainLink class.
However, as I try to pass a string pointer. error still popping up. Here is my code.
#include <iostream>
#include "MWTNode.h"
#include "MWT.h"
using namespace std;
int main() {
MWT t;
string str ="abc";
string* strPtr = &str;
t.insert(strPtr);
std::cout << "Hello, World!" << std::endl;
return 0;
}
#include "MWTNode.h"
class MWT {
public:
MWTNode *root;
string find(const string &);
void insert(const string* string);
};
void MWT::insert(const string* word) {
MWTNode* curr = root;
MWTNode newNode;
string w = *word;
for (int i = 0; i < word->length(); i++) {
const char c = w[i];
if (curr->children.find(c) == curr->children.end()){
//curr->children[c]= MWTNode();
//node->frequency=node->frequency+1;
}
curr = &(curr->children[c]);
}
curr->flag = true;
}
#include <unordered_map>
#include <vector>
#include <string>
#include <sstream>
#include <set>
using namespace std;
class MWTNode {
public:
unordered_map<char, MWTNode> children;
string value;
bool flag;
int frequency;
MWTNode(const string &);
MWTNode(const char c);
MWTNode();
void setFrequency ();
int getFrequency ();
};
MWTNode::MWTNode(const string &val) {
value = val;
flag = false;
frequency = 0;
}
MWTNode::MWTNode(const char c) {
value =c;
flag = false;
frequency = 0;
}
MWTNode::MWTNode() {
value ="";
flag = false;
frequency = 0;
}
Lets highlight a few lines of the code you show
class MWT {
public:
MWTNode *root;
// ...
};
In that you declare the member variable root as a pointer.
void MWT::insert(const string* word) {
MWTNode* curr = root;
// ...
}
In the above you make curr point to where root is pointing.
But you never make root point anywhere! The MWT::root variable is uninitialized and will have an indeterminate value. Using this pointer in any way without initialization will lead to undefined behavior.
And yes you use this pointer, as you dereference curr inside the MWT::insert function.
It's a little unclear what you're doing (to me) but you need to make sure that root (and therefore curr) is a valid pointer before attempting to dereference it.

Forward conditioned slicing with Frama-C

From the post Understanding Frama-C slicer results, it seems Frama-C supports the forward conditioned slicing with Frama-C. For the following example test.c, I wonder how to do the forward conditioned slicing with the specific function "event". #Pascal Cuoq
/*# requires a == 1 ;
*/
#include <stdio.h>
int event() {
int r;
scanf("%d", &r);
if (r>10) return 1;
else return 0;
}
void event_process() {
int x=0;
printf("event process\n");
x=1;
}
void no_event()
{
int y=0;
printf("no event\n");
}
void main ( ) {
int a;
if((a=event()) == 1)
event_process();
else
no_event();
printf("in main\n");
return;
}
I tried frama-c test.c -slice-calls event -then-on 'Slicing export' -print, why it outputs as follows:
/* Generated by Frama-C */
void event_slice_1(void)
{
return;
}
void main(void)
{
event_slice_1();
return;
}

Resources