2014-02-22 50 views
1

似乎我遇到了libc中的一个可能的错误。我有以下代码:取决于架构的不同行为

#include <stdio.h> 
#include <stdlib.h> 
#include <pthread.h> 
#include <unistd.h> 

struct bla 
{ 
    int a,b,c,d; 
}; 

pthread_t tid; 

void print (const char *s, const struct bla *fp); 

void * thr_fn1 (void * arg); 

int main() 
{ 
    struct bla *bla_main; 

    pthread_create (&tid,NULL,thr_fn1,NULL); 
    pthread_join (tid, (void *) &bla_main); 
    print ("Old thread: \n",bla_main); 
    return 0; 
} 

void print (const char *s, const struct bla *bla_print) 
{ 
    printf ("%s\n",s); 
    printf ("Struct address: %p\n",bla_print); 
    printf ("fp.a = %d\n",bla_print->a); 
    printf ("fp.b = %d\n",bla_print->b); 
    printf ("fp.c = %d\n",bla_print->c); 
    printf ("fp.d = %d\n",bla_print->d); 
} 

void * thr_fn1 (void * arg) 
{ 
    struct bla *bla_thr; 

    bla_thr= malloc(1); 
    bla_thr->a=1; 
    bla_thr->b=2; 
    bla_thr->c=3; 
    bla_thr->d=4; 
    print ("Thread 1:\n",bla_thr); 
    pthread_exit ((void *) bla_thr); 
} 

编译使用gcc -Wall -pthread file.c完成,它不会产生错误/警告。但是,当我尝试在我的树莓派(32位)运行它,我得到以下的输出:

[[email protected] code]$ ./a.out 
Thread 1: 

Struct address: 0xb6500468 
fp.a = 1 
fp.b = 2 
fp.c = 3 
fp.d = 4 
a.out: malloc.c:2365: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed. 
Aborted (core dumped) 

[[email protected] code]$ file a.out 
a.out: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.27, BuildID[sha1]=33e5d87872f0b40924a709fe266d47f9f011a06c, not stripped 

我注意到,同样的事情发生时,我尝试在英特尔处理器上运行它,使用编译步骤的-m32选项,以生成32位可执行文件。

[email protected]:~/code$ ./a.out 
Thread 1: 

Struct address: 0x804e098 
fp.a = 1 
fp.b = 2 
fp.c = 3 
fp.d = 4 

a.out: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed. 
Aborted 


[email protected]:~/code$ file a.out 
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, BuildID[sha1]=0x9966b205f3f6cd3d3a544ea010608e11346f6f9a, not stripped 

但是,在运行英特尔上的程序的64位可执行文件时,不会发生这种情况。

[email protected]:~/code$ ./a.out 
Thread 1: 

Struct address: 0xb42130 
fp.a = 1 
fp.b = 2 
fp.c = 3 
fp.d = 4 

Old thread: 

Struct address: 0xb42130 
fp.a = 1 
fp.b = 2 
fp.c = 3 
fp.d = 4 
[email protected]:~/code$ file a.out 
a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, BuildID[sha1]=0x4bb04ef61287bfe750a37427bb41b8b1578d74e1, not stripped 

所以,这是在libc中/的malloc()的错误,还是我做错了什么? 如果您需要更多详细信息,请告诉我。

感谢

+2

'bla_thr = malloc(1);'1字节可能对结构来说太小了。尝试'bla_thr = malloc(sizeof * bla_thr);'而是。 – wildplasser

+3

为什么downvoting?我不认为“这个问题没有显示任何研究工作;它不清楚或没有用”...... OP甚至尝试了不同的构建。至少告诉为什么downvoting。 – Seki

+0

也许你应该改变问题标题。最好陈述一个问题,而不是一个可能的解决方案(libc中的错误)。像“为什么不同的架构展现不同的行为?”有趣的是,当你用这种方式来描述时,未定义的行为就成了一个明显的候选人。 – stv

回答

8

你4个int小号分配1个字节:

bla_thr= malloc(1); 

bla_thr->a=1; 
bla_thr->b=2; 
bla_thr->c=3; 
bla_thr->d=4; 

这调用不确定的行为,因此一切都有可能发生。错误在你的代码中,而不是在libc中。如果你分配足够的空间:

bla_thr = malloc(sizeof *bla_thr); // == sizeof(struct bla); 

它应该工作。之后不要忘记free()的内存!

相关问题