-2

I am using a pointer in place of an array, I'm aware that a pointer needs to be freed unlike an array. Why is it that using a pointer in place of an array gives me a segmentation memory error.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

void bin(void){

char *input;
int choice;
int x = 0;


    printf("Enter Decimal Code:\n");
    scanf("%s",&input);

    int leng = strlen(input);



    for(int i = 0; i <= leng ; ++i){

        if(input[i] == '1'){
            x += pow(2,i);


        }
        else if(input[i] == '0'){
            input[i] = 0;


        }
        free(input);

    }
        printf("Binary-Dec: %d\n",x);


}

int main()
{
bin();

}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
robinhood46
  • 67
  • 1
  • 2
  • 9
  • 1
    A pointer only needs to be freed if it points to the beginning of a dynamically allocated block of data. Your pointer points nowhere good. So you can't pass it to `scanf`. – juanchopanza Jan 11 '16 at 10:34
  • @juanchopanza How would I be able to pass it through scanf/ is there an alternative – robinhood46 Jan 11 '16 at 10:35
  • Do not add tags for different languages. – too honest for this site Jan 11 '16 at 10:36
  • What are all those useless blank lines for ? – Jabberwocky Jan 11 '16 at 10:38
  • 1
    @robinhood46 You need to figure out what a pointer is (hint: there is a clue in the name) and you need to read some documentation for `scanf` (particularly what it assumes about the pointer you pass to it.) – juanchopanza Jan 11 '16 at 10:42
  • `char *input;` is a pointer that points to nowhere. – milevyo Jan 11 '16 at 10:43
  • 1
    You've missed the fact that a pointer needs to point to something - it's not a magical array replacement that conjures storage out of pixie dust when needed. It's also not true that all pointers need to be freed. I suggest that you get yourself a [good](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Jan 11 '16 at 10:44
  • http://ideone.com/idR4xn – BLUEPIXY Jan 11 '16 at 10:49
  • @juanchopanza to my understanding a pointer is a variable which points to a specific place in memory? – robinhood46 Jan 11 '16 at 10:59
  • @BLUEPIXY Thank you, all I had to do was replace %s with %ms wow – robinhood46 Jan 11 '16 at 11:04
  • @robinhood46 `%ms` is not standard. (by glibc [scanf](http://linux.die.net/man/3/scanf)) – BLUEPIXY Jan 11 '16 at 11:20

3 Answers3

4

In your code

 scanf("%s",&input);

is the problematic part. There are two issues, as I can see.

  • First, input being a pointer, &input is the address of the pointer.
  • Second, even if you remove the &, as the input is not allocated memory, it will still lead to undefined behavior.

You should rather do

 char input[32] = {0};
 scanf("%31s", input);   //limit the input buffer to prevent overflow

Also, it's advisable that you check for the return value of scanf() for success to ensure proper input.


FWIW, for the statement

[...]I'm aware that a pointer needs to be freed[..]

Right, but only when they are allocated memory dynamically through malloc() or family of functions. consider the below scenario,

char buf[32] = {0};
char *input = buf;
. . . . 

in this case, you need not free() input in any way, as the memory is not allocated dynamically. For more info, you can refer to this answer.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • In addition, calling `free()` on a pointer which does not point at dynamically allocated memory will most likely cause the program to crash, since it too is undefined behavior. – Lundin Jan 11 '16 at 12:16
1

Because your pointer is unitialized, therefore undefined behaviour will occur and your code may or may not work.

See this for more information.

Community
  • 1
  • 1
Neijwiert
  • 985
  • 6
  • 19
-1

All I had to do was to replace

   scanf("%s",&input);

with

   scanf("%ms",&input);

Thanks to BLUEPIXY

robinhood46
  • 67
  • 1
  • 2
  • 9
  • 2
    No, this does not solve the numerous severe bugs you still have in your program. Undefined behavior is tricky: the program might seem to work now, then suddenly crash next time you run it. You really need to read and understand the other answers posted. You have at least 3 severe bugs which may cause program crashes. – Lundin Jan 11 '16 at 12:11