ITKeyword,专注技术干货聚合推荐

注册 | 登录

c - Segfault Cannot access memory at address

itPublisher 分享于

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1073

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

推荐:PETSC ERROR: Segmentation Violation, probably memory access out of range

      [0]PETSC ERROR: ------------------------------------------------------------------------ [0]PETSC ERROR: Caught signal number 11 SEGV: Segmentat

I got a segmentation fault on the code section below: (regarding this segfault , I am asking 2 question in one topic)

req_cw(ECM_REQUEST *er, int32_t flag, int32_t reader_types){

    LL_NODE *ptr;
    for (ptr = er->matching_rdr->initial ; ptr ; ptr = ptr->nxt) {
        if (ptr == er->fail)
            break;
    rdr = (struct s_reader*)ptr->obj;
    we_equest(rdr->fd, er);
    }
}

As you see, even with the control structure with if(ptr->obj && ptr) to be sure ptr (or ptr->obj won't be null pointer) but it doesn't prevent it and after segmentation fault when I tried to print the value ptr->obj with:

print ptr->obj , gdb says :

(gdb) print ptr $1 = (LL_NODE *) 0x149

(gdb) x 0x149

0x149: Cannot access memory at address 0x149

My question is: How can I prevent this segmentation fault , why this segmentation fault happens and how would I fix it?

  1. Regarding this segfault , when with gdb , i had checked frame 1 (one frame before when segfault happened) , i figured out that

er->cw is "\000 <repeats 15 times>" my question is how could I control if er->cw won't be equal of "'\000' <repeats 15 times"?

I did this, I know it's not correct (and I missed something) , that's why I ask here

if (er->cw)
req_cw(er , ...)

The problem is with adding if(er->cw). I think cw is character and I shouldn't behave like integer , but I don't know how to check it to not be zero?, I mean , even with this line again when I check with gdb , still er->cw with value of "'\000' " is passing , so how with if control block I could check when er->cw is not equal to '\000' " ?

Thanks in advance.

c segmentation-fault control
|
  this question
edited Mar 4 '13 at 15:12 asked Mar 4 '13 at 13:55 nima tajfar 28 2 8 1   0x149 is definitely an invalid pointer... the problem is in another part of the code you haven't showed us –  Karoly Horvath Mar 4 '13 at 14:04      @Karoly , how with gdb i could figure out ox149 why is invalid , i mean how with gdb i could check that invalid pointer u mentioned? btw , my second part question is about how to check if cw is not equal to "'\000' repeating 15 times" with if funtion ? btw , since u said the problem is in another part of the code , what part u meant? (if u said the function name , i will provide that source code too . Thanks –  nima tajfar Mar 4 '13 at 14:09      @All at least my second part question muse be simple to many people of this forum! isn't , so after 50 review still , no respond to second part , so if i check with if(strlen(er->cw) != 0) is this control to er->cw valid for prevent the value be '\000 repeat 15 times ? –  nima tajfar Mar 4 '13 at 14:41

 | 

2 Answers
2

As you see, even with the control structure with if(ptr->obj || ptr) to be sure ptr (or ptr->null won't be null pointer)

No... that actually doesn't ensure that neither will be NULL, that sets you up for a seg fault. The || will evaluate both sides before it quits, and you set up the logic backwards anyway. You wanted && and you need to flip them or it will deference what could be a NULL pointer:

if(ptr && ptr->obj)

after segmentation fault when I tried to print the value ptr->obj with:
print ptr->obj , gdb says :
...
0x149: Cannot access memory at address 0x149

I've never seen an address of 0x149 be valid, what hardware platform are you on?

er->cw is "\000 " my question is how could I control if er->cw won't be equal of "'\000'

This is not clear, could you show the structure for er? Are you saying that er->cw is a char * type and is set to "\000 <repeats 15 times>"?


|
  this answer
answered Mar 4 '13 at 14:56 Mike 23.7k 14 64 137      Thanks for replying , cw is uchar cw[16]; (unsigned char) Hardware is SH4 / stlinux –  nima tajfar Mar 4 '13 at 15:10      regard if(ptr && ptr->obj) i knew , i said this is a typo caused by editing of moderator! –  nima tajfar Mar 4 '13 at 15:15      @nimatajfar yes, and you also said (in the other comment) that you tried (!ptr && !ptr->obj) which is incorrect. I see in or edit that you meant it without the !s. The SH4 is 32-bit hardware, so why aren't you trying to access the address with a 32-bit address? C isn't going to know which way to pad that if that was your intention. Since cw is an array of characters then you need to compare char by char or with a str function, the initial \000 is an escape char and you should see that cw[0] == 0 –  Mike Mar 4 '13 at 15:24

 | 

As you see, even with the control structure with if(ptr->obj || ptr) to be sure ptr (or ptr->null won't be null pointer)

The problem is the order you are doing the tests, when compiled the ptr->obj is done first, so if ptr is null you are accessing invalid memory get get the obj member. Reverse the order of the tests if (ptr || ptr->obj)

BUT that will still not be right... Your OR logic is wrong too, the code should be...

if ((ptr != NULL) && (ptr->obj != NULL))
{
   // use the pointer
}

|
  this answer
answered Mar 4 '13 at 14:47 Anonymouse 766 6 14      hmm , no this is not the case , after editing by moderators this happens (u had find a typo in first post), in the code i had checked with if (!ptr && !ptr->obj) which is definetly same on what u said , hmm , ok , i limit my question to one part , if i want to check er->cw which is char of array (longth of array is 15) the elements won't be zero , how could i do it? does strlen (er->cw) != '\000' do the job or not? –  nima tajfar Mar 4 '13 at 14:54      It depends what er->cw is used for... if it is a text-string then checking if (er->cw[0] == '\0') empty_string is enough to test for an empty string. Testing for if (er->cw=="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0") is BAD idea. –  Anonymouse Mar 4 '13 at 15:09      Thanks Anonymouse , is there any difference between checking if (er->cw[0] == '\0' or '\000'? –  nima tajfar Mar 4 '13 at 15:14      The absolute purists will say yes there is a difference, \0 is the NULL terminator character, where as \000 is ascii character 0. But in reality they are the same. –  Anonymouse Mar 4 '13 at 15:47

 | 

推荐:STM32 调试时,出现:cannot access memory!!!

以前做的项目是stm32f103vc+ucos,这次做一个新东东,采用的是stm32f101c8,无操作系统的,代码由stm32f103vc+ucos移植过来,用SWD调试时出现cannot access memo


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。