我正在清理我的int main()
。因爲我是C的初學者,所以我用main()
函數編寫了所有的東西。現在我想把我在其他函數中調用的一些函數集成到一個小函數中。爲main()以外的struct和call函數分配內存
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <myStructs.h> // Here are my structs geo ,tree and par defined
#include <myFunct.h> // Here are my functions
// Structs
geo dom;
tree **root;
par *p;
void settree(tree **root, par *p, geo dom){
root = malloc(sizeof(tree*));
*root = calloc(1, sizeof(tree));
compDom(&dom, p);
initTree(root, &dom, p);
}
int main (int argc, char **argv){
int n = 100;
p = malloc(sizeof(_Particle) * n); // Allocate memory for par
anop = n;
// Load particle data
par *parptr = p;
getData(parptr, n);
double T = 20;
double g = 1;
for(double h=0; h<T; h+=g){
/// Build tree ///
settree(*root, p, dom);
/// Integration ///
ec(root, p, n, g);
/// Free Memory ///
freeTree(*root);
free(root);
}
return 0;
}
我有問題,功能settree()
。我總是得到分段錯誤。但爲什麼?這個函數中的指針有問題嗎?有人可以給我一個提示,告訴我如何在這種情況下用指針工作嗎?我幾乎可以肯定,我的錯誤存在。
這並沒有解決我的問題。我仍然得到seg。故障。該錯誤可能是其他地方。你有另一個想法嗎? – Samuel
您是否有來自segfault的堆棧跟蹤? –
我不知道該怎麼做。抱歉。正如我所說的,我是C初學者,控制檯只顯示'Segmentation fault'。如果我將'settree()'的內容放在'main()'中,而不是程序工作。 – Samuel