int LA [] = {1,2,3,4,5}内存分配混乱c

时间:2016-09-14 07:06:39

标签: c arrays loops memory

我观察到为阵列分配的内存似乎是动态的。

以下是我在tutorial中找到的示例代码:

The original array elements are :
LA[0]=1 
LA[1]=3 
LA[2]=5 
LA[3]=7 
LA[4]=8 
The array elements after insertion :
LA[0]=1 
LA[1]=3 
LA[2]=5 
LA[3]=10 
LA[4]=7 
LA[5]=8

和样本输出:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier0, forIndexPath: indexPath) as! CelebrityHeaderCell
    let CelebrityTopObj = self.mCelebrityTop[indexPath.item]

    if CelebrityTopObj.video_pic != nil
    {
        SDWebImageManager.sharedManager().downloadImageWithURL(NSURL(string: CelebrityTopObj.video_pic), options: .LowPriority, progress: { (min:Int, max:Int) -> Void in
        }) { (vimage:UIImage!, error:NSError!, cacheType:SDImageCacheType, finished:Bool, url:NSURL!) -> Void in
            if vimage != nil && finished
            {
                if getScreenHeight()<=568 {
                    cell.VideoImg.image = imageResize(vimage, sizeChange: CGSize(width: self.screenWidth/2.5, height: self.screenWidth/3))
                    cell.PrevIcon.image = imageResize(UIImage(named: "icon_prev")!, sizeChange: CGSize(width: 25, height: 25))
                    cell.NextIcon.image = imageResize(UIImage(named: "icon_next")!, sizeChange: CGSize(width: 25, height: 25))
                    cell.TopName.font = cell.TopName.font.fontWithSize(15)
                    cell.PrevIcon.frame.origin.x = 10
                    cell.PrevIcon.frame.origin.y = 30
                    cell.PrevIcon.frame = CGRect(x: 10, y: 30, width: 25, height: 25)

                } else {
                    cell.VideoImg.image = imageResize(vimage, sizeChange: CGSize(width: self.screenWidth/2, height: 100))
                    cell.PrevIcon.image = imageResize(UIImage(named: "icon_prev")!, sizeChange: CGSize(width: 55, height: 55))
                    cell.NextIcon.image = imageResize(UIImage(named: "icon_next")!, sizeChange: CGSize(width: 55, height: 55))

                }
            }
        }
    }
    return cell 
}

我的工作原理如何。

3 个答案:

答案 0 :(得分:5)

首先,对于没有显式大小定义并使用大括号括起初始化程序初始化的数组的一般语句,大小将取决于初始化程序列表中的元素。所以,对于你的阵列

 int LA[] = {1,3,5,7,8};

尺寸为5,因为您有5个元素。

C使用基于0的数组索引,因此有效访问将为0到4。

在您的代码中

 LA[j+1] = LA[j];

尝试访问超出绑定访问权限的索引6,(5+1)。这会调用undefined behavior

输出具有UB的代码无法以任何方式证明。

尽管如此,根据最新的C标准,main()在技术上是无效的签名。您至少需要使用int main(void)来使代码符合托管环境。

答案 1 :(得分:2)

代码有一个缓冲区溢出错误! C中的数组无法扩展!在声明/定义它时需要分配足够的空间。

您可以通过在声明中提供尺寸来声明额外的空间:

int LA[10] = {1,3,5,7,8};

LA现在可以容纳10个索引为0到9的元素。

如果您想要更多灵活性,则应使用指针malloc / calloc / realloc来分配内存。

注意:

复制中存在第二个错误。循环开始一步太远了。

j从5开始并指定索引j+1,代码会分配LA[6],这是第7个元素。插入后只有6个元素。

我从这两个错误中得出的结论是,教程既不是由经验丰富的C程序员编写也不是由经验丰富的C程序员审阅。

答案 2 :(得分:0)

要添加其他答案,C / C ++不会对数组进行任何边界检查。

在这种情况下,你有一个堆栈分配的数组,所以只要你的索引没有留下堆栈空间,就没有&#34;错误&#34;在运行时。但是,由于您要离开数组的边界,如果它的内存位置恰好位于分配的数组之后,您可能最终会更改也在堆栈中分配的其他变量的值。这是缓冲区溢出的危险之一,并且可能在更复杂的程序中导致非常糟糕的事情发生。