Name: Anonymous 2010-01-17 12:35
I'm a newfag to programming in C, and I'm having trouble with a function that I've written for a program that does operations with "big integers". Basically, my function runs once, perfectly, but on the second iteration, it crashes when it goes to allocate memory to the temporary structure.
I've tried a few things, but this one mostly has me stumped. Any help would be greatly appreciated, you don't have to tell me what to do but just a nudge in the right direction would be sufficient.
This is my function, complete with my debugging lines so you can get a better idea of what I've been doing:
struct integer* read_integer(char* stringInt){
int length = strlen(stringInt);
//COMMENCING DEBUGGING
printf("FUNCTION START\n");
printf("LENGTH VALUE = %d\n", length);
struct integer* temp = (struct integer*)malloc(sizeof(struct integer)*length);
temp->size = length;
//DEBUGGING
printf("The SIZE value of the structure is currently: %d\n", temp->size);
//DEBUGGING
printf("stringInt currently contains all of the following:\n");
for(i=0;i<length;i++){
printf("%d: %c\n", i, stringInt[i]);
}
int i = 0;
int j = length;
while(stringInt[i]!='\0'){
temp->digits[j-1]=(int)stringInt[i];
temp->digits[j-1]-=48;
i++;
j--;
}
//MORE DEBUGGING
printf("The ARRAY within the STRUCT has the following VALUES:\n");
for(i=0;i<length;i++){
printf("%d: %d\n", i, *temp->digits);
}
//After DEREFERENCING the struct above by adding an asterisk, it displayed correct values. Function works so far.
//DEBUGGING
printf("FUNCTION COMPLETE\n\n\n");
return temp;
}