English 中文(简体)
为什么取消扼杀?
原标题:Why c string is freed?
  • 时间:2012-01-12 13:22:06
  •  标签:
  • c

不熟悉C,请纠正我的任何错误。

这里的有些法典:

void db_cstr(char* cstr, int len) {
    char* temp2 = cstr;
    cstr = (char*)malloc(len*2*sizeof(char));
    // print 1
    printf(cstr);
    printf("
");
    //print 2
    printf(temp2);
    printf("
");
    strcpy(cstr, temp2);
    //free
    free(temp2);
    //print 3
    printf(cstr);
}
int somefunction(){
    int array_len = 10;
    char* cmd = (char*)malloc(array_len*sizeof(char));
    strcpy(cmd, "apple");
    db_cstr(cmd, array_len);
    // final print
    printf(cmd);
    return 1;
}

我的数值(途径)为//印刷1=“”和/印刷2=“pple”,/印刷3=“pple”。 然而,当我做最后印刷时,就没有印刷。 我假定这一点与自由(第2段)有关;因此,我评论说,最后印刷是“pple”。 我认为,这是因为某些功能的圆柱形点仍然指温2的释放阵列。 你们如何把 cm点点点指在 d中的新方体。 (我不想干).,不敢回去。)

最佳回答

如果你想要改变一个老板,你就应该通过一个要点。 即便是老兵,情况也是如此。

因此,ave db_cstr(char** cstr, int len),db_cstr(&cmd, protocol_len);,等等。

顺便提一下,不直接使用印刷品。 http://www.ohchr.org。

问题回答

www.un.org/Depts/DGACM/index_french.htm

char* temp2 = cstr;
free(temp2);

因此,最后的<代码>(>)正试图打印已经被释放的记忆,这是未界定的行为。

订立<代码>db_cstr(>的最容易方式是,新点人返回:

char* void db_cstr(char* cstr, int len) {
    ...
    printf(cstr);
    return cstr;
}

int somefunction(){
    ...
    cmd = db_cstr(cmd, array_len);
    ...
}

可以通过将第1条论点(db_cstr()变成点对点(char**)和适当修改该守则来实现类似效果。

你们应该回来。 但是,你毫无动机地指出,你不会“放弃”任何东西。 因此,我猜测你需要把这一论点作为点人,这样,你就可以改变点人的职能:

void db_cstr(char **cstr, size_t len);

Don t use f but puts in You case. 如果你在没有其他理由的情况下通过<条码>“%sx%d”至<条码>,则你的方案有不明确的行为,将坠毁。

学习使用夸张,并印刷插图的地址。 例如,你可以在几个地方发表类似于声明的声明。

fprintf(stderr, "at %s:%d cstr=%p
", __FILE__, __LINE__, cstr);

http://code>cstr 可能更适当的变称号为两倍。

或许最简单的做法是从这一职能中恢复新的记忆,而不是试图通过点子将其退回。

char* db_cstr(char* cstr, int len) {
    char* result = malloc(len);
    strcpy(result, cstr);
    free(cstr);
    return result;
}

接着,请大家说:

cmd = db_cstr(cmd, array_len);

改变这个百分点的唯一途径是把一分点推向四分五裂。

因此,你们需要重新制定这样的法典:

void db_cstr(char** cstr, int len) {
    char* temp2 = *cstr;
    *cstr = (char*)malloc(len*2*sizeof(char));
    strcpy(*cstr, temp2);
    free(temp2);
}
int somefunction(){
    int array_len = 10;
    char* cmd = (char*)malloc(array_len*sizeof(char));
    strcpy(cmd, "apple");
    db_cstr(&cmd, array_len);
    // final print
    printf(cmd);
    return 1;
}

看见db_cstr(&cmd, 阵列_len);,你正在通过点人到这里。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签