开发者

memcpy causing 'exc bad access'

开发者 https://www.devze.com 2023-04-10 15:39 出处:网络
I\'m trying to loop through an array and copy across data, but after 1023 loops, I get an exc bad access message thrown and I have a feeling it might be to do with my memory.

I'm trying to loop through an array and copy across data, but after 1023 loops, I get an exc bad access message thrown and I have a feeling it might be to do with my memory. In my loop, I need to append data to my totalValues array, so I did this:

memcpy(totalValues + totalCopied, tempBuffer, 600 * sizeof(float));

This is done inside a loop and totalCopied keeps track of how much data has been appended to totalValues so that I know where to write from when the loop hits memcpy again. I'm not sure why I get the "exc bad access" error, but my theory is that the memory is not contiguous and, therefore, the totalValues + totalCopied line might be causing trouble. I'm not sure if an error would be thrown in this case, or if the memory would just be overwritten anyway. The interesting thing is, it always occurs after 1023 loops. If I remove the 'memcpy' line, the program loops through without any problems. Any ideas what could be causing this?

EDIT - The cause was that the memory allocation was hard coded for another file. Normally, I won'开发者_开发问答t know the length of the file before the memory allocation, so how can I ensure that enough memory is allocated at runtime?


Sounds like you're writing more bytes than totalValues can contain. Show us how you're allocating it.

Incidentally, we usually do this kind of thing with NSData objects on iOS.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号