Discussion in "Software" started by    bhagirath    Jul 5, 2011.
Tue Jul 05 2011, 09:49 am
#1
Hi all,

I am a newbie to Keil. I was just working in one demo project and stuck in between due to some access violation error. I tried googleing to various forums but didn't work, also tried applying map command but no success may be anyone could provide proper steps to do so. So could can one please help me in this regards? My code is as follows

//start
#include<reg51.h>

#include<string.h>


char var1[1];
char var2[1];
char var3[1];
char var4[5];
char var5[5];
char var6[1];
char var7[1];

void main()
{
    while(1)
    {
        char str[]= "123456789123456";
        int idx1 = 0;
        int index2 = 1;
        strncpy (var1,str+idx1,index2);
        idx1+=1;
        index2+=1;
        strncpy (var2,str+idx1,index2);
        idx1+=1;
        index2+=1;
        strncpy (var3,str+idx1,index2);
        idx1+=5;
        index2+=5;
        strncpy (var4,str+idx1,index2);
        idx1+=1;
        index2+=5;
        strncpy (var5,str+idx1,index2);
        idx1+=5;
        index2+=1;
        strncpy (var6,str+idx1,index2);
        idx1+=1;
        index2+=1;
        strncpy (var7,str+idx1,index2);
    }
}
//end


The error comes while debugging the code, here any where in between it stops debugging and throws error 65 access violation error.
If possible please reply me with steps to resolve this. Let me know if you want further information.

Thanks in advance

Info: Keil version 3.0 eval, hardware : AT89S52
Tue Jul 05 2011, 11:52 pm
#2
For a start,to find an error remove all the code you can until the error disappears.
In this case everything in your while loop.
Then put back a bit at a time until the error comes back .
This tells you where to look.

Your error(s) come from the use of strncpy.
Most of your target buffers are only one byte long and yet you are often trying to copy
more than one byte into them.
This means you are "violating" memory space you "don't own"



[ Edited Wed Jul 06 2011, 08:58 pm ]
Wed Jul 06 2011, 01:22 pm
#3
Hi ExperimenterUK,

Thank you very much for reply,

As per your advice I have removed all the multiple characters trimming, now I am doing only on single character, but then also I am getting the same error. I had googled on this error and some where it was mentioned to keep the code in single loop thats why i have kept it under while(1).

#include<reg51.h>

#include<string.h>


char var1[1];
char var2[1];
char var3[1];
char var4[1];
char var5[1];
char var6[1];
char var7[1];

void main()
{
    while(1)
    {
        char str[]= "123456789123456";
        unsigned char idx1 = 0;
        unsigned char index2 = 1;
        strncpy (var1,str+idx1,index2);
        idx1+=1;
        index2+=1;
        strncpy (var2,str+idx1,index2);
        idx1+=1;
        index2+=1;
        strncpy (var3,str+idx1,index2);
		idx1+=1;
        index2+=1;
        strncpy (var4,str+idx1,index2);
        idx1+=1;
        index2+=1;
        strncpy (var5,str+idx1,index2);// here the location of error..
        idx1+=1;
        index2+=1;
        strncpy (var6,str+idx1,index2);
        idx1+=1;
        index2+=1;
       	strncpy (var7,str+idx1,index2);
		break;
    }
}


I will be really grateful to you if you could try this in your environment.

According to my requirement I need to perform substring function on one string, thats why i have kept strncpy().

Thanks once again.
Wed Jul 06 2011, 02:01 pm
#4
I tried your code on My keil, it didnt showed any error,
Check your keil with other programs(simple one)
Wed Jul 06 2011, 02:46 pm
#5
Hi kirangowle,

I am getting error on debugging the code line by line. The code compiles fine with keil. the only issue
is while debugging it throws error for access violation.

Could you please try debugging the code?

Thanks
Bhagirath
Wed Jul 06 2011, 03:52 pm
#6
Hi,
I debugged your code, it was getting halt at strncpy(var3,)

In array declaration i increased the array length to 5(var[5]) and keep the local variable declaration outside of the while(1).
Wed Jul 06 2011, 05:01 pm
#7
Hi Kirangowle,

Thanks for reply,

I have changed the code as you have suggested, for performance and testing issue i have changed the capacity for each var to 1 byte. But no success, in debugging it stops at var5, after which i have noticed that all variables are cleared which were holding their values before error. here is my changed code

char var1[1];
char var2[1];
///...
char var7[1];
char str[]= "123456789123456";
char idx1 = 0;
char index2 = 1;

void main()
{

    while(1)
    {
        strncpy (var1,str+idx1,index2);
        idx1+=1;
        index2+=1;
        strncpy (var2,str+idx1,index2);
        idx1+=1;
        index2+=1;
        strncpy (var3,str+idx1,index2);
		idx1+=1;
        index2+=1;
        strncpy (var4,str+idx1,index2);
        idx1+=1;
        index2+=1;
        strncpy (var5,str+idx1,index2);
        idx1+=1;
        index2+=1;
        strncpy (var6,str+idx1,index2);
        idx1+=1;
        index2+=1;
       	strncpy (var7,str+idx1,index2);
		
    }
}


Wed Jul 06 2011, 05:56 pm
#8
You didnt change the array length from var[1] to var[5].
i am not understanding what basically u are try to achieve .
Wed Jul 06 2011, 07:04 pm
#9
Hi,

Let me describe my workflow here, I want to substring the "str" in different segments. Let say that segments would be like
var1 = first character
var2 = 2nd - 4th
var3 = 5th - 8th
like wise, it is my custom requirement.

In given example i have created var1[1] - var7[1] just to check, because it was some times showing error in array with bigger ranges. Actually it will be as describe above.e.g.
var1[1], var2[3], var2[4], var4[1]....

Hope this clears it..

And in doing so keil is throwing the error of either access violation or it stops debugging.
Wed Jul 06 2011, 09:21 pm
#10


Hi ExperimenterUK,
As per your advice I have removed all the multiple characters trimming, now I am doing only on single character,

bhagirath


Actually I meant remove all the statements,then put back one statement at a time.


I had googled on this error and some where it was mentioned to keep the code in single
loop thats why i have kept it under while(1).

bhagirath


Whatever you Googled is not relevent here,remove the while() loop, or at least move it to the end

Remove this line for now " index2+=1; " it makes you copy more bytes each time


Try this
#include<reg51.h>
#include<string.h>
char var1[1];
char var2[1];
char var3[1];

void main()
{

    
        char str[]= "123456789123456";
        unsigned char idx1 = 0;
        unsigned char index2 = 1;
        strncpy (var1,str+idx1,index2);  //this should work
        idx1+=1;
 //;  index2+=1; don't do this as it copies 2 bytes next time

        strncpy (var2,str+idx1,index2);   //this should work
        idx1+=1;
        index2+=1;
        strncpy (var3,str+idx1,index2);   //this should not  work
//;		break;  // ??? not needed
    while(1);

}


Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Lewisuhakeply
Thu Apr 18 2024, 06:00 pm
Darrellciz
Thu Apr 18 2024, 11:07 am
Charlessber
Thu Apr 18 2024, 09:29 am
BartonSem
Thu Apr 18 2024, 04:56 am
DonaldKnown
Thu Apr 18 2024, 12:24 am
utaletxcyw
Wed Apr 17 2024, 10:21 am
Anthonyvab
Wed Apr 17 2024, 08:48 am
RobertCix
Wed Apr 17 2024, 06:46 am