char ***的动态内存分配

时间:2017-04-02 22:33:30

标签: c dynamic-memory-allocation realloc calloc

我试图为char ***动态分配内存。

char*** tas;

使用char ***我想描述一个只包含2列的字符串表。

我试图实现的主要问题不是编译程序,而是让它在没有分段错误的情况下运行。

我认为问题是realloc的使用不当。 检查realloc / calloc / malloc的手册页

void *realloc(void *ptr, size_t size);
void *malloc(size_t size);
void *calloc(size_t nmemb, size_t size);

我发现我必须生成适当的size_t变量来发送分配函数。

关键代码如下:

tas         = (char ***)realloc(tas,sizeof(char **)*(i+1));
tas[i]      = (char **)calloc(2,sizeof(char *));
tas[i][0]   = (char *)calloc(lenKey+1,sizeof(char));
tas[i][1]   = (char *)calloc(lenValue+1,sizeof(char));
++i;

tas使用char ***。

-------- EDIT -------

我一块一块地编译了一个答案,我发布了我在这里找到问题的解决方案。

3 个答案:

答案 0 :(得分:1)

这不是那么难,你只需要保持清醒。

首先,请记住一般模式:要分配类型为n的{​​{1}}元素数组,请使用:

T

然后在T * p = malloc(n * sizeof(T)); 范围内初始化p[i]的值i,然后在完成后取消初始化元素(如有必要),然后释放数组分配:

[0, n)

现在只需对你的字符串数组进行递归处理,记住字符串是一个字符数组,free(p); 行的m列字符串:

n

在回来的路上,释放一切:

// allocate the table (the row pointers)
char *** tas = malloc(m * sizeof(char**));

for (size_t i = 0; i != m; ++i)
{
    // allocate each row (the row cells)
    tas[i] = malloc(n * sizeof(char*));
    for (size_t j = 0; j != n; ++j)
    {
        // initialize the row cell by allocating the string
        tas[i][j] = /* allocate string */
    }
}

答案 1 :(得分:0)

char ***就像一个字符串表。

      R\C  ||---1-----|----2----|   
   |---1---||string11 |string12 |
   |---2---||string21 |string22 |
   |---3---||string31 |string32 |
   | ....  ||  .....     ...... |
   |-(N-1)-||str N-1.1|str N-1.2|
   |---N---||stringN1 |stringN2 |

这是一个包含注释的演示源代码,以便了解如何实现这样的目标:

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
void rellocateFunction(size_t lenKey, size_t lenValue);
void firstAllocateFunction(size_t lenKey, size_t lenValue);
int i=0;
char*** tas;

int main(){
    int j=0;
    size_t length1, length2; 
    char* stringN1=(char *)malloc(10*sizeof(char));
    char* stringN2=(char *)malloc(10*sizeof(char));
    while(1){
        printf("dragons\n");
        printf("MAIN: Enter string[%d][0]:",j); scanf("%s", stringN1);
        printf("MAIN: Enter string[%d][1]:",j); scanf("%s", stringN2);
        length1 = strlen(stringN1); // You need the length of the string to pass it as an argumnet to allocation functions
        length2 = strlen(stringN2); // Same as above comment
        if(j==0) firstAllocateFunction(length1,length2); // We cant start with the use of realloc. So here you are initiliazing the allocation with calloc only.
        else rellocateFunction(length1,length2); // When we already have entries in the table, we are going to use this function to allocate more memory for the new entries.
        strcpy(tas[j][0],stringN1);  // Population of char*** at row j and col 0
        strcpy(tas[j][1],stringN2);  // Population of char*** at row j and col 1
        j++;
        for (int c=0; c<j; c++) // Print the results
        {
            printf("tas[%d][0]:<%s>\n",c, tas[c][0]);
            printf("tas[%d][1]:<%s>\n",c, tas[c][1]);
        }
    } // Notice the forever ongoing loop... That's to test if the code works. Cancel the execution with ctrl+c
    return 0;
}

void firstAllocateFunction(size_t len1, size_t len2){ 
tas         = (char ***)calloc(1,sizeof(char **)); //One char*** pointer
tas[i]      = (char **)calloc(2,sizeof(char *));   //pointin to 2 char** 
tas[i][0]   = (char *)calloc(len1+1,sizeof(char)); //containing len1+1 
tas[i][1]   = (char *)calloc(len2+1,sizeof(char)); //and len2+1 elements of sizeof(char) bytes.
++i;
}

void rellocateFunction(size_t len1, size_t len2){
tas         = (char ***)realloc(tas,sizeof(char **)*(i+1)); //One more char***
tas[i]      = (char **)calloc(2,sizeof(char *));   ////////////////////
tas[i][0]   = (char *)calloc(len1+1,sizeof(char)); // Same as above //
tas[i][1]   = (char *)calloc(len2+1,sizeof(char)); ///////////////////
++i;
}

我使用了calloc,因为我想避免使用memset来初始化字符串。其余部分用评论解释。

请注意,您必须首次使用不同的函数()为char ***分配内存,而不是基本上重新分配char ***内存的其余时间。

警告。上面的代码没有实现free()函数(强烈建议使用它。所以注意不要用死数据填满你的内存。

答案 2 :(得分:0)

您的初始分配应使用malloccalloc代替realloc;否则,您的初始指针参数必须为NULL

char ***tas = malloc( (i+1) * sizeof *tas );

char ***tas = calloc( i+1, sizeof *tas );

char ***tas = realloc( NULL, (i+1) * sizeof *tas );

char ***tas = NULL;
tas = realloc( tas, (i+1) * sizeof *tas );

否则你会非常接近 - 只是失去无关的演员阵容。

tas[i] = calloc( 2 * sizeof *tas[i] );

tas[i][0] = calloc( lenKey + 1, sizeof *tas[i][0] );
tas[i][1] = calloc( lenValue + 1, sizeof *tas[i][1] );

与往常一样,检查每次*alloc来电的结果。