1

整数の配列を生成する際に問題が発生しました。プログラムは、配列の何百ものセルを、0 から 3 までの数字で 3 つのセルではなく、-32429173 のような数字で埋めます (たとえば)。問題は、メモリの割り当て方法が間違っている可能性がありますか? ここにコードの間違った部分があります。事前に助けてくれてありがとう。

int* generate()
    {

        int maxnumb;
        int i;
        scanf_s("%d",&size);    //size of an array
        scanf_s("%d",&maxnumb); //asks for maxnumb to fill the array with random numbers from 0 to maxnumb
        int* array=(int*)calloc(size,sizeof(int));
        for (i=0;i<size;i++)
          array[i] = rand() % maxnumb + 1;
        return array;
    }

ここに完全なコードがあります

#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<stdio.h>
int size;

void swap(int* elem1,int* elem2) //swap elements
{
    int temp;
    temp=*elem1;
    *elem1=*elem2;
    *elem2=temp;
}
void bublesort(int* array,int size) //bublesort
{
    for (int j=1;j<size-1;++j)
    {
    for (int i=0;i<size-j;++i)
    {
        if ((array[i])>(array[i+1]))
        swap(&array[i],&array[i+1]);
    }

    }
}
int* enterHand() //handle entering
{

    int i;
    scanf_s("%d",&size);
    int* array=(int*)calloc(size,sizeof(int));
    for (i=0;i<size;i++)
    {
        scanf_s("%d",&array[i]);
    }
    return array;
}
int* enterFile() //entering from the file
{
    int i;
    int singlenumb;
    FILE* foo;
errno_t err;
    err=fopen_s(&foo,"input.txt","r");
    if( err == 0 )
  {
      printf( "The file 'input.txt' was opened\n" );
  }
  else
  {
      printf( "The file 'input.txt' was not opened\n" );
  }
    while (!feof(foo))  
    {
        fscanf_s(foo, "%d", &singlenumb);
        size++;
    }

    size-=1; 
    int* array=(int*)calloc(size,sizeof(int));
    rewind(foo);
    i=0;
    while (i!=size)  
    {

        fscanf_s(foo, "%d", &array[i]);
        i++;
    }
    fclose(foo);
    return array;
}
int* generate()
{

    int maxnumb;
    int i;
    scanf_s("%d",&size);    //size of an array
    scanf_s("%d",&maxnumb); //asks for maxnumb to fill the array with random numbers from 0 to maxnumb
    int* array=(int*)calloc(size,sizeof(int));
    for (i=0;i<size;i++)
      array[i] = rand() % maxnumb + 1;
    return array;
}
void putsFile(int* array, int size)
{
    int i=0;
    int k;
    FILE* fooo;
    fopen_s(&fooo,"output.txt","w");

    while (i!=size)
    {
        for (k=0; k<10; k++)
        {
            fprintf(fooo,"%d ", array[i]);
            i++;
        }
        fprintf(fooo,"\n");
    }
    fclose(fooo);
}
void printArray(int* array, int size)
{
    int i=0;
    int k;
    while (i!=size)
    {
        for (k=0; k<10; k++)
        {
            printf("%d ", array[i]);
            i++;
        }
        printf("\n");
    }
}
int main()
{
    int choice;
    int* pntr;
printf("choose a type of filling an array\n1 = handle filling\n2 = filling from the file\n3 = generating\nenter the number...\n");
scanf("%d", &choice);
switch (choice)
{
case 1: {pntr=enterHand();} break;
case 2: {pntr=enterFile();} break;
case 3: {pntr=generate();} break;
default: {pntr=NULL;} 
}
bublesort(pntr,size);
printf("choose a type of typing an array\n1 = console\n2 = file\nenter the number...\n");
scanf("%d", choice);
switch (choice)
{
case 1: {printArray(pntr, size);} break;
case 2: {putsFile(pntr, size);} break;
default: {printf("you entered the wrong number");} 
}
return 0;
}
4

3 に答える 3

0

printArray()およびは、putsFile()配列サイズの外側に出力される場合があります。

int* enterFile()sizeファイル内の数を決定する前に 0 に設定されないintため、予期しないゼロ以外のサイズが使用される可能性があります。

[編集] Wellsizeは暗黙的に 0 に設定され、グローバルな初期化されていない空間にあります。それでも、 で明示的に 0 に設定することをお勧めしenterFile()ます。

#if 0
  // The value of size is dependent on program history or uninitialized.
  while (!feof(foo)) {
    fscanf_s(foo, "%d", &singlenumb);
    size++;
  }
  size-=1; 
#else
  size = 0;  // Reset size to 0
  while (1 == fscanf_s(foo, "%d", &singlenumb)) {
    size++;
  }
#endif

提案:

void printArray(int* array, int size) {
// Also in void putsFile(int* array, int size)
  int i=0;
  while (i < size) {
    int k;
    for (k=0; k<10; k++) {
      printf("%d ", array[i]);
      i++;
      // Add
      if (i >= size) break;
    }
    printf("\n");
  }
}
于 2013-10-27T23:28:13.753 に答える