我的realloc()没有重新分配更多内存。例如,我有一个方法:
void addInfo(LocationArray** myArray, int* count, int* numberOfLoc) {
if(*count == *numberOfLoc) { // Checks if the array is already full
printf("count is %d and numberOfLoc is %d\n", *count, *numberOfLoc);
resizeArray(*myArray, numberOfLoc); //Resizes the array if it's full
}
.......
}
如果count
值等于numberOfLoc
,则resizeArray()
应重新分配更多内存。但我得到了resizeArray()
:
printf("The memory heap is exhausted!\n");
我在代码中做错了什么?
typedef struct Location {
char locName[35];
char locDesc[85];
float latitude;
float longitude;
} LocationArray;
int main() {
printf("How many locations would you like to be inside the array?\n");
int numberOfLoc = 0; //variable for storing the size of the LocationArray
scanf(" %d", &numberOfLoc); //gets the user input and stores in numerOfLoc
LocationArray* myArray; //declares a LocationArray with the size of numberOfLoc
myArray = (LocationArray*)malloc(numberOfLoc * sizeof(LocationArray));
int count = 0;
while(1) {
//Prints the menu
printMenu(&myArray, &count, &numberOfLoc);
}
//Free the pointer
free(myArray);
return 0;
}
void addInfo(LocationArray** myArray, int* count, int* numberOfLoc) {
if(*count == *numberOfLoc) { // Checks if the array is already full
printf("count is %d and numberOfLoc is %d\n", *count, *numberOfLoc);
resizeArray(myArray, numberOfLoc); //Resizes the array if it's full
}
.......
(*count)++;
}
void resizeArray(LocationArray** myArray, int* numberOfLoc) {
*numberOfLoc = *numberOfLoc * 2; //Double the size
LocationArray* temp;
temp = (LocationArray*)realloc(myArray, *numberOfLoc * sizeof(LocationArray)); //Reallocates more memory
//Checks if the memory heap is exhausted
if(temp == NULL) {
printf("The memory heap is exhausted!\n");
}
else {
*myArray = temp; //Copy from the temp struct variable to myArray
}
}
答案 0 :(得分:2)
我直接看到一个问题:
*myArray = temp; //Copy from the temp struct variable to myArray
free(*myArray);
我不认为你应该这样做,因为temp
是新分配的内存,然后你放入*myArray
然后立即释放它。
此外,由于resizeArray
和addInfo
都采用LocationArray **
类型的变量,因此您可能希望按原样传递它。换句话说,addInfo
的最后一行应该是:
resizeArray (myArray, numberOfLoc);
盲目地忽略使用它们表示失败的函数的返回码也不是一个好主意。
答案 1 :(得分:0)
resizeArray()接受private void sendHttpPost(String input, ChannelHandlerContext ctx) {
try {
String url = "http://localhost";
URI uri = new URI(url);
Bootstrap b = new Bootstrap();
b.group(new NioEventLoopGroup())
.channel(NioServerSocketChannel.class)
.handler(new PostRequestHandler())
.option(ChannelOption.AUTO_READ, false);
Channel f = b.connect("REMOTE_HOST", 8888).sync().channel();
HttpRequest postReq = new DefaultHttpRequest(HttpVersion.HTTP_1_1,
HttpMethod.POST, uri.getRawPath());
postReq.headers().set(HttpHeaders.Names.HOST, "localhost");
postReq.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
postReq.headers().set(HttpHeaders.Names.CONTENT_TYPE,"application/x-www-form-urlencoded");
f.writeAndFlush(postReq);
// Wait for the server to close the connection.
f.closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
}
}
,但addInfo()将LocationArray **
传递给它,所以你最终将垃圾传递给realloc()。