指针和内存池:简单的测试程序

时间:2013-08-16 01:39:04

标签: c malloc function-pointers

我只是想自学如何创建,改变传递指针并使用内存池中的内存块。我在下面的程序中尝试到的是从内存池(我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]

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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;
}

ASLO:

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

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

在主

的末尾添加free (ptr);

答案 2 :(得分:0)

以下是你错了的事情:

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

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

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

  4. 函数ptrEnemyLife只是内存空间的一部分。