I think I find a bug

Post Reply
1989lzhh
Posts: 3
Joined: Sat Sep 25, 2010 3:43 pm

I think I find a bug

Post by 1989lzhh » Sun Oct 10, 2010 8:53 am

Code: Select all

#include <stdio.h>
#include <nds.h>
#include <malloc.h>

u16 m[1000]={0};

void WriteRAM(u32 address,const u8* buffer,u32 size)
{       u32 i=0;
	u16* addr=m+address;  
	u16* pData=(u16*)buffer;   
	for(i=0;i<size;i+=2)   
	{      
		addr[i>>1]=pData[i>>1];
	}		
}
void ReadRAM(u32 address,u8* buffer,u32 size)
{
	u32 i=0;
	u16* addr=m+address;  
	u16* pData=(u16*)buffer;
	for(i=0;i<size;i+=2)
	{
		pData[i>>1]=addr[i>>1];   
//Here!The ReadRAM function will work fine if I add i++,i--
                }
//Here!The ReadRAM function will work fine if I put a iprintf() blew.
//iprintf("");
}

//---------------------------------------------------------------------------------
int main(void) {
//---------------------------------------------------------------------------------
    consoleDemoInit();
    int in[3]={10,1100000,20};
    int out[3]={0};
    WriteRAM(0,(u8*)in,sizeof(int)*3);
    ReadRAM(0,(u8*)out,sizeof(int)*3);
    iprintf("%d %d %d\n",out[0],out[1],out[2]);
    while(1);
   
}
I compiled it with devkitARM r31 & libnds1.4.6. In the ReadRAM function code ,If I didn't put a iprintf() at the end,the function didn't work and iprintf("%d %d %d\n",out[0],out[1],out[2]); will get 0 0 0.

WinterMute
Site Admin
Posts: 1478
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: I think I find a bug

Post by WinterMute » Sun Oct 10, 2010 11:34 am

You're passing a pointer to the out array into the ReadRAM function and not marking the memory it points to as volatile. The compiler then assumes you're not changing the memory. Using volatile int out[3]; makes everything work as expected.
Help keep devkitPro toolchains free, Donate today

Personal Blog

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest