简体   繁体   English

二维矩阵在C中,realloc不起作用

[英]2-D Matrix In C, realloc not work

I have a little problem, first of all, sorry for my English, secondly, I'm making a matrix 2-D, but i have a problem with the work memory, I don't understand why it doesn't work. 我有一点问题,首先,对不起我的英语,其次,我正在制作2-D矩阵,但我的工作记忆存在问题,我不明白为什么它不起作用。 The code is written in C. I want to set one or more strings and save into an array, for this i need to increase the array's capacity dynamically. 代码用C语言编写。我想设置一个或多个字符串并保存到数组中,为此我需要动态增加数组的容量。

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

Create a Struct 创建一个Struct

typedef struct {
 int rows;
 int col;
 int flagPosition;
 int wordBiggest;
 int tamanioString;
} matrixTest;

int insertString( char **matriz );
void rellenarMatriz( char **matriz, char string[], matrixTest *datosMatriz );
int getElementBiggest( char **matriz, int size );

Main 主要

int main()
{
char *stringg = NULL;

insertString( &stringg );

free( stringg );
return 0;
}

In the next function, i initialize the struct and i'm loading the string, this string then i send to another function called 'rellenarMatriz'. 在下一个函数中,我初始化结构,我正在加载字符串,然后我发送到另一个名为'rellenarMatriz'的函数。 Until here it's all fine. 直到这里一切都很好。

int insertString( char **matriz ){

char __string[300];
int i = 0;
int sizeRow = 0;
matrixTest datosMatriz = { 1,1,0,0,0 };

matriz = (char **) calloc( datosMatriz.rows, sizeof(char *));

for (i = 0; i < datosMatriz.rows; i++)
    matriz[i] = (char*)calloc(datosMatriz.col, sizeof(char));

puts( "User say: " );

while( fgets( __string, 300, stdin ) ){
    puts( "User say: " );
    i = 0;
    sizeRow = 1;

    while( i < strlen( __string ) - 1  ){
        if( __string[i] == ' ' )
            sizeRow++;
        i++;
    }

    datosMatriz.tamanioString = strlen( __string ) - 1;
    datosMatriz.rows = datosMatriz.rows + sizeRow;
    datosMatriz.flagPosition = sizeRow;

    rellenarMatriz( matriz, __string, &datosMatriz );
}

return 0;
}

Here is the problem.. Look at the realloc and calloc functions. 这是问题..看看realloc和calloc函数。 I'm going crazy 我要疯了

void rellenarMatriz( char **matriz, char string[], matrixTest *datosMatriz){

int i = 0,
    j = 0;

datosMatriz->wordBiggest = getElementBiggest( &string, datosMatriz->tamanioString );

if( datosMatriz->col < datosMatriz->wordBiggest ){

    (*matriz) = realloc( (*matriz), (datosMatriz->wordBiggest) * sizeof( char * ) );

    for( i = datosMatriz->col; i < datosMatriz->wordBiggest; i++ ){
        matriz[i] = calloc( 1, sizeof( char ));
    }
    datosMatriz->col = datosMatriz->wordBiggest;
}

for( i = 0; i < datosMatriz->col; i++ )
    (*matriz) = realloc( matriz[i], ( datosMatriz->rows ) * sizeof( char * ) );

for( i = 0; i < datosMatriz->col; i++ ){
    for( j = datosMatriz->rows-datosMatriz->flagPosition; j < datosMatriz->rows; j++ )
        matriz[i][j] = calloc( 1, sizeof( char )); // Here is the biiig problem. Warning: assignment makes integer from pointer without a cast|
}

for( i = 0; i < datosMatriz->rows; i++ ){
    for( j = 0; j < datosMatriz->col; j++ ){
        printf( "%d and %d\n", i, j );
        matriz[i][j] = 'a';
    }
}

for( i = 0; i < datosMatriz->rows; i++ ){
    puts( "\t" );
    for( j = 0; j < datosMatriz->col; j++ )
        printf( "[%c]", matriz[i][j] );
    puts( "\n" );
}
}

This doesn't matter 这没关系

int getElementBiggest( char **matriz, int size ){

int i = 0,
    biggest = 0,
    countWord = 0;

for( i = 0; i < size; i++ ){
    if( (char)(*matriz)[i] == ' ' ){
        if( countWord > biggest )
            biggest = countWord;
        countWord = 0;
    }
    else
        countWord++;
}
return biggest;
}

Also if you find a mistake in my redaction in english, please tell me, i'm studing that and would be so good that you correct my syntax. 另外,如果你发现我的英语编辑错误,请告诉我,我正在研究这个问题并且会很好,以至于你纠正了我的语法。

this line: 这一行:

matriz[i][j] = calloc( 1, 1 );

has the target: matriz[i][j] referencing a single character in the array matrix[i] 有目标: matriz[i][j]引用数组matrix[i]的单个字符matrix[i]

That is why the compiler is outputting a warning message 这就是编译器输出警告消息的原因

Also, note; 另外,请注意; this code block: 这段代码:

for( i = 0; i < datosMatriz->col; i++ )
    (*matriz) = realloc( matriz[i], ( datosMatriz->rows ) * sizeof( char * ) );

is re-allocating the same pointer over and over and over. 一遍又一遍地重新分配相同的指针。

Also, note: this line: 另外,请注意:此行:

free( stringg );

only free's the first array, not all the other arrays created by calloc() 只有free是第一个数组,而不是calloc()创建的所有其他数组

Also, note: this line: 另外,请注意:此行:

char *stringg = NULL;

is declaring a single pointer, but it is being used as if it were declared via: 声明一个指针,但它被用作通过以下方式声明:

char **stringg = NULL;

and the problem line in the code is treating it as if it were declared via: 并且代码中的问题行将其视为通过以下方式声明:

char ***stringg = NULL;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM