Tag: 动态分配

退出前我应该释放内存吗?

因错误而退出程序时,我应该释放所有我的mallocated内存吗? something = (char**) malloc (x * sizeof(char*)); for (i = 0; i < x; i++) something[i] = (char*) malloc (y + 1); … if (anything == NULL) { printf("Your input is wrong!"); // should I free memory of every mallocated entity now? exit(1); } else { // work with mallocated entities … free(something); // […]

C中未知矩阵的Dynamica分配

我需要获取用户输入的文件并将其乘以另一个文件。 我知道该怎么做。 问题是一个文件是一个数组,另一个是矩阵。 我需要在矩阵的第一行扫描以找到矩阵的大小,然后我需要从文件中动态分配矩阵和数组。 这是我到目前为止: #include #include #include #include int main() { int row1, col1; //These values need to be pulled from the first file// char filename1[100]; //Setting the file name for entry and setting the limit to 100// FILE* fp1; //FILE must be set as a pointer (FILE must also be capitalized)// printf(“Enter file […]