2013-07-23 61 views

回答

1

下面的例子顯示溢出,因爲在循環每次繼續增加的時間,這意味着它超越了內存緩衝限制,thts爲什麼會出現溢出。

int count = 2147483647; 
NSString *str = @""; 
for (int i = 0; i < count; i++) { 
    @try { 
     str = [str stringByAppendingString:@"\n"];//NSString stringWithFormat:@"%@%@", str , @"\n" 
    } 
    @catch (NSException *exceptions) { 
     NSLog(@"end after %d loops (%@)", i, [exceptions description]); 
    } 
} 

see this link to more

+0

有多大沒你的字符串得到什麼?我已經與NSStrings一樣大到13GB,根本沒有遇到任何問題。 –

+2

stringByAppendingString返回一個自動釋放的字符串,所以這實際上是使用比str的當前長度更多的內存(大約i^2/2多),因爲所有創建的字符串在runloop之後纔會被釋放。 – cncool