简体   繁体   English

使用memcmp()时出现段错误

[英]Segfault while using memcmp()

I'm getting segfault in this line: 我在这行中出现段错误:

if(memcmp(datap, 0x38 , 1) == 0)

This is a trace from gdb, you can see datap here: 这是来自gdb的跟踪,您可以在此处查看datap:

Program received signal SIGSEGV, Segmentation fault.
0x00000000004010f1 in processMTMHeader (
    datap=0x2aaaab0b001c "1\34\66\63\36\65\34\66.\36\70\34AAAA1.ETR\36\67\64\34U\35\36\61\60\63\34\61\36\62\65\70\34\60\71:00:00\36\62\70\61\34\60\71:00:00\36\64\62\67\34\63\60\60\60\36\65\63\34\63\36\66\63\34\63\36\66\67\34\63\36\70\60\34\63\36\70\61\34\61\60\60\60\36\70\62\34\60\71:00:00\36\70\63\34\61\60\60\60\3Ea", h=0x7fffffffb960,
    endmmsgp=0x2aaaab0b0090 "\3Ea") at unzipper.c:91
91      if(memcmp(datap, 0x38 , 1) == 0)

You're using the integer value 0x38 as a pointer, which is very likely not a good idea. 您将整数值0x38用作指针,这可能不是一个好主意。

You should probably have: 您可能应该具有:

const uint8_t data[] = { 0x38 };

if(memcmp(datap, data, sizeof data) == 0)

Or, of course, since it's just a single byte: 或者,当然,因为它只是一个字节:

if(((uint8_t *) datap)[0] == 0x38)

UPDATE Assuming that datap is declared to be unsigned char *datap , we can drop the cast and just do: 更新假设datap被声明为unsigned char *datap ,我们可以删除unsigned char *datap并执行以下操作:

if(*datap == 0x38)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM