SDL Surfaces 数组在一定数量的重新分配后更改内容
Array of SDL Surfaces changes contents after a certain number of reallocations
最近我一直在尝试学习 SDL,这是一个用于 C 的图形库。我还没有走得太远,但是我从 this tutorial 那里学到了基础知识(是的,我知道它适用于 C++,但似乎大部分内容仍然相同),我尝试创建一个 "template" SDL 程序来启动我所有其他未来的程序。这是我的:
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
int initProg(SDL_Window **window, SDL_Surface **surface, char *name, int x, int y, int w, int h, int flags);
void mainLoop(SDL_Window *window, SDL_Surface *surface);
void closeProg(SDL_Window *window);
SDL_Surface *loadImage(char *path);
int main(int argc, char *args[]) {
SDL_Window *window = NULL;
SDL_Surface *surface = NULL;
char windowName[13] = "SDL Tutorial";
int windowXPos = SDL_WINDOWPOS_UNDEFINED;
int windowYPos = SDL_WINDOWPOS_UNDEFINED;
int windowWidth = 600;
int windowHeight = 600;
int flags = SDL_WINDOW_SHOWN;
if (!initProg(&window, &surface, windowName, windowXPos, windowYPos, windowWidth, windowHeight, flags)) {
return 1;
}
mainLoop(window, surface);
closeProg(window);
return 0;
}
int initProg(SDL_Window **window, SDL_Surface **surface, char *name, int x, int y, int w, int h, int flags) {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
printf("Failed to initialize SDL.\nError: %s\n", SDL_GetError());
return 0;
} else {
*window = SDL_CreateWindow(name, x, y, w, h, flags);
if (*window == NULL) {
printf("Failed to create a window.\nError:%s\n", SDL_GetError());
return 0;
} else {
*surface = SDL_GetWindowSurface(*window);
return 1;
}
}
}
void mainLoop(SDL_Window *window, SDL_Surface *surface) {
// Simple program to fade between white and black background
int g = 0;
int diff = -1;
int quit = 0;
SDL_Event e;
while (!quit) {
while(SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quit = 1;
}
}
SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, g, g, g));
SDL_UpdateWindowSurface(window);
if (g == 0 || g == 255) {
diff *= -1;
}
g += diff;
SDL_Delay(10);
}
}
void closeProg(SDL_Window *window) {
SDL_DestroyWindow(window);
window = NULL;
SDL_Quit();
}
程序分为三个部分:一个是初始化 SDL,一个是主程序发生的地方,另一个是关闭 SDL 并释放表面。
我意识到这个问题是,如果我创建了另一个表面,那么我将不得不向 closeProg()
函数添加代码以在最后释放表面。 N.B:请记住,这些 "surfaces" 只是指向实际事物的指针,据我所知,您并没有真正与之交互,您只是处理指向它的指针。
为了解决这个问题,我决定创建一个包含这些指向表面的指针的数组,然后创建一个函数来创建一个表面并将它的指针添加到该数组。这将允许我在 closeProg()
函数的最后遍历每个表面并释放它,然后也释放数组。 (请注意 window 的表面不会添加到此列表中,因为它会使用 SDL_DestroyWindow()
函数自动释放)
这是这个新数组的声明:
// The pointer to the array. Currently NULL until something is added
SDL_Surface **surfaces = NULL;
// Keeps track of the size of the array
size_t surfaceCount = 0;
这是添加到数组的函数:
int addSurface(SDL_Surface *surface, SDL_Surface **surfaces, size_t *surfaceCount) {
size_t new_count = *surfaceCount + 1;
SDL_Surface **temp = realloc(surfaces, sizeof(*temp) * new_count);
if (temp == NULL) {
printf("Failed to reallocate to %d bytes of memory.", sizeof(*temp) * new_count);
return 0;
} else {
surfaces = temp;
*(surfaces + new_count - 1) = surface;
*surfaceCount = new_count;
return 1;
}
}
这里正在使用中。请注意,loadImage()
returns 来自图像的表面,showSurfaces()
打印出 surfaces
数组的内容。
// Check contents before adding anything
showSurfaces(surfaces, *surfaceCount);
// Add an image
SDL_Surface *fire = loadImage("./fire.bmp");
addSurface(fire, surfaces, surfaceCount);
// Check contents again
showSurfaces(surfaces, *surfaceCount);
// Add another image and check contents again
SDL_Surface *ice = loadImage("./ice.bmp");
addSurface(ice, surfaces, surfaceCount);
showSurfaces(surfaces, *surfaceCount);
// Add another image and check contents a final time
SDL_Surface *man = loadImage("./man.bmp");
addSurface(man, surfaces, surfaceCount);
showSurfaces(surfaces, *surfaceCount);
直到前两个表面,一切都很顺利。但是,当我尝试添加第三个表面时,程序在关闭前立即挂起(表明 closeProg()
函数有问题?)。我决定打印出数组的内容,这就是我得到的内容。
No current surfaces.
Current surfaces:
Index 0: 00753C98
Current surfaces:
Index 0: 00753C98
Index 1: 00754780
Current surfaces:
Index 0: 02805150
Index 1: 008F00C0
Index 2: 201339FC
在前两个打印输出中,一切似乎都很好,但是在第三个打印输出中,您可以注意到以前的地址已更改。 无论我添加了多少表面,这种情况都会反复发生。在前两次重新分配之后,数组的内容不断变化。
我认为这就是程序在关闭时挂起的原因,因为在 closeProg 函数中,程序被告知释放一个不是表面的未知指针,因此它崩溃了。更不用说我还将该指针设置为 NULL,谁知道还会导致什么。
数组内容的这种变化正常吗?如果不正常,请您帮我找到什么,我将不胜感激导致这种奇怪的行为。 我再说一遍,我是一个 完全初学者,所以任何帮助,即使是与这个问题无关的事情也会 GREATLY 感谢。 先谢谢你:)
供参考,这里是 images I used。
如果您感兴趣,请查看完整代码:
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
int initProg(SDL_Window **window, SDL_Surface **surface, char *name, int x, int y, int w, int h, int flags);
void mainLoop(SDL_Window *window, SDL_Surface *surface, SDL_Surface **surfaces, size_t *surfaceCount);
void closeProg(SDL_Window *window, SDL_Window **surfaces, size_t surfaceCount);
SDL_Surface *loadImage(char *path);
int addSurface(SDL_Surface *surface, SDL_Surface **surfaces, size_t *surfaceCount);
int main(int argc, char *args[]) {
SDL_Window *window = NULL;
SDL_Surface *surface = NULL;
// The pointer to the array. Currently NULL untill something is added
SDL_Surface **surfaces = (SDL_Surface *)calloc(1, sizeof(*surfaces));
// Keeps track of the size of the array
size_t surfaceCount = 0;
char windowName[13] = "SDL Tutorial";
int windowXPos = SDL_WINDOWPOS_UNDEFINED;
int windowYPos = SDL_WINDOWPOS_UNDEFINED;
int windowWidth = 600;
int windowHeight = 600;
int flags = SDL_WINDOW_SHOWN;
if (!initProg(&window, &surface, windowName, windowXPos, windowYPos, windowWidth, windowHeight, flags)) {
return 1;
}
mainLoop(window, surface, surfaces, &surfaceCount);
closeProg(window, surfaces, surfaceCount);
return 0;
}
int initProg(SDL_Window **window, SDL_Surface **surface, char *name, int x, int y, int w, int h, int flags) {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
printf("Failed to initialize SDL.\nError: %s\n", SDL_GetError());
return 0;
} else {
*window = SDL_CreateWindow(name, x, y, w, h, flags);
if (*window == NULL) {
printf("Failed to create a window.\nError:%s\n", SDL_GetError());
return 0;
} else {
*surface = SDL_GetWindowSurface(*window);
return 1;
}
}
}
void mainLoop(SDL_Window *window, SDL_Surface *surface, SDL_Surface **surfaces, size_t *surfaceCount) {
// Simple program to fade between white and black background
int g = 0;
int diff = -1;
// Check contents before adding anything
showSurfaces(surfaces, *surfaceCount);
// Add an image
SDL_Surface *fire = loadImage("./fire.bmp");
addSurface(fire, surfaces, surfaceCount);
// Check contents again
showSurfaces(surfaces, *surfaceCount);
// Add another image and check contents again
SDL_Surface *ice = loadImage("./ice.bmp");
addSurface(ice, surfaces, surfaceCount);
showSurfaces(surfaces, *surfaceCount);
// Add another image and check contents a final time
SDL_Surface *man = loadImage("./man.bmp");
addSurface(man, surfaces, surfaceCount);
showSurfaces(surfaces, *surfaceCount);
int quit = 0;
SDL_Event e;
while (!quit) {
while(SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quit = 1;
}
}
SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, g, g, g));
SDL_UpdateWindowSurface(window);
if (g == 0 || g == 255) {
diff *= -1;
}
g += diff;
SDL_Delay(10);
}
}
void closeProg(SDL_Window *window, SDL_Window **surfaces, size_t surfaceCount) {
// Go through the array and free each surface.
for (int i = 0; i < surfaceCount; i++){
SDL_FreeSurface(*(surfaces + i));
*(surfaces + i) = NULL;
}
// Free the array itself.
free(surfaces);
SDL_DestroyWindow(window);
window = NULL;
SDL_Quit();
}
SDL_Surface *loadImage(char *path) {
SDL_Surface *image = SDL_LoadBMP(path);
if (image == NULL) {
printf("Failed to load image.\nError: %s\n", SDL_GetError());
}
return image;
}
int addSurface(SDL_Surface *surface, SDL_Surface **surfaces, size_t *surfaceCount) {
size_t new_count = *surfaceCount + 1;
SDL_Surface **temp = realloc(surfaces, sizeof(*temp) * new_count);
if (temp == NULL) {
printf("Failed to reallocate to %d bytes of memory.", sizeof(*temp) * new_count);
return 0;
} else {
surfaces = temp;
*(surfaces + new_count - 1) = surface;
*surfaceCount = new_count;
return 1;
}
}
void showSurfaces(SDL_Surface **surfaces, size_t surfaceCount) {
if (surfaceCount == 0) {
printf("\nNo current surfaces.\n");
} else {
printf("\nCurrent surfaces:\n");
for (int i = 0; i < surfaceCount; i++) {
printf("\tIndex %d: %p\n", i, *(surfaces + i));
}
putchar('\n');
}
}
如果您可以重现此错误,请在下方发表评论,这样我就知道这不是我的机器或类似问题的问题。
注意:我使用的是 SDL 2.0
I realized the problem with this is that if I had created another
surface then I would have to add code to the closeProg() function to
free the surface at the end.
不是真的。这样做是最干净的,但是当程序终止时,可以依赖 OS 释放程序的内存,包括任何剩余的动态分配的内存。您可以很容易地保持分配的内存直到那时,而不是在终止之前显式释放它。
无论如何,
In the first two print-outs, everything seemed well but in the third one, you can notice that the previous addresses have changed. This
happened repeatedly and no matter how many surfaces I added. After the
first two reallocations, the array's content kept on changing.
I think that is why the program hangs when closing, because in the
closeProg function, the program is told to free an unknown pointer
that is not a surface, hence it crashes. Not to mention that I also
set that pointer to NULL, and who knows what else that could cause.
我觉得你的分析很有道理。
Is this changing of content of the array normal?
没有。使用 realloc()
重新分配会保留原始 space 的内容,如果不重叠则将其复制到新的 space,直到两个 space 的大小较小者s.
I would really appreciate if you could help me find what is causing
this strange behavior.
您的 addSurface()
功能有缺陷。尽管您已经很好地处理了重新分配本身的细节,同时考虑到重新分配没有就地发生的可能性以及重新分配失败的可能性,但您没有成功地将新指针传递给调用者。
特别是,请考虑以下摘录:
surfaces = temp;
*(surfaces + new_count - 1) = surface;
*surfaceCount = new_count;
surface
和surfaceCount
变量都是函数参数。您似乎明白,为了通过后者将新的表面计数值传回给调用者,它必须是指向调用者可访问的变量的指针,以便函数可以通过指针更新该值。这就是摘录的最后一行所做的。
surfaces
指针的情况也一样。当您在摘录的第一行分配给 surfaces
时,您并未进行调用者可见的更改。您只是在函数内部修改局部变量的值。您必须为 surface
添加一个间接层,或者通过函数的 return 值将新指针传回给调用者。就个人而言,我会选择后者,因为三 *
编程并没有被广泛接受为好的风格。
最近我一直在尝试学习 SDL,这是一个用于 C 的图形库。我还没有走得太远,但是我从 this tutorial 那里学到了基础知识(是的,我知道它适用于 C++,但似乎大部分内容仍然相同),我尝试创建一个 "template" SDL 程序来启动我所有其他未来的程序。这是我的:
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
int initProg(SDL_Window **window, SDL_Surface **surface, char *name, int x, int y, int w, int h, int flags);
void mainLoop(SDL_Window *window, SDL_Surface *surface);
void closeProg(SDL_Window *window);
SDL_Surface *loadImage(char *path);
int main(int argc, char *args[]) {
SDL_Window *window = NULL;
SDL_Surface *surface = NULL;
char windowName[13] = "SDL Tutorial";
int windowXPos = SDL_WINDOWPOS_UNDEFINED;
int windowYPos = SDL_WINDOWPOS_UNDEFINED;
int windowWidth = 600;
int windowHeight = 600;
int flags = SDL_WINDOW_SHOWN;
if (!initProg(&window, &surface, windowName, windowXPos, windowYPos, windowWidth, windowHeight, flags)) {
return 1;
}
mainLoop(window, surface);
closeProg(window);
return 0;
}
int initProg(SDL_Window **window, SDL_Surface **surface, char *name, int x, int y, int w, int h, int flags) {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
printf("Failed to initialize SDL.\nError: %s\n", SDL_GetError());
return 0;
} else {
*window = SDL_CreateWindow(name, x, y, w, h, flags);
if (*window == NULL) {
printf("Failed to create a window.\nError:%s\n", SDL_GetError());
return 0;
} else {
*surface = SDL_GetWindowSurface(*window);
return 1;
}
}
}
void mainLoop(SDL_Window *window, SDL_Surface *surface) {
// Simple program to fade between white and black background
int g = 0;
int diff = -1;
int quit = 0;
SDL_Event e;
while (!quit) {
while(SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quit = 1;
}
}
SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, g, g, g));
SDL_UpdateWindowSurface(window);
if (g == 0 || g == 255) {
diff *= -1;
}
g += diff;
SDL_Delay(10);
}
}
void closeProg(SDL_Window *window) {
SDL_DestroyWindow(window);
window = NULL;
SDL_Quit();
}
程序分为三个部分:一个是初始化 SDL,一个是主程序发生的地方,另一个是关闭 SDL 并释放表面。
我意识到这个问题是,如果我创建了另一个表面,那么我将不得不向 closeProg()
函数添加代码以在最后释放表面。 N.B:请记住,这些 "surfaces" 只是指向实际事物的指针,据我所知,您并没有真正与之交互,您只是处理指向它的指针。
为了解决这个问题,我决定创建一个包含这些指向表面的指针的数组,然后创建一个函数来创建一个表面并将它的指针添加到该数组。这将允许我在 closeProg()
函数的最后遍历每个表面并释放它,然后也释放数组。 (请注意 window 的表面不会添加到此列表中,因为它会使用 SDL_DestroyWindow()
函数自动释放)
这是这个新数组的声明:
// The pointer to the array. Currently NULL until something is added
SDL_Surface **surfaces = NULL;
// Keeps track of the size of the array
size_t surfaceCount = 0;
这是添加到数组的函数:
int addSurface(SDL_Surface *surface, SDL_Surface **surfaces, size_t *surfaceCount) {
size_t new_count = *surfaceCount + 1;
SDL_Surface **temp = realloc(surfaces, sizeof(*temp) * new_count);
if (temp == NULL) {
printf("Failed to reallocate to %d bytes of memory.", sizeof(*temp) * new_count);
return 0;
} else {
surfaces = temp;
*(surfaces + new_count - 1) = surface;
*surfaceCount = new_count;
return 1;
}
}
这里正在使用中。请注意,loadImage()
returns 来自图像的表面,showSurfaces()
打印出 surfaces
数组的内容。
// Check contents before adding anything
showSurfaces(surfaces, *surfaceCount);
// Add an image
SDL_Surface *fire = loadImage("./fire.bmp");
addSurface(fire, surfaces, surfaceCount);
// Check contents again
showSurfaces(surfaces, *surfaceCount);
// Add another image and check contents again
SDL_Surface *ice = loadImage("./ice.bmp");
addSurface(ice, surfaces, surfaceCount);
showSurfaces(surfaces, *surfaceCount);
// Add another image and check contents a final time
SDL_Surface *man = loadImage("./man.bmp");
addSurface(man, surfaces, surfaceCount);
showSurfaces(surfaces, *surfaceCount);
直到前两个表面,一切都很顺利。但是,当我尝试添加第三个表面时,程序在关闭前立即挂起(表明 closeProg()
函数有问题?)。我决定打印出数组的内容,这就是我得到的内容。
No current surfaces.
Current surfaces:
Index 0: 00753C98
Current surfaces:
Index 0: 00753C98
Index 1: 00754780
Current surfaces:
Index 0: 02805150
Index 1: 008F00C0
Index 2: 201339FC
在前两个打印输出中,一切似乎都很好,但是在第三个打印输出中,您可以注意到以前的地址已更改。 无论我添加了多少表面,这种情况都会反复发生。在前两次重新分配之后,数组的内容不断变化。
我认为这就是程序在关闭时挂起的原因,因为在 closeProg 函数中,程序被告知释放一个不是表面的未知指针,因此它崩溃了。更不用说我还将该指针设置为 NULL,谁知道还会导致什么。
数组内容的这种变化正常吗?如果不正常,请您帮我找到什么,我将不胜感激导致这种奇怪的行为。 我再说一遍,我是一个 完全初学者,所以任何帮助,即使是与这个问题无关的事情也会 GREATLY 感谢。 先谢谢你:)
供参考,这里是 images I used。
如果您感兴趣,请查看完整代码:
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
int initProg(SDL_Window **window, SDL_Surface **surface, char *name, int x, int y, int w, int h, int flags);
void mainLoop(SDL_Window *window, SDL_Surface *surface, SDL_Surface **surfaces, size_t *surfaceCount);
void closeProg(SDL_Window *window, SDL_Window **surfaces, size_t surfaceCount);
SDL_Surface *loadImage(char *path);
int addSurface(SDL_Surface *surface, SDL_Surface **surfaces, size_t *surfaceCount);
int main(int argc, char *args[]) {
SDL_Window *window = NULL;
SDL_Surface *surface = NULL;
// The pointer to the array. Currently NULL untill something is added
SDL_Surface **surfaces = (SDL_Surface *)calloc(1, sizeof(*surfaces));
// Keeps track of the size of the array
size_t surfaceCount = 0;
char windowName[13] = "SDL Tutorial";
int windowXPos = SDL_WINDOWPOS_UNDEFINED;
int windowYPos = SDL_WINDOWPOS_UNDEFINED;
int windowWidth = 600;
int windowHeight = 600;
int flags = SDL_WINDOW_SHOWN;
if (!initProg(&window, &surface, windowName, windowXPos, windowYPos, windowWidth, windowHeight, flags)) {
return 1;
}
mainLoop(window, surface, surfaces, &surfaceCount);
closeProg(window, surfaces, surfaceCount);
return 0;
}
int initProg(SDL_Window **window, SDL_Surface **surface, char *name, int x, int y, int w, int h, int flags) {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
printf("Failed to initialize SDL.\nError: %s\n", SDL_GetError());
return 0;
} else {
*window = SDL_CreateWindow(name, x, y, w, h, flags);
if (*window == NULL) {
printf("Failed to create a window.\nError:%s\n", SDL_GetError());
return 0;
} else {
*surface = SDL_GetWindowSurface(*window);
return 1;
}
}
}
void mainLoop(SDL_Window *window, SDL_Surface *surface, SDL_Surface **surfaces, size_t *surfaceCount) {
// Simple program to fade between white and black background
int g = 0;
int diff = -1;
// Check contents before adding anything
showSurfaces(surfaces, *surfaceCount);
// Add an image
SDL_Surface *fire = loadImage("./fire.bmp");
addSurface(fire, surfaces, surfaceCount);
// Check contents again
showSurfaces(surfaces, *surfaceCount);
// Add another image and check contents again
SDL_Surface *ice = loadImage("./ice.bmp");
addSurface(ice, surfaces, surfaceCount);
showSurfaces(surfaces, *surfaceCount);
// Add another image and check contents a final time
SDL_Surface *man = loadImage("./man.bmp");
addSurface(man, surfaces, surfaceCount);
showSurfaces(surfaces, *surfaceCount);
int quit = 0;
SDL_Event e;
while (!quit) {
while(SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quit = 1;
}
}
SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, g, g, g));
SDL_UpdateWindowSurface(window);
if (g == 0 || g == 255) {
diff *= -1;
}
g += diff;
SDL_Delay(10);
}
}
void closeProg(SDL_Window *window, SDL_Window **surfaces, size_t surfaceCount) {
// Go through the array and free each surface.
for (int i = 0; i < surfaceCount; i++){
SDL_FreeSurface(*(surfaces + i));
*(surfaces + i) = NULL;
}
// Free the array itself.
free(surfaces);
SDL_DestroyWindow(window);
window = NULL;
SDL_Quit();
}
SDL_Surface *loadImage(char *path) {
SDL_Surface *image = SDL_LoadBMP(path);
if (image == NULL) {
printf("Failed to load image.\nError: %s\n", SDL_GetError());
}
return image;
}
int addSurface(SDL_Surface *surface, SDL_Surface **surfaces, size_t *surfaceCount) {
size_t new_count = *surfaceCount + 1;
SDL_Surface **temp = realloc(surfaces, sizeof(*temp) * new_count);
if (temp == NULL) {
printf("Failed to reallocate to %d bytes of memory.", sizeof(*temp) * new_count);
return 0;
} else {
surfaces = temp;
*(surfaces + new_count - 1) = surface;
*surfaceCount = new_count;
return 1;
}
}
void showSurfaces(SDL_Surface **surfaces, size_t surfaceCount) {
if (surfaceCount == 0) {
printf("\nNo current surfaces.\n");
} else {
printf("\nCurrent surfaces:\n");
for (int i = 0; i < surfaceCount; i++) {
printf("\tIndex %d: %p\n", i, *(surfaces + i));
}
putchar('\n');
}
}
如果您可以重现此错误,请在下方发表评论,这样我就知道这不是我的机器或类似问题的问题。
注意:我使用的是 SDL 2.0
I realized the problem with this is that if I had created another surface then I would have to add code to the closeProg() function to free the surface at the end.
不是真的。这样做是最干净的,但是当程序终止时,可以依赖 OS 释放程序的内存,包括任何剩余的动态分配的内存。您可以很容易地保持分配的内存直到那时,而不是在终止之前显式释放它。
无论如何,
In the first two print-outs, everything seemed well but in the third one, you can notice that the previous addresses have changed. This happened repeatedly and no matter how many surfaces I added. After the first two reallocations, the array's content kept on changing.
I think that is why the program hangs when closing, because in the closeProg function, the program is told to free an unknown pointer that is not a surface, hence it crashes. Not to mention that I also set that pointer to NULL, and who knows what else that could cause.
我觉得你的分析很有道理。
Is this changing of content of the array normal?
没有。使用 realloc()
重新分配会保留原始 space 的内容,如果不重叠则将其复制到新的 space,直到两个 space 的大小较小者s.
I would really appreciate if you could help me find what is causing this strange behavior.
您的 addSurface()
功能有缺陷。尽管您已经很好地处理了重新分配本身的细节,同时考虑到重新分配没有就地发生的可能性以及重新分配失败的可能性,但您没有成功地将新指针传递给调用者。
特别是,请考虑以下摘录:
surfaces = temp;
*(surfaces + new_count - 1) = surface;
*surfaceCount = new_count;
surface
和surfaceCount
变量都是函数参数。您似乎明白,为了通过后者将新的表面计数值传回给调用者,它必须是指向调用者可访问的变量的指针,以便函数可以通过指针更新该值。这就是摘录的最后一行所做的。
surfaces
指针的情况也一样。当您在摘录的第一行分配给 surfaces
时,您并未进行调用者可见的更改。您只是在函数内部修改局部变量的值。您必须为 surface
添加一个间接层,或者通过函数的 return 值将新指针传回给调用者。就个人而言,我会选择后者,因为三 *
编程并没有被广泛接受为好的风格。