小编Ale*_*eev的帖子

分段错误,大数组

#include <stdio.h>
#define N 1024
int main(){
  int i, j;
  int a[N][N];
  int b[N][N];
  for (i=0;i<N;i++){
    a[i][i]=i;
    b[i][i]=i;
  }
  for (i=0;i<N;i++)
    for(j=0;j<N;j++)
    {
         printf("%d", a[i][j]);
         printf("%d", b[i][j]);
    }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

此程序是分段错误的原因,但如果我将N定义为1023,程序将正常工作.为什么会这样?

c segmentation-fault

18
推荐指数
1
解决办法
2万
查看次数

虚假分享和pthreads

我有以下任务来演示虚假共享并编写了一个简单的程序:

#include <sys/times.h>
#include <time.h>
#include <stdio.h> 
#include <pthread.h> 

long long int tmsBegin1,tmsEnd1,tmsBegin2,tmsEnd2,tmsBegin3,tmsEnd3;

int array[100];

void *heavy_loop(void *param) { 
  int   index = *((int*)param);
  int   i;
  for (i = 0; i < 100000000; i++)
    array[index]+=3;
} 

int main(int argc, char *argv[]) { 
  int       first_elem  = 0;
  int       bad_elem    = 1;
  int       good_elem   = 32;
  long long time1;
  long long time2;
  long long time3;
  pthread_t     thread_1;
  pthread_t     thread_2;

  tmsBegin3 = clock();
  heavy_loop((void*)&first_elem);
  heavy_loop((void*)&bad_elem);
  tmsEnd3 = clock();

  tmsBegin1 = clock();
  pthread_create(&thread_1, NULL, heavy_loop, …
Run Code Online (Sandbox Code Playgroud)

c pthreads false-sharing

8
推荐指数
2
解决办法
3495
查看次数

使用try {...} catch(..){...}代替转到

是不好的做法使用像goto一样的试试?例如,简单的代码

try{
    if(argc<2){
        std::cout<<"no inputfile"<<std::endl;
        throw 1;
    }
    STARTUPINFO cif;
    ZeroMemory(&cif,sizeof(STARTUPINFO));
    PROCESS_INFORMATION pi;
    if(FALSE==CreateProcess(argv[1],NULL,NULL,NULL,FALSE,NULL,NULL,NULL,
                            &cif,&pi)){
        printf("smth is wrong");
        throw 1;
    }
    WaitForSingleObject(pi.hProcess, INFINITE);
    GetExitCodeProcess(pi.hProcess, &exitCode);
    std::cout<<"Process return"<<exitCode<<std::endl;
    throw 1;
}
catch(int a){
    printf("press Enter");
    getchar();
}
Run Code Online (Sandbox Code Playgroud)

c++ goto try-catch

1
推荐指数
1
解决办法
1006
查看次数

标签 统计

c ×2

c++ ×1

false-sharing ×1

goto ×1

pthreads ×1

segmentation-fault ×1

try-catch ×1