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 回答
1
int *EnemyLife(int level) // here  you return  a pointer
{

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

    for (int i = 0; i < level; ++i)
    {
        RandTime = rand() % 100;
        *(ptr+i) = RandTime;         // keep ptr pointer the start address
    }

    return ptr;
}

阿斯洛:

  int* (*PtrEnemyLife) (int) = EnemyLife;

  ptr = (*PtrEnemyLife)(amount);
  Or just:
  ptr = PtrEnemyLife(amount);

free (ptr);在 main 末尾添加

于 2013-08-16T01:57:41.747 回答
0

以下是您做错的事情:

  1. 你必须返回一个指针,但你要返回你初始化的第一个指针的内容(这不是非法的,但我不认为这是你想要做的)相反你应该写return ptr;

  2. 当您传递amountenemylife函数时,它不是要在屏幕上打印的元素数,而是要存储在分配空间中的整数数。

  3. 在 main 函数中,您只是打印第一个初始化指针的内容。

  4. 该功能ptrEnemyLife只是内存空间的腰部。

于 2013-08-16T02:12:55.117 回答