0

i keep getting valgrind errors in my code and i have no idea how to fix it. :/ the idea is that no matter how much tabs / spaces are between 2 or more words/letters in an input, in the output it should be only one space. for example:

a    b   c d -> a b c d

code:

char* echo(char* in) {
    char buffer[256];
    int incounter=0, buffcounter=0;

    while(incounter<(strlen(in))) {
        if(in[incounter] == ' ' || in[incounter] == '\t') incounter++;
        else if(in[incounter] != ' ' && in[incounter] != '\t') {
            while(in[incounter] != ' ' && in[incounter] != '\t') {
                buffer[buffcounter] = in[incounter];  //53
                incounter++;
                buffcounter++;              
            }           
            buffer[buffcounter] = ' ';
            buffcounter++;
        }
    }
    char* out = buffer;
    return out;
}

errors:

==20521== Conditional jump or move depends on uninitialised value(s)
==20521==    at 0x4010B4: echo (hhush.c:53)
==20521==    by 0x4021CA: readCommand (hhush.c:327)
==20521==    by 0x402538: main (hhush.c:371)
==20521==  Uninitialised value was created by a stack allocation
==20521==    at 0x402017: readCommand (hhush.c:301)
==20521== 
==20521== Conditional jump or move depends on uninitialised value(s)
==20521==    at 0x4010CE: echo (hhush.c:53)
==20521==    by 0x4021CA: readCommand (hhush.c:327)
==20521==    by 0x402538: main (hhush.c:371)
==20521==  Uninitialised value was created by a stack allocation
==20521==    at 0x402017: readCommand (hhush.c:301)

thats where i am now, still the same errors

char* echo(char* in,char* buffer){
    size_t inlen=strlen(in);
    int incounter=0,buffcounter=0;
    while(incounter<inlen){
        if(in[incounter]==' '||in[incounter]=='\t')incounter++;
        else{
            while(in[incounter]!=' '&&in[incounter]!='\t'){
                buffer[buffcounter]=in[incounter];
                incounter++;
                buffcounter++;              
            }           
            buffer[buffcounter]=' ';
            buffcounter++;
        }   
    }
    return buffer;
}

i call it with:

char input[256];
fgets(input,sizeof(input),stdin);
...
char buffer[256];
printf("%s\n",echo(input,buffer));
9
  • 1
    what makes you think that strlen(in) <= 255 ? Commented Aug 7, 2014 at 23:30
  • the function will never get called with a bigger input Commented Aug 7, 2014 at 23:35
  • Why do you compute strlen(in) in every loop iteration? It does not change. OTAH, I think this part can simply be removed if(in[incounter] != ' ' && in[incounter] != '\t'). Commented Aug 7, 2014 at 23:36
  • then the blanks wont be skipped Commented Aug 7, 2014 at 23:45
  • The inner while loop can run off the end of the input string Commented Aug 7, 2014 at 23:51

3 Answers 3

4
 char* out=buffer;
 return out;
}

Do not return a pointer to an array with automatic storage duration (here buffer array). If the pointer value is accessed, it invokes undefined behavior as automatic objects are discarded when the block where they are declared is exited (here when echo function returns).

Sign up to request clarification or add additional context in comments.

3 Comments

@slyr To quickly check if it fixes the valgrind messages, just add the static specifier in the array declaration.
@slyr in that case we also need to see how is called echo and how is its argument declared.
My guess is that this is what valgrind found, but there appear to be several other bugs in the program, not just this.
0

If you want to use the buffer outside of the function then you should pass the allocated buffer to the function.

void echo(char *in, char *buffer)
{
    //do stuff
}

This way you won't be trying to access memory that has gone out of scope.

You can then use the function as such

char buffer[256];
echo(string, buffer);

The buffer should now contain your edited string.

Comments

0

This message:

==20521== Conditional jump or move depends on uninitialised value(s)
==20521==    at 0x4010B4: echo (hhush.c:53)
==20521==  Uninitialised value was created by a stack allocation
==20521==    at 0x402017: readCommand (hhush.c:301)

is telling you that an if or while test on line 53 depends on uninitialzed data. The data in question was allocated at line 301. By looking at those lines, you should be able to tell what is going on, but it looks like you're allocating an array on the stack in readCommand and then passing it to echo as an argument, without ever putting anything in the array. So your 'i call it with code' is actually something like:

char input[256];
fgets(input,sizeof(input),stdin);
...
char buffer[256];
printf("%s\n",echo(buffer));

and you're not passing what you think you're passing

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.