使用NULL数组将内存分配给2D数组(c) [英] Allocating memory to 2D array using an array of NULL (c)
问题描述
感谢您抽出宝贵的时间阅读本文。
在我的问题中,向量定义为一维整数数组。
因此,向量数组应为
我被要求使用:
int **向量-2D数组
int size-一个整数,表示** vectors中有多少个向量
int * sizes-一个一维整数数组,表示向量的长度
thanks for taking the time in reading this.
In my question a "vector" is defined as a 1D dimensional array of integers.
Therefore an array of vectors would be a 2D dimensional array in which every vector can be of a different length.
I'm asked to use:
int** vectors- the 2D array
int size -an integer that represents how many vectors exist inside **vectors
int* sizes-a 1D array of integers that represents the length of the vectors
例如,对于:
向量= {{4,3,4,3},{11,22,33,44,55,66},NULL,{5},{ 3,33,333,33,3}}。
的大小为5(向量内有5个向量)。
的大小为{4,6,0,1,5}( 4是第一个向量的长度,依此类推。)
大小由用户在main()的开头输入,并且** vectors和amp; * sizes是动态分配的,具有size的值。
for example,for:
vectors = {{4,3,4,3},{11,22,33,44,55,66},NULL,{5},{3,33,333,33,3}}.
size is 5 (there are 5 vectors inside vectors).
sizes is {4,6,0,1,5} (4 is the length of the first vector and so on).
size is inputted by the user at the beginning of main() and **vectors&*sizes are dynimacilly allocated with size's value.
我被要求编写函数:
int init(int *** vectors,int ** sizes,int size)初始化* * vectors为NULL数组,* vectors为零数组。
我想出了以下代码:
I'm asked to write the function:
int init(int ***vectors, int **sizes, int size) which initializes **vectors to be an array of NULLs and *sizes to be an array of zeros.
I came up with this code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int init(int*** vectors, int** sizes, int size)
{
int i, k,j;
printf("check\n");
*vectors = (int**)malloc(size * sizeof(int*));
if (*vectors == NULL)
return 0;
for (i = 0; i < size; i++)
{
*(vectors + i) = NULL;
}
printf("check 2\n");
for (k = 0; k<size; k++)
{
if (*(vectors+k) != NULL)
printf("didn't work\n");
else
printf("current is null\n");
}
*sizes= (int*)malloc(size * sizeof(int));
if (*sizes == NULL)
return 0;
for (j= 0; j < size; j++)
{
*(sizes + j) = 0;
printf("%d ", *(sizes + j));
}
printf("\n");
return 1;
}
int main()
{
int size, i;
int** vectors = NULL;
int* sizes = NULL;
printf("\nPlease enter an amount of vectors:\n");
scanf("%d", &size);
printf("%d\n", init(&vectors, &sizes, size));
printf("size is %d now\n", size);
// for (i = 0; i < size; i++)
// printf("%d ", *(sizes+i));
printf("check 3\n");
free(sizes);
free(vectors);
printf("check 4\n");
printf("check 5\n");
return 0;
}
忘了说,如果init分配内存失败,它将返回0,否则返回1
打印支票是为了让我可以看到程序在哪里失败。
问题是无论如何,在打印最后一张支票(支票5)$ b $之后b程序失败。(运行时检查失败2)
如果有人可以帮助我了解我在做什么错,我将非常感激。
非常感谢您的阅读并度过了美好的一天
编辑:
i还在init内打印了数组大小/向量,只是看它是否打印零/空值,我实际上不需要这样做。
forgot to mention that init returns 0 if it fails to allocate memory and 1 otherwise.
printing the "checks" was so I could see where the program fails.
the problem is that no matter what,after printing the last check (check 5)
the program fails.(Run-Time Check Failure #2)
if anyone could help me understand what I'm doing wrong I would HIGHLY appreciate it.
thanks alot for reading and have an amazing day.
edit:
i also printed the array sizes/vectors inside init just to see if it prints zeros/nulls,i don't actually need to do it.
推荐答案
OP代码的一个问题是指针算法。给出:
One problem of OP's code is in the pointer arithmetic. Given:
int ***vectors;
*vectors = malloc(size * sizeof(int*));
此循环:
for (i = 0; i < size; i++)
{
*(vectors + i) = NULL;
}
将迭代下一个未分配的 pointer指针 strong>转换为int,而OP所需的是
Would iterate over the next unallocated pointer to pointer to pointer to int, while what the OP needs is
for (i = 0; i < size; i++)
{
*(*vectors + i) = NULL; // or (*vectors)[i] = NULL;
}
在以下循环中也是如此,其中 * (sizes + j)
代替 *(* sizes + j)
(或(* sizes)[j ]
)。
The same holds in the following loops, where *(sizes + j)
is used instead of *(*sizes + j)
(or (*sizes)[j]
).
这篇关于使用NULL数组将内存分配给2D数组(c)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!