C 我总是犯错
我需要将名为C 我总是犯错,c,arrays,segmentation-fault,malloc,C,Arrays,Segmentation Fault,Malloc,我需要将名为vetor1、vetor2、vetor3的3个数组添加到名为convento的大数组中,但我在运行代码时不断遇到分段错误 我使用函数iniciaiza创建一个50位数组,并用0填充该数组。然后,在函数read中,我将读取一个数组(数组的大小通常为3),在函数add中,我需要将三个数组复制到我使用malloc创建的数组中。最后,我需要打印三个数组和一个包含所有三个数组副本的数组 #include <stdio.h> #include <stdlib.h> in
vetor1
、vetor2
、vetor3
的3个数组添加到名为convento
的大数组中,但我在运行代码时不断遇到分段错误
我使用函数iniciaiza
创建一个50位数组,并用0填充该数组。然后,在函数read
中,我将读取一个数组(数组的大小通常为3),在函数add中,我需要将三个数组复制到我使用malloc
创建的数组中。最后,我需要打印三个数组和一个包含所有三个数组副本的数组
#include <stdio.h>
#include <stdlib.h>
int *conjunto;
int *vetor1, *vetor2, *vetor3, n1, n2, n3;
int tam = 50;
void inicializa (int **tconj, int tam)
{
int i;
*tconj = (int *) malloc (tam * sizeof(int));
for (i = 0; i < tam; i++)
{
tconj[i] = 0;
}
}
void read (int **vec, int *n)
{
int i;
printf("size of array: ");
scanf ("%d", n);
printf("array: \n");
*vec = (int *) malloc (*n * sizeof(int));
for (i = 0; i < *n; i++)
{
scanf ("%d", &(*vec)[i]);
}
}
void add (int *conjunto, int *vetor1, int *vetor2, int *vetor3, int n1, int n2, int n3)
{
int i, j, k, w;
int fim1 = (n1 + n2);
int fim2 = (n1 + n2 + n3);
for (i = 0; i < n1; i++)
{
conjunto[i] = vetor1[i];
}
for (j = n1; j < fim1; j++)
{
conjunto[j] = vetor2[j];
}
for (k = fim1; k < fim2; k++)
{
conjunto[k] = vetor3[k];
}
}
void print_array (int *vec, int n)
{
int i;
printf("array: ");
for (i = 0; i < n; i++)
{
printf("%d ", vec[i]);
}
printf("\n");
}
int main()
{
inicializa (&conjunto, tam);
read (&vetor1, &n1);
read (&vetor2, &n2);
read (&vetor3, &n3);
print_array (vetor1, n1);
print_array (vetor2, n2);
print_array (vetor3, n3);
add (conjunto, vetor1, vetor2, vetor3, n1, n2, n3);
print_array (conjunto, tam);
return 0;
}
#包括
#包括
int*concento;
int*vetor1、*vetor2、*vetor3、n1、n2、n3;
int-tam=50;
无效inicializa(内部**tconj,内部tam)
{
int i;
*tconj=(int*)malloc(tam*sizeof(int));
对于(i=0;i
在您的函数中添加您正在阅读的vetor2[j]代码>和<代码>vetor3[k]代码>但是j
和k
可能超出您正在阅读的vetor2[j]函数中的vetor
的范围代码>和<代码>vetor3[k]
但是j
和k
可能超出vetor
的范围您的初始化功能几乎很好,只是缺少一个小星星:
void inicializa (int **tconj, int tam)
{
int i;
*tconj = (int *) malloc (tam * sizeof(int));
for (i = 0; i < tam; i++)
{
(*tconj)[i] = 0;
}
}
在程序结束时,在返回之前
,您应该添加:
free(conjunto);
free(v1);
free(v2);
free(v3);
编辑:我在add()
中遗漏了另外两个bug
void add(int*concento,int*vetor1,int*vetor2,int*vetor3,int n1,int n2,int n3)
{
int i,j,k;
int fim1=(n1+n2);
int fim2=(n1+n2+n3);
对于(i=0;i
编辑2:如果用户添加有趣的值(如零或负大小数组),它也将不起作用,我将这些控件留给您。您的初始化功能几乎很好,只是缺少一个小星星:
void inicializa (int **tconj, int tam)
{
int i;
*tconj = (int *) malloc (tam * sizeof(int));
for (i = 0; i < tam; i++)
{
(*tconj)[i] = 0;
}
}
for (i = 0; i < tam; i++)
{
// This is not doing what you think it is.
// You are setting some memory locations at and after tconj to zero,
// which is not the array you just allocated.
// The pointer to array that you just allocated is now 0
tconj[i] = 0; // pretty sure you mean *tconf[i]
}
在程序结束时,在返回之前
,您应该添加:
free(conjunto);
free(v1);
free(v2);
free(v3);
编辑:我在add()
中遗漏了另外两个bug
void add(int*concento,int*vetor1,int*vetor2,int*vetor3,int n1,int n2,int n3)
{
int i,j,k;
int fim1=(n1+n2);
int fim2=(n1+n2+n3);
对于(i=0;i
编辑2:如果用户添加有趣的值(如零或负大小数组),它也将不起作用,我将这些控件留给您。for(I=0;Ifor (i = 0; i < tam; i++)
{
// This is not doing what you think it is.
// You are setting some memory locations at and after tconj to zero,
// which is not the array you just allocated.
// The pointer to array that you just allocated is now 0
tconj[i] = 0; // pretty sure you mean *tconf[i]
}
{
//这不是在做你认为的事情。
//您正在将tconj前后的一些内存位置设置为零,
//这不是您刚才分配的数组。
//您刚才分配的数组指针现在为0
tconj[i]=0;//很确定您的意思是*tconf[i]
}
用于(i=0;i
那么segfault究竟什么时候发生?未使用的变量“w”[-Werror=unused variable]
当我运行程序时,我可以读取所有三个数组并打印它们,但代码没有复制。我忘了删除w。试图理解初始化应该做什么…这是否意味着你现在修复了你的程序?那么segfault到底什么时候发生?未使用的变量“w”[-Werror=unused variable]
当我运行程序时,我可以读取所有三个数组并打印它们,但代码没有制作副本。我忘了删除w。试图理解初始化应该做什么…这是否意味着您现在修复了程序?谢谢,现在我没有出现分段错误:)谢谢,现在我没有出现分段错误:)