-1

This part of my code is about signup.

I only can signup for one time, the next time the program stops.

What is the problem?

while (1) { /*usercounter initialized with 0*/
    printf("enter your order:\n");
    gets(buffer);
    order = strtok(buffer, " ");
    if (strcmp(order, "signup") == 0) {
        usercounter++;
        if (usercounter > 50) {
            username=realloc(username,usercounter*sizeof(*username));
            password=realloc(password, usercounter*sizeof(*password));
        }
        username[(usercounter - 1)] = (char *)malloc(50*sizeof(char));
        strcpy(username[usercounter - 1], strtok(NULL, " "));
        password[(usercounter - 1)] = (char *)malloc(50*sizeof(char));
        strcpy(password[usercounter - 1], strtok(NULL, "\n"));
        free(buffer);
        continue;
    }
}
jwpfox
  • 5,124
  • 11
  • 45
  • 42
Me.Me
  • 19
  • 4
  • 1
    [DO NOT use `gets()`, it is dangerous](http://stackoverflow.com/q/1694036/2173917). use [`fgets()`](https://linux.die.net/man/3/fgets) instead. – Sourav Ghosh Dec 08 '16 at 20:02
  • `free(buffer);` !? – BLUEPIXY Dec 08 '16 at 20:09
  • All I see here is very dangerous code and exploits. I'm not versed enough at C to cover the fixes without introducing other bugs, but there's a use after free, gets() issues, and I can't see any way you're managing (counting) your references so leaks and faults are likely to occur. – gelliott181 Dec 08 '16 at 20:12
  • 1
    Need to see more: variable declarations, inputs used, etc. – chux - Reinstate Monica Dec 08 '16 at 20:23

1 Answers1

1
free(buffer);

why this is used, it will lead to undefined behavior, you are freeing a static memory. Remove the above statement.

Rest is fine.

Sohil Omer
  • 1,171
  • 7
  • 14