3

パスポインターを作成、変更し、メモリプールからメモリブロックを使用する方法を自分自身に教えようとしています。以下のプログラムで私がしようとしているのは、(私がmalloc編集した) メモリ プールからメモリ ブロックへのポインタを返すことですが、エラーが発生しています。誰かが私のエラーを説明し、それを修正する方法を示して正しい方向に向けることができれば (または正しい方向に導くことができれば)、それは素晴らしいことです!

これが私が今持っているコードです(さらに下に私のエラーメッセージがあります):

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>

int EnemyLife(int level)
{
    int RandTime;
    int *ptr;
    srand(time(NULL));
    ptr = (int*) malloc(sizeof(int)*level);

    for (int i = 0; i < level; ++i)
    {
        RandTime = rand() % 100;
        *ptr++ = RandTime;
    }

    return *ptr;
};

int main(void)
{
    int Ammount, RandValue;
    int (*PtrEnemyLife) (int) = EnemyLife;

    printf("Ammount of random number printed to the screen?\n");

    scanf("%d", &Ammount);

    int *ptr;

    *ptr = (*PtrEnemyLife) (Ammount);
    printf("%d\n", *ptr);

    return 0;
}

-Wall...そしてこれは、ピアがフラグを使用してコンパイルするようにアドバイスした後に発生するエラーです。

/home/definity/Desktop/Cloud/code/rand.c: In function ‘main’:
/home/definity/Desktop/Cloud/code/rand.c:39:7: warning: ‘ptr’ is used uninitialized in this function [-Wuninitialized]
/home/definity/Desktop/Cloud/code/rand: In function `EnemyLife':
/home/definity/Desktop/Cloud/code/rand.c:8: multiple definition of `EnemyLife'
/tmp/ccvZvKYA.o:/home/definity/Desktop/Cloud/code/rand.c:8: first defined here
/home/definity/Desktop/Cloud/code/rand: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here
/home/definity/Desktop/Cloud/code/rand: In function `data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here
/home/definity/Desktop/Cloud/code/rand: In function `data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-linux-gnu/4.7/crtbegin.o:(.data+0x0): first defined here
/home/definity/Desktop/Cloud/code/rand:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
/home/definity/Desktop/Cloud/code/rand: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o:(.text+0x0): first defined here
/home/definity/Desktop/Cloud/code/rand: In function `main':
/home/definity/Desktop/Cloud/code/rand.c:28: multiple definition of `main'
/tmp/ccvZvKYA.o:/home/definity/Desktop/Cloud/code/rand.c:28: first defined here
/home/definity/Desktop/Cloud/code/rand: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/x86_64-linux-gnu/4.7/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
/home/definity/Desktop/Cloud/code/rand:(.data+0x10): first defined here
/usr/bin/ld: error in /home/definity/Desktop/Cloud/code/rand(.eh_frame); no .eh_frame_hdr table will be created.
collect2: error: ld returned 1 exit status
[Finished in 0.1s with exit code 1]
4

3 に答える 3

3

ここにはかなりの数のエラーがあります。コードのスニペット内にコメントを追加して、各ステップで何を行っているかを説明し、それを元のコードと比較して、なぜそうしたのかを確認します。以下は、あなたがやろうとしていると私が思うことです(あなたの論理に従おうとしています):

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>

int *enemyLife( int const level ) {
  // Requests a block of memory of size int * level from the 
  // memory pool.
  int *ptr = malloc( sizeof( int ) * level );

  // Determines if we ran out of memory from the memory pool. Important 
  // to always check the result from a system call.
  if ( ptr == NULL ) {
    exit( EXIT_FAILURE );
  }

  srand( time( NULL ) );

  // Iterates level times and stores a random number in the pointer ptr 
  // at the ith position.
  for ( int i = 0; i < level; i++ ) {
    ptr[ i ] = ( rand() % 100 ); // Side: ptr[ i ] => *( ptr + i )
  }

  // Returning a POINTER to an integer.
  return ptr;
}

int main( void ) {
  int amount;

  printf( "Amount of random numbers printed to the screen?\n" );
  scanf( "%d", &amount );

  // Defining a pointer to an integer ptr and storing the result from 
  // enemyLife in the pointer. Passing "amount" because we want that many 
  // numbers.
  int *ptr = enemyLife( amount );

  printf( "Outputting those random values.\n" );

  // Iterate over every position in the returned pointer to get each random 
  // number. Output it to stdout.
  for ( int i = 0; i < amount; i++ ) {
    printf( "%d\n", ptr[ i ] );
  }

  // Free the memory block that ptr points to. We no longer need it.
  free( ptr );

  return 0;
}
于 2013-08-16T01:42:50.417 に答える
0

ここにあなたが間違っていることがあります:

  1. ポインターを返す必要がありますが、初期化した最初のポインターの内容を返しています (違法ではありませんが、これがあなたのやりたいことだとは思いません) 代わりに、return ptr;

  2. 関数に渡すamountenemylife、画面に表示される要素の数ではなく、割り当てられたスペースに格納される int の数になります。

  3. メイン関数では、最初に初期化されたポインターの内容を出力しているだけです。

  4. 関数ptrEnemyLifeはメモリ空間のほんの一部です。

于 2013-08-16T02:12:55.117 に答える