www.digitalmars.com         C & C++   DMDScript  

D - Access Violation (WinXP)

reply "Andrew Edwards" <crxace13 nospam.comcast.net> writes:
The following compiles and links sucessfully but causes a runtime "access
violation" error while trying to access t. Any ideas?

int main()
{
    char[] t;
    for(int i=0; i < 10; i++){
        if(i==0) t="time"; else t="times";
        printf("%d %.s around\n", i, t);
    }
    return 0;
}

This version doesn't increment i before iterating through the loop:

int main()
{
    char[] t;
    for(int i=0; i <= 10; ++i){
        if(i==1) t="time"; else t="times";
        printf("%d %.s around\n", i, t);
    }
    return 0;
}
Nov 21 2002
parent reply J C Calvarese <jcc-47 excite.com> writes:
Andrew,

I casted t to a char*.
   printf("%d %.s around\n", i, cast(char*) t);

This got rid of the access violation, but "time(s)" didn't print.

Also, I though maybe a null needed to be appended as shown below, but it 
  didn't work any better.

   printf("%d %.s around\n", i, cast(char*) (t ~ \x00));

Finally I imported strings from Phobos and used toString for converting 
i to a string.  This seemed to work, but I suspect there's another (more 
direct) solution.


   import string;

   int main()
   {
	char[] t;
	for(int i=0; i < 10; i++){
		if(i==0) t="time"; else t="times";
		printf(toString(i) ~ " " ~  t  ~  " around\n");
	}
	return 0;
   }


Justin


Andrew Edwards wrote:
 The following compiles and links sucessfully but causes a runtime "access
 violation" error while trying to access t. Any ideas?
 
 int main()
 {
     char[] t;
     for(int i=0; i < 10; i++){
         if(i==0) t="time"; else t="times";
         printf("%d %.s around\n", i, t);
     }
     return 0;
 }
 
 This version doesn't increment i before iterating through the loop:
 
 int main()
 {
     char[] t;
     for(int i=0; i <= 10; ++i){
         if(i==1) t="time"; else t="times";
         printf("%d %.s around\n", i, t);
     }
     return 0;
 }
 
 
Nov 21 2002
parent reply Lloyd Dupont <lloyd galador.net> writes:
what about
printf("%d %.*s around\n", i, t);
as advised in the documentation ?

J C Calvarese wrote:

 Andrew,

 I casted t to a char*.
   printf("%d %.s around\n", i, cast(char*) t);

 This got rid of the access violation, but "time(s)" didn't print.

 Also, I though maybe a null needed to be appended as shown below, but it
  didn't work any better.

   printf("%d %.s around\n", i, cast(char*) (t ~ \x00));

 Finally I imported strings from Phobos and used toString for converting
 i to a string.  This seemed to work, but I suspect there's another (more
 direct) solution.


   import string;

   int main()
   {
     char[] t;
     for(int i=0; i < 10; i++){
         if(i==0) t="time"; else t="times";
         printf(toString(i) ~ " " ~  t  ~  " around\n");
     }
     return 0;
   }


 Justin


 Andrew Edwards wrote:

 The following compiles and links sucessfully but causes a runtime 
"access
 violation" error while trying to access t. Any ideas?

 int main()
 {
     char[] t;
     for(int i=0; i < 10; i++){
         if(i==0) t="time"; else t="times";
         printf("%d %.s around\n", i, t);
     }
     return 0;
 }

 This version doesn't increment i before iterating through the loop:

 int main()
 {
     char[] t;
     for(int i=0; i <= 10; ++i){
         if(i==1) t="time"; else t="times";
         printf("%d %.s around\n", i, t);
     }
     return 0;
 }
Nov 21 2002
parent J C Calvarese <jcc-47 excite.com> writes:
I guess I forgot to read the directions.

Thanks.  I knew there was a direct solution.  I haven't worked much in 
C so all of these printf codes (%d, %s, etc.) are kind of foreign to me.

Justin

Lloyd Dupont wrote:
 what about
 printf("%d %.*s around\n", i, t);
 as advised in the documentation ?
Nov 21 2002