造成的memcpy“EXC坏的访问” [英] memcpy causing 'exc bad access'

查看:204
本文介绍了造成的memcpy“EXC坏的访问”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是通过一个数组试图环和跨数据复制,但经过1023圈,我得到一个 EXC坏访问消息抛出,我有一种感觉它可能与我记忆的事情。
在我的循环,我需要将数据追加到我的 totalValues​​ 数组,所以我这样做:

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));

这是一个循环内完成, totalCopied 跟踪有多少数据被追加到 totalValues​​ 使我知道从哪里当循环再次命中写MEMCPY。我不知道为什么我得到的坏EXC访问错误,但我的理论是,内存是不连续的,因此,在 totalValues​​ + totalCopied 线可能造成麻烦。我不知道如果有错误将在此情况下被抛出,或者如果内存也只是无论如何覆盖。有趣的是,它总是经过1023循环发生。如果我删除的memcpy行,程序遍历没有任何问题。任何想法可能会导致这?

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?

编辑 - 原因是,内存分配是另一个文件硬codeD。通常情况下,我不知道内存分配前的文件的长度,所以我怎么能保证足够的内存在运行时分配?

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?

推荐答案

听起来你正在写比totalValues​​可以包含多个字节。告诉我们你是如何分配的。

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

顺便说一句,我们通常做这种事情与iOS上的NSData对象。

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

这篇关于造成的memcpy“EXC坏的访问”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆