English 中文(简体)
动态矩阵中的问题处理点
原标题:Problems handling pointers in a dynamic matrix

我与点人交战: 我试图发挥一种类似于热情回报的职能。 我指的是,我想把命令分成几句,每一句由不同的点人指明,以便通过方括号加以提及。 (e:命令[0],命令3.1)。

我想就几句话说几句,以便我使用二点名记忆。 为了分立命令,我使用斜体,正确操作。 问题在于:

在转稿**中,我保留对每个字眼点,即字眼点的实时记忆,而我则做以下工作:

  • I increase the size of resul
  • I reserve memory for the pointer to the token
  • I asign the token

(案文用于制止标记衰减。)

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

int main(int argc, char *argv[]){
    char linea[] = "send UDP 4500 50";
    char **resul=NULL;
    int numTokens,conta2;
    char *sep=" ";//character which divides the orders
    int conta=0;  //counter of the number of tokens found
    char *saveptr,*token;

    for(token=strtok_r(linea,sep,&saveptr); token!=NULL; token=strtok_r(NULL,sep,&saveptr)){
        printf("token: %s
",token);
        printf("size: %i
",(conta+1)*sizeof(char*));
        resul = (char **)realloc(resul,(conta+1)*sizeof(char*)); //Increase the size for the array of pointers

        resul[conta]= malloc(sizeof(char*)); //Reserve size for the pointer
        resul[conta]=token; //Asign word to the pointer
        conta++;
    }
    //Print the results
    for(conta2=0;conta2<conta;conta2++)
        printf("resul: "%s"
",resul[conta2]);
    //free memory
    for(conta2=0;conta2<conta;conta2++){
        printf("liberando: %i
",conta2);
        free(resul[conta2]);
    }
    free(resul);
    return(0);
}

产出如下:

$ ./lectura_consola 
token: send
size: 4
token: UDP
size: 8
token: 4500
size: 12
token: 50
size: 16
resul: "send"
resul: "UDP"
resul: "4500"
resul: "50"
liberando: 0
*** glibc detected *** ./lectura_consola: free(): invalid pointer: 0xbffb7f1b ***

If I run it with valgrind in order to see the failures in memory, in the free(resul[0]) step happens that:

liberando: 0
==8372== Invalid free() / delete / delete[]
==8372==    at 0x40257ED: free (vg_replace_malloc.c:366)
==8372==    by 0x80489FA: main (lectura_consola.c:92)
==8372==  Address 0xbee8e28b is on thread 1 s stack

有了这一法典,该方案就能够正确地节省和印刷这些词语,但在解放记忆的时候,以及在试图自由转播时,它说: Invalid Pointer。

你们能否告诉我错误在哪里? I m 完全消失,任何帮助都是有益的。

最佳回答

这里的问题是,你再次试图忘却你没有分配的记忆。 您的首例:

resul[conta]= malloc(sizeof(char*)); //Reserve size for the pointer
resul[conta]=token; //Asign word to the pointer

第二项声明超越了第一次发言的结果,因此,当你打电话free(resul[conta]时,你又试图免除token(该编码为示意图)而不是你所分配的指点。

无论如何,你都不需要首先发言。 页: 1 该电话是malloc, 仅分配sizeof (char*) bytes to no good effect (i.e. wasting memory). 既然价值只是虚构的,那么你最终就会泄露这一记忆。

仅删除关于<代码>小型的呼吁,而你的代码应当发挥作用。

问题回答

只要我记得,在象征性之间仅仅分配记忆,这意味着你只读密码记忆——送UDP 4500 50”就是这样。





相关问题
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 ...

热门标签