I don't know much about the iMac. R's .C() passes R-language integers as pointers to 32-bit ints. If your iMac is 64-bit and sizeof(long)==8 in your compiler (pretty common for 64-bit compilers but not so for Microsoft Windows compilers) then Long[0] will use the next 64 bits to make an integral value out of while Int[0] will only use the next 32 bits. Since R only passes 32 bits/integer your Long[0] would include 32 random bits in its value and using it as a subscript would often point to a place in memory that you don't own.
Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com ________________________________ From: David [mailto:david.mailli...@gmail.com] Sent: Wednesday, October 13, 2010 11:13 AM To: William Dunlap Cc: r-help@r-project.org Subject: Re: [R] "Memory not mapped" when using .C,problem in Mac but not in Linux Thank you very much for your kind reply. I used gdb, and it returns a reason "KERN_INVALID_ADDRESS" on a very simple operation (obtaining the time step for a numerical integration). Please see bellow. But, um, I solved it by changing the function's arguments in the C code from "unsigned long int" to "int". In R, those arguments were defined with "as.integer". So, can you now understand why the error happens (on iMac and not on Linux)? If yes, can you explain it to me? :-) If some argument is defined in C as "unsigned long int", I see that the R equivalent is *not* "as.integer". Which is it? Thank you very much in advance, David Can you please Cc to me any replies, just in case I may miss any of them among the whole amount of emails :-) ? --------------------------------------- Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_INVALID_ADDRESS at address: 0x0000000902ef3d68 [...] 139 double time_step=(times[number_times[0]-1]-times[0])/(number_timesteps[0]); (note: "number_times" and "number_timesteps" are integer arrays with a single component, because they are arguments of the C function that need to be passed from R. They were defined as "unsigned long int" in C, and "as.integer" in R, giving the error. If I define them as "int" in C, the error disappears). 2010/10/13 William Dunlap <wdun...@tibco.com> This often happens when your C code uses memory that it did not allocate, particularly when it reads or writes just a little beyond the end of a memory block. On some platforms or if you are lucky there is unused memory between blocks of allocated memory and you don't see a problem. Other machines may pack allocated memory blocks more tightly and writing off the end of an array corrupts another array, leading to the program crashing. (Or reading off the end of an array may pick up data from the next array, which leads to similar crashes later on.) You may also be reading memory which has never been written to, hence you are using random data, which could corrupt things. Use a program like valgrind (on Linux, free) or Purify (on various platforms, but pricy) to detect memory misuse in C/C++ code. As long as your code isn't #ifdef'ed for various platforms, you can use valgrind to detect and fix memory misuse on Linux and you will find that overt problems on other platforms will go away. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of David > Sent: Wednesday, October 13, 2010 6:47 AM > To: r-help@r-project.org > Subject: [R] "Memory not mapped" when using .C,problem in Mac > but not in Linux > > Hello, > > I am aware this may be an obscure problem difficult to advice > about, but > just in case... I am calling a C function from R on an iMac > (almost shining: > bought by my institution this year) and gives a "memory not > mapped" error. > > Nevertheless, exactly the same code runs without problem in a powerful > computer using SuSE Linux, but also on my laptop of 2007, 32 > bits, 2 GB RAM, > running Ubuntu. My supervisor says that he can run my code on > his iMac (a > bit older than mine) without problem. > > I have upgraded to the latest version of R, and I have tried > compiling the C > code with the latest version of gcc (obtained from MacPorts), > but the error > persists. > > Any ideas? > > Thank you very much in advance, > > David > Can you please Cc to me any replies, just in case I may miss > any of them > among the whole amount of emails :-) ? > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.