Thread: The program does not output the string in certain circumstances

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    5

    Unhappy The program does not output the string in certain circumstances

    Software code with a bug: pastebin.com/raw/3Tt974if
    Code without bug: pastebin.com/raw/Nh8JqqQH
    In the second code, I output a string without single quotes, and therefore both this line and the past were displayed on the screen. I do not understand what the problem is! Here are the screenshots:
    Bug screenshot: i.stack.imgur.com/GDy3I.png
    The second screenshot where there is no glitch: i.stack.imgur.com/bwKwa.png

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,525
    Don't be so lazy, post your code here.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char * reverse(char *s)
    {
    	int len = strlen(s);
    	char buf[len];
    	for(int l = len; l > 0; l--)
    		buf[l] = *s++;
    	buf[len] = '\0';
    	return buf;
    }
    
    int main(int argc, char const *argv[])
    {
    	char *str = "hello";
    	printf("'%s'\n", reverse(str));
    	str = "hel";
    	printf("'%s'\n", reverse(str));
    	return 0;
    }
    
    vs
    #include <stdio.h>
    #include <string.h>
    
    char * reverse(char *s)
    {
    	int len = strlen(s);
    	char buf[len];
    	for(int l = len; l > 0; l--)
    		buf[l] = *s++;
    	buf[len] = '\0';
    	return buf;
    }
    
    int main(int argc, char const *argv[])
    {
    	char *str = "hello";
    	printf("'%s'\n", reverse(str));
    	str = "hel";
    	printf("'%s'\n", reverse(str));
    	printf("%s\n", reverse(str));
    	return 0;
    }
    In both cases, you're returning a pointer to a local variable when you do return buf;
    That means your entire result string is out of scope when you get back to main.
    The memory that buf occupied can be re-used for other purposes, leading to all manner of interesting effects, including apparently working.

    Reverse is broken - period.
    Tweaking main into giving you the apparently right answer is voodoo.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    5
    Quote Originally Posted by Salem View Post
    Don't be so lazy, post your code here.
    If I were lazy, I would not throw codes on pastebin, because it would make me tense up more than putting the code here!

  4. #4
    Registered User
    Join Date
    Apr 2018
    Posts
    5

    Red face

    Quote Originally Posted by Salem View Post
    In both cases, you're returning a pointer to a local variable when you do return buf;
    That means your entire result string is out of scope when you get back to main.
    The memory that buf occupied can be re-used for other purposes, leading to all manner of interesting effects, including apparently working.

    Reverse is broken - period.
    Tweaking main into giving you the apparently right answer is voodoo.
    I've programmed a lot on java and python, and for me it's very difficult to understand the C language in which you can not return a local variable without any problems.
    The problem was because I was mistaken in the program! I made a debug and realized that I went beyond the array and here is the working code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char * reverse(char *s, char *buf)
    {
        int len = strlen(s);
        strcpy(buf, s);
        for(int l = len; l > 0; l--) {
            //printf("%i: '%c'\n", l, *s);//DEBUG
            buf[l-1] = *s++;
        }
        return buf;
    }
    
    int main(int argc, char const *argv[])
    {
        char *str = "hello";
        char buf[5];
        printf("'%s'\n", reverse(str, buf));
        str = "hel";
        printf("'%s'\n", reverse(str, buf));
        printf("'%s'\n", buf);
        return 0;
    }

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    657
    Closer. buf needs to be allocated with 6 elements because the string "hello" has 5 characters plus a null terminator.

  6. #6
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Did you know that you have two variables named buf? You can put in something like this to see.
    Code:
    printf("%p", &buf);
    I think you need to use double pointers to solve this one.
    Code:
    static void reverse(char **pstr, char *pbuf) {
    called like
    Code:
    reverse(&str, buf);
    Last edited by christophergray; 04-06-2018 at 12:24 AM.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    657
    Quote Originally Posted by christophergray View Post
    Did you know that you have two variables named buf?
    There are two "buf" variables, but they both point to (the first element of) the same array, so there's no need to change anything in the code as it's written (except to make the array larger).

    Edit: actually, "buf" in "main" is an array, and "buf" in "reverse" is a pointer that points to the first element of the "buf" array in "main". They can be used interchangeably.
    Last edited by christop; 04-06-2018 at 10:09 AM.

  8. #8
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Quote Originally Posted by christop View Post
    there's no need to change anything in the code as it's written
    Isn't the buf in reverse declared locally? So it is going out of scope right after returning to main, right? Oh, now looking back I see Salem already said that. I tried to code this but I think I need quiet, morning alertness, and coffee. I was trying to use double pointers and it gave me a headache.

  9. #9
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    657
    Quote Originally Posted by christophergray View Post
    Isn't the buf in reverse declared locally? So it is going out of scope right after returning to main, right?
    Yes, but that's only in the original code in #1 (and #2). In the code in #4, there is a local variable (function parameter) in the reverse function named "buf" that points to the "buf" array in main. That's the code that I was referring to.

  10. #10
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Quote Originally Posted by christop View Post
    T "buf" in "reverse" is a pointer that points to the first element of the "buf" array in "main".
    Where is it being returned to​? Some unnamed variable on the stack?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-18-2017, 10:06 AM
  2. Replies: 5
    Last Post: 04-14-2016, 08:27 AM
  3. Replies: 1
    Last Post: 01-18-2016, 06:12 PM
  4. output file is a unicode file under certain circumstances
    By rivkyfried1 in forum C Programming
    Replies: 4
    Last Post: 10-25-2010, 09:16 AM
  5. how to store the pipe the output of a program to string ?
    By jabka in forum Linux Programming
    Replies: 1
    Last Post: 06-25-2007, 10:22 AM

Tags for this Thread