Access violation on copying array contents. | | |
Dear experts:
Hope you can point my error here. This function of mine receives an
array of length 32 and then the for loop adds some scrambled characters
at patterns. I think, I can not read the array w[] beyond 32 , but dont
know how to achieve the result. Thanks a lot for help !
void scramble( char *w, int key)
{
x = key; // key = (0-9)
int len = strlen(w);
cout << "length is:" << len << endl; // 32
len = (len + len/x); // 48
cout << "length is:" << len << endl;
char s[] = "@#$"; //array to hold scramble characters
int n = strlen(s);
int i,j,k,m=0;
char * c;
c = new char[len+1];
memset(c, 0, sizeof(c));
//if (c == NULL) exit(1);
for (i=0,j=0,k=0; i<len-1; i++){
if(k<x) {
c[j++] = w[i];k++;
}
else {
if(m<n)
c[j++] = s[m++];
else {
c[j++] = s[0]; m = 1;}
c[j++]=w[i]; k=1; }
}
cout << "scrambled output is :" << c << endl;
delete c;
}
The output is this, which is desired but it has run time error as
mentioned.
input is : ba ced fhgi kzl nmpo qsr tvuxwzy
scrambled output is :ba@ c#ed$ f@hg#i $kz@l #nm$po@ q#sr$ t@vu#xw$zy@ | | | | re: Access violation on copying array contents.
dbuser wrote:[color=blue]
> Hope you can point my error here. This function of mine receives an
> array of length 32 and then the for loop adds some scrambled characters
> at patterns. I think, I can not read the array w[] beyond 32 , but dont
> know how to achieve the result. Thanks a lot for help !
>
> void scramble( char *w, int key)[/color]
If 'w' is the array the bounds of which you're not supposed to cross,
then this function needs to know the bounds -- pass the actual size of
that array into the function, as another argument.
[color=blue]
> {
> x = key; // key = (0-9)[/color]
Where is 'x' declared? Is it a global variable?
[color=blue]
> int len = strlen(w);
> cout << "length is:" << len << endl; // 32[/color]
Is '32' what it prints out?
[color=blue]
> len = (len + len/x); // 48[/color]
What does '48' mean? x == 2? What is 'len' now?
[color=blue]
> cout << "length is:" << len << endl;[/color]
Does it print 48 here?
[color=blue]
> char s[] = "@#$"; //array to hold scramble characters[/color]
Four letters is the size of your 's' array.
[color=blue]
> int n = strlen(s);[/color]
n is 3.
[color=blue]
> int i,j,k,m=0;
> char * c;[/color]
Why not merge the two statements into one?
[color=blue]
> c = new char[len+1];
> memset(c, 0, sizeof(c));[/color]
sizeof(c) is 4. You really need to do 'memset(c, 0, len+1)' here.
[color=blue]
> //if (c == NULL) exit(1);[/color]
^^^^^^^^^^^^^^^^^^^^^^^^^
Get rid of this comment, it has no value.
[color=blue]
> for (i=0,j=0,k=0; i<len-1; i++){
> if(k<x) {
> c[j++] = w[i];k++;[/color]
So, you copy the characters if 'k' is smaller than the 'key'. IOW, you
leave up to 'key' characters unchanged.
[color=blue]
> }
> else {[/color]
Otherwise...
[color=blue]
> if(m<n)
> c[j++] = s[m++];[/color]
You insert a scrambled character until they are all used up (m>=n)
[color=blue]
> else {
> c[j++] = s[0]; m = 1;}[/color]
Otherwise you start over...
[color=blue]
> c[j++]=w[i]; k=1; }[/color]
... and still append the source character...
[color=blue]
> }
> cout << "scrambled output is :" << c << endl;
> delete c;[/color]
delete[] c;
[color=blue]
> }
>
> The output is this, which is desired but it has run time error as
> mentioned.
> input is : ba ced fhgi kzl nmpo qsr tvuxwzy
> scrambled output is :ba@ c#ed$ f@hg#i $kz@l #nm$po@ q#sr$ t@vu#xw$zy@[/color]
Well, your calculation of 'len' may be a bit flawed. Try adding 1 to it.
V | | | | re: Access violation on copying array contents.
dbuser wrote:[color=blue]
> Dear experts:
> Hope you can point my error here. This function of mine receives an
> array of length 32 and then the for loop adds some scrambled characters
> at patterns. I think, I can not read the array w[] beyond 32 , but dont
> know how to achieve the result. Thanks a lot for help !
>
> void scramble( char *w, int key)
> {
> x = key; // key = (0-9)
> int len = strlen(w);
> cout << "length is:" << len << endl; // 32
> len = (len + len/x); // 48[/color]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ replace with
int newLen = (len + len/x);[color=blue]
> cout << "length is:" << len << endl;
> char s[] = "@#$"; //array to hold scramble characters
> int n = strlen(s);
> int i,j,k,m=0;
> char * c;
> c = new char[len+1];[/color]
^^^^^^^^^^^^^^^^^^^ c = new char[newLen + 1];[color=blue]
> memset(c, 0, sizeof(c));[/color]
^^^^^^^ sizeof(c) is 4 on 32 bit machines
memset(c, 0, newLen + 1);
[color=blue]
> //if (c == NULL) exit(1);
> for (i=0,j=0,k=0; i<len-1; i++){
> if(k<x) {
> c[j++] = w[i];k++;
> }
> else {
> if(m<n)
> c[j++] = s[m++];
> else {
> c[j++] = s[0]; m = 1;}
> c[j++]=w[i]; k=1; }
> }
> cout << "scrambled output is :" << c << endl;
> delete c;[/color]
^^^^^^^^^^^^^^^^^^^^
delete[] c;
[color=blue]
> }
>
> The output is this, which is desired but it has run time error as
> mentioned.
> input is : ba ced fhgi kzl nmpo qsr tvuxwzy
> scrambled output is :ba@ c#ed$ f@hg#i $kz@l #nm$po@ q#sr$ t@vu#xw$zy@[/color]
work on the indentation style. also, write one statement per line.
your for loop was running 48 times, and J was incremented 2 some times
(that is how it works) so you were writing past the end of newly
allocated memory for c
/dan | | | | re: Access violation on copying array contents.
Thanks a lot for your valuable suggestions. I corrected mistakes but to
fix run time error, I had to read source array upto its own length like
this:
for (i=0,j=0,k=0,p=0; i<newLen,p<len; i++,p++){
if(k<x) {
c[j++] = w[p];k++;
}
else {
if(m<n)
c[j++] = s[m++];
else {
c[j++] = s[0];
m = 1;
}
c[j++]=w[p];
k=1;
}
} | | | | re: Access violation on copying array contents.
dbuser schrieb:[color=blue]
> Thanks a lot for your valuable suggestions. I corrected mistakes but to
> fix run time error, I had to read source array upto its own length like
> this:
> for (i=0,j=0,k=0,p=0; i<newLen,p<len; i++,p++){[/color]
^^^^^^^^^^^^^^
Do you mean (i<newLen && p<len) or (i<newLen || p<len)?
The comma operator throws away the result of the first part.
[color=blue]
> if(k<x) {
> c[j++] = w[p];k++;
> }
> else {
> if(m<n)
> c[j++] = s[m++];
> else {
> c[j++] = s[0];
> m = 1;
> }
> c[j++]=w[p];
> k=1;
> }
> }
>[/color]
Th. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|