Reputation: 15
I'm writing a program which seems to have become reliant on the line
printf("%s", xStr);
Removing this line, commenting it out, or changing it to
printf("%s\n", xStr);
will cause my program to crash. Is there any circumstance, ever, under which removing this line should break a program?
Edit: Full code is below
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
char *toString(int x){
char *tmp;
sprintf(tmp, "%d", x);
return tmp;
}
int valueOf(char c){
if(isalpha(c))
return c - 'A' + 10;
else
return c - '0';
}
char *to10(char *xStr, int initBase){
int tmp = 0, i;
for(i = 1; i <= strlen(xStr); i++){
tmp += valueOf(xStr[strlen(xStr) - i])*pow(initBase, i - 1);
}
return toString(tmp);
}
char **buildAnsArray(FILE *fp, int n){
int i;
char **ansArray = malloc(sizeof(char)*n);
for(i = 0; i < n; i++){
char *item = malloc(10*sizeof(char));
fscanf(fp, "%s", item);
ansArray[i] = item;
}
return ansArray;
};
char *baseConversion(char *xStr, int initBase, int finBase){
printf("%s", xStr);
if(initBase != 10){
xStr = to10(xStr, initBase);
}
int x = atoi(xStr), i = 0;
while(pow(finBase,i) < x){
i++;
}
char *tmp = malloc(sizeof(char)*i);
int max = i;
while(i > 0){
int divisor = pow(finBase, i - 1);
int quotient = x/divisor;
if(quotient > 9){
tmp[max - i] = 'A' + quotient - 10;
}
else{
tmp[max - i] = (char)(((int)'0')+quotient);
}
x -= quotient*divisor;
i--;
}
printf("%s\n", tmp);
return tmp;
}
char *pow2Converter(int x, int initBase, int finBase){
x = atoi(baseConversion(x, initBase, 2));
int chunkSize = log2(finBase);
int tmp = 0;
while(x > 0){
tmp += atoi(baseConversion(toString(x%(int)pow(10,chunkSize)), 2, finBase));
x /= pow(10,chunkSize);
}
return tmp;
}
int findAnswer(char **ansArray, char *xStr, int high, int low){
}
main(){
FILE *in;
int num, i;
in = fopen("data.txt", "r");
fscanf(in, "%d", &num);
char **ansArray = buildAnsArray(in, num);
for(i = 0; i < num; i++){
int initBase, finBase, ansPos;
char *xFin, *xInit = malloc(sizeof(char)*10);
fscanf(in, "%s %d %d", xInit, &initBase, &finBase);
xFin = baseConversion(xInit, initBase, finBase);
ansPos = findAnswer(ansArray, xFin, num - 1, 0);
//printf("%d\n", ansPos);
}
}
Upvotes: 1
Views: 180
Reputation: 1
What exactly is your code doing? It compiles and runs but does not do anything because your find answer function was left empty (not sure if that was your intention). If you could post the finished code so it would print output that would help us.
and maybe some sample data. I created data of my own, but I'm getting odd outputs when trying to print out some of you values. Want to give us some data to read and your output you recieve.
Upvotes: 0
Reputation: 881103
What you have is a Heisenbug, one which disappears or moves whenever you try to look at it :-)
It's usually caused by doing something undefined, such as:
char *toString(int x){
char *tmp;
sprintf(tmp, "%d", x);
return tmp;
}
Don't do that. You're creating a pointer which could point anywhere, then you writing information to it.
You can't even use the time-honored technique of creating an array, since you return it and would no doubt use it after it's gone out of scope.
If you're sure you're not going to call it more than once at a time (such as with threads), you can do this as a quick fix:
char *toString (int x) {
static char tmp[30];
sprintf(tmp, "%d", x);
return tmp;
}
Upvotes: 4