Reputation: 159
Just learnt C this week. My task is to take a large big integer input from the user, store it into a struct Integer, and make a function to print out the appropriate struct integer to the standard output. The program works as such, but as soon as it gives the output, it stops responding. I dont get any direct error in the compiler and cannot figure out what is wrong. Any other advice/tips to improve upon programming style would also be really appreciated :)
// Header Files Go Here
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function Declarations Go Here
struct integer * convert_integer(char * stringInt);
void printer(struct integer * p);
struct integer {
int * arr;
int length;
};
// Main Program
int main() {
char * x;
x = (char *) malloc(sizeof(char) * 10000);
printf("Enter a small string\n");
scanf("%s",x);
int j = 0;
struct integer * book1;
book1 = convert_integer(x);
printer(book1);
return 0;
}
// Function Definitions Go Here
struct integer * convert_integer(char * stringInt) {
struct integer * x = malloc(sizeof(int) * 100);
int j = 0;
while (stringInt[j] != '\0') {
if (stringInt[j] < 48 || stringInt[j] >= 57) {
printf("Invalid input. Enter a number ");
return;
}
x->arr[j] = stringInt[j] - 48;
j++;
}
x->length = j;
printf("\n the length is %d\n", x->length);
return x;
}
void printer(struct integer * p) {
int j = 0;
while (j < p->length) {
printf("%d", p->arr[j]);
j++;
}
}
Upvotes: 0
Views: 131
Reputation: 3751
I am adding this answer because NPE was not clear enough.
The are a few errors in this program but I would say the pressing one is in the function convert_integer
. You are doing the following:
struct integer* x= malloc(sizeof(int) * 100);
... but that is incorrect. You are requesting too many bytes of memory for x
considering its data type (nothing really wrong with that), however you are not requesting blocks of memory for arr
. It needs to be as follows:
struct integer *x = malloc( sizeof( struct integer ) );
x->arr = malloc( sizeof( int ) * c );
... where c
is some constant (in your case 100?). Make sure that when you free
this structure you first free
the arr
and then free
the structure, or else you will have a memory leak.
Something else I noticed you are not doing, always check the result of a system call. You are not checking to see if malloc
returned an invalid memory block.
Upvotes: 2
Reputation: 8829
You need to add a prototype for printer
on top. The if
statement validating the number should be
if(stringInt[j]<48 || stringInt[j]>57) // > instead of >=
It will make more sense to write it as
if ( stringInt[j] < '0' || stringInt[j] > '9' )
If you know about isdigit
function, you can use that as well.
You are allocating memory for a string with 10000 characters. That seems like an overkill. You can allocate memory for 256 characters and that should be plenty. You also should deallocate memory before executing return
using the statement free (x);
You need to allocate space for arr
inside the convert_integer
function. Your allocations should be:
struct integer* x= malloc(sizeof(integer)); x->arr = malloc ( sizeof(int) * 256 );
Upvotes: 0
Reputation: 3295
Your code is failing at the line
x->arr[j]=stringInt[j]-48;
The reason is the way you allocate your memory slice:
struct integer* x= malloc(sizeof(int) * 100);
This allocates a slice of n*100
bytes and stores a pointer to that slice in x
. What you want to do is the following:
struct integer *x = malloc(sizeof(*x));
x->arr = malloc(sizeof(int)*100);
That allocates the memory needed to store the integer
structure x
and it allocates memory to store 100 integers and assigns it to x->arr
. With your code, x->arr
is not initialized and thus has random values in it (more precisely: The values are those that were stored at that memory location before).
After finished using the allocated memory, you should also free it:
free(x->arr);
free(x);
Upvotes: 0
Reputation: 500157
Here:
struct integer* x= malloc(sizeof(int) * 100);;
you are allocating memory for the structure (i.e. the pointer and the int), but not for the array.
The moment you try to assign to x->arr[j]
, you have undefined behaviour.
Upvotes: 0