Memory 为什么我只是改变代码位置,内存使用就不同了?

Memory 为什么我只是改变代码位置,内存使用就不同了?,memory,pthreads,Memory,Pthreads,A行的malloc将消耗比B行更多的内存, 为什么?它与pthread相关吗 int main() { char *buf = (char*)malloc(1024*1024*1024); //Line A memset(buf,0,sizeof(1024*1024*1024)); pthread_t m_sockThreadHandle[8]; for (int i=0;i<8;i++) { if ( pthread_create(&a

A行的malloc将消耗比B行更多的内存, 为什么?它与pthread相关吗

int main()
{
    char *buf = (char*)malloc(1024*1024*1024); //Line A
    memset(buf,0,sizeof(1024*1024*1024));
    pthread_t m_sockThreadHandle[8];
    for (int i=0;i<8;i++)
    {
      if ( pthread_create(&m_sockThreadHandle[i], NULL, thread_run, NULL) != 0 )
      {
        perror("pthread_create");
      }
    }
    sleep(10);
    char *buf = (char*)malloc(1024*1024*1024);//Line B
    memset(buf,0,sizeof(1024*1024*1024));
    for (int i=0;i<8;i++)
    {
      pthread_join(m_sockThreadHandle[i],NULL);
    }
}
intmain()
{
char*buf=(char*)malloc(1024*1024*1024);//行A
memset(buf,0,sizeof(1024*1024*1024));
pthread_t m_socksthreadhandle[8];

对于(int i=0;i可能是因为这并没有达到您所认为的效果:

memset(buf,0,sizeof(1024*1024*1024));
sizeof(1024*1024*1024)
在我的编译器上是4。我想你的意思是:

memset(buf,0, 1024*1024*1024);

从您发布的代码来看,
buf
未使用,因此不清楚您想做什么,或者为什么。但这至少是错误的….

您如何确定这一点?