printf中的超出错误 [英] Out of bound error in printf

查看:103
本文介绍了printf中的超出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此代码中,当我想打印结果时,我得到非法写入,出界错误





如果输入字符串很短它不会给出错误。

如果我删除printf行ID不会出错。



我有什么试过:



In this code when I want to print the result I get "illegal write, out of bounds error"


if input string be short it does not give error.
if I delete printf line id does not give error.

What I have tried:

<pre>#define MIN3(a, b, c) ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c)))
static void levenshtein(char *s1, char *s2) {
      int x, y, s1len, s2len,similarity;
    s1len = strlen(s1);
    s2len = strlen(s2);
	printf("string1= %s \nstring2= %s \n",s1,s2);
      int matrix[s2len+1][s1len+1];
    matrix[0][0] = 0;

    for (x = 1; x <= s2len; x++)
        matrix[x][0] = matrix[x-1][0] + 1;
  for (y = 1; y <= s1len; y++)
        matrix[0][y] = matrix[0][y-1] + 1;
      for (x = 1; x <= s2len; x++)
        for (y = 1; y <= s1len; y++)
            matrix[x][y] = MIN3(matrix[x-1][y] + 1, matrix[x][y-1] + 1, matrix[x-1][y-1] + (s1[y-1] == s2[x-1] ? 0 : 1));
        similarity = matrix[s2len][s1len];
       printf("result = %d \n", similarity) ;  
}







static void
recv_uc(struct unicast_conn *c, const linkaddr_t *from)
{ 

printf("Request received from %d.%d: '%s'\n", from->u8[0], from->u8[1], (char *)packetbuf_dataptr());
numberofrecv = numberofrecv + 1; 

struct record *n; 

for(n = list_head(recv_record_list); n != NULL; n = n->next)
{ 
if(n->next == NULL){
printf("last record = %s \n",n->message );
levenshtein((char *)packetbuf_dataptr(),n->message);

}

}

推荐答案

这是调试器帮助你的地方。

在函数的第一行放置一个断点,然后通过调试器运行代码。然后查看您的代码,数据,并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


我们无法确切地告诉您如何做到这一点 - 我们不知道您使用的是哪种系统 - 但是您的开发环境名称和调试器的快速Google应该会为您提供信息。



很抱歉,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
This is where the debugger help you out.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, at your data, and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

We can't tell you exactly how to do that - we have no idea what systems you are using - but a quick Google for the name of your development environment and "debugger" should get you the information.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


这篇关于printf中的超出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆