0

I'm trying to allocate a dynamic memory for string which is of unknown length (im trying for a mock-compi question) but when i do that my printf wont print the first character from second execution. I'm using gcc 5.something. I've attached a screen shot of the output.enter image description here

   #include<stdio.h>
   #include<stdlib.h>
   #include<string.h>
   void main (){


    //VARIABELS

        //Input bitstream as a string
        char *stringInput;

        //No of inputs
        int dataCount;

        //memory size to allocate to array
        int dataSize; 

        //FILE pointer
        FILE *fptr;

        //Loop count
        int i;

        //Binary declarations
        long num, decimal_num, remainder, base = 1, binary = 0, noOf1 = 0;



        //MEMORY ALLOCATION

        //Enter N
        printf("Enter N (the number of inputs):\n");
        scanf("%d",&dataCount);

        //+1 for comma (,) characters
        dataSize = ((sizeof(int)+1)* dataCount);

        //Initialize pointer allocated memory
        stringInput = malloc(dataSize);

        if (!stringInput)
        { 
            perror("Error allocating memory");
            abort();
        }

        memset(stringInput, 0, dataSize);
        //Scan the numbers + TEST
        scanf("%s",stringInput);

        //Initialize file pointer
        fptr = fopen("inputString.txt","w"); 
        fprintf(fptr,"%s",stringInput);

        free(stringInput);



        //DECIMAL CONVERSION
        while (num > 0){
            remainder = num % 2;

            //Calc no of 1's
            if (remainder == 1)
                noOf1++;

            //Conversion
            binary = binary + remainder * base;
            num = num / 2;
            base = base * 10;
        }



    }
Xtreme
  • 31
  • 5
  • 2
    `sizeof int` gives you the number of bytes needed to store an `int`. This has nothing to do with the number of characters you need to write this value as a number in decimal digits. –  Jul 21 '18 at 17:31
  • 1
    Use `sizeof(char)` instead, or just omit it since `sizeof(char)` is always 1. And you need +1 for the null terminator. And you should pass `dataCount` as a parameter to `scanf()` so it can't overflow `stringInput` if the user types more than `dataCount` number of characters: `scanf("%*s", dataCount, stringInput);` – Remy Lebeau Jul 21 '18 at 17:35
  • @FelixPalmen yes i need to take the input as string but the string will have int and commas and no spaces, 50 ints max, but since the input is in string form, we are dealing with characters (1 byte) and ints are of 8 bytes (x64 enviorenment) so my max array size = (50 integers*8+49 commas +1(for \0)) = 450 Oh gg i solved my own problem lol a lil 1 have huge 1's ahead – Xtreme Jul 21 '18 at 21:01
  • DO NOT post screen shots. Rather, copy and paste the actual text into your question – user3629249 Jul 22 '18 at 20:00

1 Answers1

0

Change this:

dataSize = ((sizeof(int)+1)* dataCount);

to this:

dataSize = ((sizeof(char) + 1) * (dataCount + 1));

since you want to store a string, not a number.

Notice the +1 I used at the end, which is for the string NULL-terminator.


PS: What should main() return in C and C++? int.

gsamaras
  • 66,800
  • 33
  • 152
  • 256
  • which would assume every "number" has just a single decimal digit. Oh and the `+1` adds one to every single item here. Honestly, I don't understand OPs code, but it does some calculations on an uninitialized `num`. –  Jul 21 '18 at 17:45
  • `sizeof(char)` is the size of one char, the size of a sequence of characters which represent an integer is amultiple of that. The size of `','` is also `sizeof(char)` this time actually only once. Using `+1` here assumes that one char is one byte. Am I missing something? – Yunnosch Jul 21 '18 at 17:49
  • Sorry, my comment was wrong, but indeed, it adds two bytes. And looking at OP's code, again, this doesn't look like the actual source of the problem. Using uninitialized variables is bad, using `scanf("%s", ...)` is bad, bad things can happen anywhere in this code. –  Jul 21 '18 at 17:53