String printing garbage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <stdlib.h>

#define MAXINPUT 100

char *getinput()
{
    int a;
    char*string=malloc(MAXINPUT+1);
    char*stringhead=string;
    while(a=getchar()!=';')
    {
        *string=a;
        string++;
    }
    *string='\0';
    return stringhead;
}

int main(void)
{
    printf("%s",getinput());
    return 0;
}


And before anyone points out line 8 as culprit, its not incorrect
Last edited on
Do not use malloc, printf or C strings in C++.
A proper version of this is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;

string getInput()
{
  string line;
  getline(cin,line,';');
  return line;
}

int main()
{
  cout << getInput() << endl;
}

Doesn't your string go out of scope when you return it?
Also, good practice is to free() everything you malloc().

Anyway the problem is that line 11 should be
while((a = getchar()) != ';')

And for God's sake learn to type spaces.
HA!

;-) How'd you pick that up so quickly? THANKS
I must keep repeating to myself:
"Precedence, precedence, precedence"

Doesn't your string go out of scope when you return it?


What do you mean by this? At the end it points to '\0'...

Last edited on
What do you mean by this? At the end it points to '\0'...


I take that back, I was overthinking.
I was thinking about local variables being lost i.e. "going out of scope" when a function ends.
This doesn't hold for this case, you're returning a mere address.
Oh okay.

I fixed up the code like you suggested:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <stdlib.h>

#define MAXINPUT 100

char* getinput()
{
    int a;
    char* string = malloc(MAXINPUT+1);
    char* stringhead = string;
    while((a = getchar()) != ';')
    {
        *string = a;
        string++;
    }
    *string = '\0';
    free(string);             /* Added Free() */
    return stringhead;
}

int main(void)
{
    printf("%s", getinput());
    return 0;
}


And I just want to ask about free(). Does it basically set the next malloc pointer to "overwrite" what it was pointing to? Im not really sure what free does...
I think it simply unmarks the memory as being in use.

As for your program you should manage your string outside the getinput() function. Because once you free() it, I personally wouldn't trust its contents anymore but maybe that's just me.

I'm thinking this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <stdlib.h>

#define MAXINPUT 100

char* getinput(char *string)
{
    int a;
    char* stringhead = string;
    while((a = getchar()) != ';')
    {
        *string = a;
        string++;
    }
    *string = '\0';
    return stringhead;
}

int main(void)
{
    char* string = malloc(MAXINPUT+1);
    printf("%s", getinput(string));
    free(string);
    return 0;
}
Last edited on
As a matter of interest... I tried avoiding the string terminator '\0' and it doesn't affect the programme. What difference does it make in this scenario?
Chances are the memory you're allocating is "clean" meaning it's 0000000000 when you allocate it...
Putting a '\0' will make a difference when the memory happens not to be "clean".

Which is why you should use calloc(size_t num, size_t size) for safety.
http://www.cplusplus.com/reference/clibrary/cstdlib/calloc/
Topic archived. No new replies allowed.