gpt4 book ai didi

c - 指针和内存池 : Simple Test Program

转载 作者:行者123 更新时间:2023-12-04 02:48:35 27 4
gpt4 key购买 nike

我只是想自学如何创建、改变传递指针以及使用内存池中的内存块。我试图在下面的程序中返回一个指向内存池中的内存块的指针(我 malloced),但是它给了我一个错误。如果有人能通过解释我的错误为我指出正确的方向,告诉我如何解决它(或引导我朝着正确的方向前进),那就太棒了!

这是我现在的代码(下面是我的错误消息):

#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]

最佳答案

这里有不少错误。我在代码片段中添加了注释,以解释我在每个步骤中所做的事情,将其与您的原始代码进行比较,看看我为什么这样做。以下是我认为您正在尝试做的事情(试图遵循您的逻辑):

#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;
}

关于c - 指针和内存池 : Simple Test Program,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18264509/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com