添加到字符串开头的额外字符?

时间:2016-02-29 04:10:46

标签: c

我有两个额外的字符被添加到我的字符串的开头,我似乎无法找出原因。字符甚至不出现在代码中。我在这里不知所措。这是我的代码:

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

char *chars;

char* vector(char input, char *newlist);

int main(){

    char *input, *out = "Input: ";

    printf("Enter characters: ");                   
    while(1){
        char i = getchar();                         //get input
        if(i == '\n'){
            break;                                  //detect a return key
        } else{
            input = vector(i, input);               //call vector
        }
    }

    char * print = (char *)malloc(1 + strlen(input) + strlen(out));
    strcpy(print, out);                             //concat the strings
    strcat(print, input);

    printf("\n%s", print);                          //print array

    free(print);
    free(input);
    free(chars);

    return 0;                                       //exit
}

char* vector(char in, char *newlist){

    int length = strlen(newlist);                   //determine length of newlist(input)

    chars = (char*)calloc(length+2, sizeof(char));  //allocate more memory
    strcpy(chars, newlist);                         //copy the array to chars
    chars[length] = in;                             //appened new character
    chars[length + 1] = '\0';                       //append end character

    return chars;
}

由于某种原因,代码产生了这个:

Enter characters: gggg

Input: PEgggg

什么时候应该产生这个:

Enter characters: gggg

Input: gggg

5 个答案:

答案 0 :(得分:6)

所有的点@MikeCat都是正确的,只是补充说free分配的内存没有被释放,导致内存泄漏。您可以在评论中{@ 1}} @ M.M所说的那样,但是下次为了避免内存泄漏,您可以使用valgrind

  

让我们将您的计划作为hash.c。到了命令行并编译它,例如:

gcc hash.c -Wall
     

如果您的程序成功编译,将出现可执行文件或out文件。由于我们没有指定可执行文件的名称,因此它的默认名称为a.out。所以让我们用valgrind

运行它
valgrind -- leak-check=full ./a.out
     

这将运行可执行文件以及valgrind,如果存在内存泄漏,它将在可执行文件结束时显示它。

           

如果您没有安装valgrind,可以从here安装。

答案 1 :(得分:5)

您已将未初始化的input传递给vector()并使用了它,因此您调用了未定义的行为

尝试将char *input更改为char *input = ""

同时删除free(chars);,否则您将遇到双重免费问题。

答案 2 :(得分:5)

我认为你有一个或多个未初始化的字段。当我尝试编译时,我收到了这些警告:

$ clang -Weverything vector.c 
vector.c:15:18: warning: implicit conversion loses integer precision: 'int' to 'char' [-Wconversion]
        char i = getchar();                         //get input
             ~   ^~~~~~~~~
vector.c:19:31: warning: variable 'input' may be uninitialized when used here [-Wconditional-uninitialized]
            input = vector(i, input);               //call vector
                              ^~~~~
vector.c:11:16: note: initialize the variable 'input' to silence this warning
    char *input, *out = "Input: ";
               ^
                = NULL
vector.c:40:33: warning: implicit conversion changes signedness: 'int' to 'unsigned long' [-Wsign-conversion]
    chars = (char*)calloc(length+2, sizeof(char));  //allocate more memory
                   ~~~~~~ ~~~~~~^~
vector.c:38:18: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
    int length = strlen(newlist);                   //determine length of newlist(input)
        ~~~~~~   ^~~~~~~~~~~~~~~
vector.c:5:7: warning: no previous extern declaration for non-static variable 'chars' [-Wmissing-variable-declarations]
char *chars;
      ^
5 warnings generated.

当我使用ASan what is ASan时,我收到以下错误:

$ echo 1 2 3 | ./a.out 
Enter characters: 
=================================================================
==23718==ERROR: AddressSanitizer: attempting double-free on 0x60200000ef70 in thread T0:
    #0 0x4a5f4b in free /home/development/llvm/3.7.0/final/llvm.src/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:30:3
    #1 0x4cd631 in main (/home/brian/src/so/a.out+0x4cd631)
    #2 0x7f3b94ef5a3f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x20a3f)
    #3 0x4174c8 in _start (/home/brian/src/so/a.out+0x4174c8)

0x60200000ef70 is located 0 bytes inside of 7-byte region [0x60200000ef70,0x60200000ef77)
freed by thread T0 here:
    #0 0x4a5f4b in free /home/development/llvm/3.7.0/final/llvm.src/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:30:3
    #1 0x4cd5fa in main (/home/brian/src/so/a.out+0x4cd5fa)
    #2 0x7f3b94ef5a3f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x20a3f)

previously allocated by thread T0 here:
    #0 0x4a63b4 in calloc /home/development/llvm/3.7.0/final/llvm.src/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:56:3
    #1 0x4cd67c in vector (/home/brian/src/so/a.out+0x4cd67c)
    #2 0x4cd57b in main (/home/brian/src/so/a.out+0x4cd57b)
    #3 0x7f3b94ef5a3f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x20a3f)

SUMMARY: AddressSanitizer: double-free /home/development/llvm/3.7.0/final/llvm.src/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:30:3 in free
==23718==ABORTING

答案 3 :(得分:1)

缺少char *的初始化,因此导致未定义的行为。请初始化char *

答案 4 :(得分:0)

你需要删除free(print)并分配给pointers.First调用double free,最后导致core dumped.I在ubuntu上工作,我的gcc版本是4.8.4