我创建了一个名为PLAYER的结构,我想创建一个列表来存储指向PLAYER对象的指针。
如果我想用完成它
PLAYER **ptr = malloc(10*sizeof(PLAYER *));
如何将指针分配给每个索引?我试过了:
PLAYER *a;
PLAYER *b;
ptr[0] = a;
ptr[1] = b;
1.这似乎奏效了。我能解释一下它后面的存储器地址吗
我也试过:
ptr = a;
//increase the address and assign b
ptr += sizeof(PLAYER *);
ptr = b;
2.我认为这不正确。我能在不使用[]括号的情况下找到分配列表的正确方法吗?
3.如果我只分配一个条目的大小并分配多个条目:
PLAYER **ptr = malloc(1*sizeof(PLAYER *));
ptr[0] = a;
ptr[1] = b;
我可以通过使用ptr[0]ptr[1]来获得这些PLAYER对象,但这会导致任何问题,比如覆盖其他内存吗?
4.如果我使用[]括号,我是否需要在每个索引处进行malloc才能使用它?
PLAYER *ptr[10];
for(int i = 0; i < 10; i++)
ptr[i] = malloc(sizeof(PLAYER *));
5.使用数组后是否需要释放它?例如:
char ptr[10] = "abc";
//do something with ptr
free(ptr);
char *ptr2[10] = {"123", "abc"};
free(ptr2);
任何帮助都将不胜感激!
如果您有PLAYER **ptr = malloc(10*sizeof(PLAYER *));
这意味着您必须对每个ptr[i] = malloc(sizeof(PLAYER));
进行malloc
访问索引处的数组将是ptr[i]->somevalue
注意:如果结构中有指针,也需要为其分配!!
解放你的记忆将是:
for(int i = 0; i<10;i++){
free(ptr[i]->anyAllocatedPointersInside);
free(ptr[i]);
}
free(ptr);
特别是按该顺序
如果你用完整的结构更新帖子,我可以更新我的结构,更准确地帮助你。
如果有疑问,请用以下术语来思考malloc()
分配:它分配原始内存,而它对您的结构一无所知!当你用这些术语思考时,你会做对的。
让我们试着回答您的问题:
-
您基本上是在堆栈中实例化一个指针,其中包含任何内容,就像
int hello;
一样。该整数可以包含任何内容,因为您没有将其设置为int hello = 2;
。同样的事情也发生在您的指针上:int * hello;
将是一个可以包含任何地址的指针(指向整数(。因此,如果你取消引用这样的指针,你被SIGSEGV捕获的机会并不低。然后,一旦创建了可以是任何东西的指针,就要将它们的地址分配给已分配的指针数组的指针。不要那样做。 -
这是不正确的,因为如果你有一个指向给定类型的指针数组,你可以简单地用
+= n
递增,编译器会计算出合适的";sizeof(type_you're-pointing_to("并将自动添加。这是声明指向给定类型的指针的主要目的。 -
您实际上正在覆盖其他内存。
-
括号只是指针取消引用:
*ptr+n
与ptr[n]
相同。 -
您需要释放每一行,然后释放指针的指针数组。基本上,你用
malloc()
得到的每个指针,都必须用free()
释放它。不要将free()
调用到尚未从malloc()
中吐出的任何其他指针。
让我给你看一些我刚刚写的代码,让你看得更好:
#include <stdlib.h>
#include <stdio.h>
#include <string.h> // for memset
#define N_POINTERS 4
#define M_PLAYERS_PER_LINE 3
struct PLAYER
{
int id;
int score;
int age;
};
int
main()
{
// Allocate the array of pointers, big enough to old N pointers.
struct PLAYER ** pointers = malloc(N_POINTERS*sizeof(struct PLAYER*));
// Always better zeroize pointers arrays.
memset(pointers, 0, N_POINTERS*sizeof(struct PLAYER *));
// Allocate each line of M `PLAYER` structs.
// Basically we allocate N chunks of memory big enough to contain M PLAYER structs one next each other.
// What we get is something like this:
//
// pointer pointers PLAYER lines
// of pointers array
// [addrP] -> [addr0] -> [PLAYER0 PLAYER1 PLAYER2] .. M
// [addr1] -> [PLAYER0 PLAYER1 PLAYER2] .. M
// ...N
//
int id = 0;
for (int i = 0; i < N_POINTERS; ++i)
{
pointers[i] = malloc(M_PLAYERS_PER_LINE*sizeof(struct PLAYER));
// Set the data you want to the structs.
for (int k = 0; k < M_PLAYERS_PER_LINE; ++k)
{
pointers[i][k].id = id++;
pointers[i][k].score = 123 + k;
pointers[i][k].age = 33 + i;
}
}
// Print data.
// Here we use a single PLAYER pointer that will
// traverse the entire PLAYER matrix.
struct PLAYER * player;
for (int i = 0; i < N_POINTERS; ++i)
{
for (int k = 0; k < M_PLAYERS_PER_LINE; ++k)
{
// Assign the current PLAYER to our pointer.
player = pointers[i] + k;
// Print PLAYER data, by reading the pointed struct.
printf("Player: #%i age:%i score:%dn", player->id, player->age, player->score);
}
}
// Deallocate!
for (int i = 0; i < N_POINTERS; ++i)
{
// Deallocate each line chunk.
free(pointers[i]);
}
// Deallocate the array of pointers.
free(pointers);
return 0;
}
作为一个额外的跟踪,如果你需要分配一个M*N PLAYER
结构的矩阵,你也应该看看这个代码,它将把M*N PLAYER
结构分配到一个唯一的内存块中,一个接一个,这更容易管理,正如你从代码本身看到的那样:
#include <stdlib.h>
#include <stdio.h>
#define LINES 4
#define COLUMNS 3
#define GET_ARRAY_POS(lin, col) (col+(lin*COLUMNS))
struct PLAYER
{
int id;
int score;
int age;
};
int
main()
{
// Allocate a *FLAT* array of PLAYER structs, big enough to
// contain N*M PLAYER structs, one next each other.
struct PLAYER * array = malloc(LINES*COLUMNS*sizeof(struct PLAYER));
// Set the data you want to the structs.
int id = 0;
for (int lin = 0; lin < LINES; ++lin)
{
for (int col = 0; col < COLUMNS; ++col)
{
int pos = GET_ARRAY_POS(lin, col);
array[pos].id = id++;
array[pos].score = 123 + col;
array[pos].age = 33 + lin;
}
}
// Print data.
// Here we use a single PLAYER pointer that will
// traverse the entire PLAYER matrix.
for (int i = 0; i < (LINES*COLUMNS); ++i)
{
// Print PLAYER data, by reading the pointed struct.
printf("Player: #%i age:%i score:%dn", array[i].id, array[i].age, array[i].score);
}
// Deallocate!
free(array);
return 0;
}
享受!^_^