我需要修改现有的API,基本上,我唯一的选择就是通过函数参数返回指向不规则数组的指针(这通常不是我的个人喜好)。我无法全力以赴,并在此部分代码中不断遇到分段错误:
void getMxByArg(int ***pppX) {
*pppX = m_ppMx; // SEGMENTATION FAULT HERE
}
我在下面提供了一个示例,该示例没有任何外部依赖关系,并封装了问题。
#include <stdint.h>
#include <iostream>
using namespace std;
class Mx {
public:
Mx() {
int *buff01 = (int*)malloc(3 * sizeof(int));
int *buff02 = (int*)malloc(3 * sizeof(int));
buff01[0] = 0;
buff01[1] = 1;
buff01[2] = 3;
buff02[0] = 4;
buff02[1] = 5;
buff02[2] = 6;
m_n = 2;
m_ppMx = (int**)malloc(m_n * sizeof(int*));
m_ppMx[0] = buff01;
m_ppMx[1] = buff02;
}
~Mx() {
for (int i=0; i<m_n; ++i) {
free(m_ppMx[i]);
}
free(m_ppMx);
}
int** getMx() {
return m_ppMx;
}
void getMxByArg(int ***pppX) {
*pppX = m_ppMx; // SEGMENTATION FAULT HERE
}
private:
int **m_ppMx;
int m_n;
};
int main()
{
Mx mx;
// SUCCESS
int **ppX = mx.getMx();
// FAILURE, Results in segmentation fault in getMxByArg
int ***pppX;
mx.getMxByArg(pppX);
return 0;
}
答案 0 :(得分:4)
在发布的代码中,您要取消引用未初始化的指针。这是导致行为不确定的原因。
解决方案是:
' 07-31 18:02:14.123 2526-3085/info.burhanisolution.dholkaapp W/cr_CrashFileManager: /data/user/0/info.burhanisolution.dholkaapp/cache/WebView/Crash Reports does not exist or is not a directory 07-31 18:02:14.967 2526-3008/info.burhanisolution.dholkaapp W/Ads: Fail to instantiate adapter com.google.DummyAdapter
android.os.RemoteException
at com.google.android.gms.internal.ads.zzxm.zzbp(Unknown Source)
at com.google.android.gms.internal.ads.zzxm.zzbo(Unknown Source)
at com.google.android.gms.internal.ads.zzxm.zzbm(Unknown Source)
at com.google.android.gms.internal.ads.zzxo.dispatchTransaction(Unknown Source)
at com.google.android.gms.internal.ads.zzek.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:499)
at mt.a(:com.google.android.gms.dynamite_adsdynamite@12874022@12.8.74 (040308-204998136):10)
at com.google.android.gms.ads.internal.mediation.client.d.a(:com.google.android.gms.dynamite_adsdynamite@12874022@12.8.74 (040308-204998136):5)
at com.google.android.gms.ads.internal.reward.a.a(:com.google.android.gms.dynamite_adsdynamite@12874022@12.8.74 (040308-204998136):15)
at com.google.android.gms.ads.internal.reward.mediation.j.a(:com.google.android.gms.dynamite_adsdynamite@12874022@12.8.74 (040308-204998136):27)
at com.google.android.gms.ads.internal.util.b.run(:com.google.android.gms.dynamite_adsdynamite@12874022@12.8.74 (040308-204998136):4)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
的变量。int**
另一种选择是将参数类型更改为int **ppX2;
mx.getMxByArg(&ppX2);
。
int**&
然后,您可以使用:
void getMxByArg(int**& ppX) {
ppX = m_ppMx;
}