久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

當大小是一個變量而不是一個常量時創建一個數

Create an array when the size is a variable not a constant(當大小是一個變量而不是一個常量時創建一個數組)
本文介紹了當大小是一個變量而不是一個常量時創建一個數組的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

這是程序:

int siz = 0;
int n = 0;
FILE* picture;

picture = fopen("test.jpg", "r");
fseek(picture, 0, SEEK_END);
siz = ftell(picture);

char Sbuf[siz];
fseek(picture, 0, SEEK_SET); //Going to the beginning of the file
while (!feof(picture)) {
    n = fread(Sbuf, sizeof(char), siz, picture);
    /* ... do stuff with the buffer ... */
    /* memset(Sbuf, 0, sizeof(Sbuf)); 
}

我需要讀取文件大小.我確信這段代碼是在另一個編譯器上編譯的.如何正確聲明 siz 以便代碼編譯?

I need to read the file size. I know for sure that this code compiled on another compiler. How to correctly declare siz correctly so that the code compiles?

推薦答案

沒有正確的方法可以做到這一點,因為具有任何可變長度數組的程序是 格式錯誤.

There is no proper way to do this, as a program with any variable length array is ill-formed.

可以說,可變長度數組的替代方案是 std::vector:

An alternative, so to speak, to a variable length array is a std::vector:

std::vector<char> Sbuf;

Sbuf.push_back(someChar);

當然,我應該提一下,如果您特別使用 charstd::string 可能適合你.以下是一些如何使用 std::string 的示例,如果你有興趣.

Of course, I should mention that if you are using char specifically, std::string might work well for you. Here are some examples of how to use std::string, if you're interested.

可變長度數組的另一種替代方法是 new 操作符/關鍵字,盡管 std::vector 如果你可以使用它通常會更好:

The other alternative to a variable length array is the new operator/keyword, although std::vector is usually better if you can make use of it:

char* Sbuf = new char[siz];

delete [] Sbuf;

但是,此解決方案確實存在內存泄漏的風險.因此,std::vector 是首選.

However, this solution does risk memory leaks. Thus, std::vector is preferred.

這篇關于當大小是一個變量而不是一個常量時創建一個數組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How do I set the icon for my application in visual studio 2008?(如何在 Visual Studio 2008 中為我的應用程序設置圖標?)
Convert CString to const char*(將 CString 轉換為 const char*)
Remove secure warnings (_CRT_SECURE_NO_WARNINGS) from projects by default in Visual Studio(默認情況下,在 Visual Studio 中從項目中刪除安全警告 (_CRT_SECURE_NO_WARNINGS))
How do I start a new CUDA project in Visual Studio 2008?(如何在 Visual Studio 2008 中啟動新的 CUDA 項目?)
Exporting classes containing `std::` objects (vector, map etc.) from a DLL(從 DLL 導出包含 `std::` 對象(向量、映射等)的類)
What are some reasons a Release build would run differently than a Debug build(發布版本與調試版本的運行方式不同的一些原因是什么)
主站蜘蛛池模板: 免费黄色小网站 | xxxx色| 国产黄色片在线观看 | 看真人一级毛片 | 婷婷六月综合 | 在线观看免费毛片 | 黄色大片免费在线观看 | 特黄毛片| 日韩免费看 | 91最新视频| 这里只有精品视频在线观看 | 国产午夜视频 | 午夜久久久久久久 | 亚洲成人免费观看 | 福利片在线观看 | 国产成人精品一区二区三区视频 | 久久久黄色片 | 久久免费看片 | 欧美成人精品一区二区三区 | www.97超碰 | 午夜视频网站 | 日本特级淫片 | 2014天堂网 | 色婷婷综合在线 | 天天做夜夜爽 | 色一区二区三区 | 中文字幕在线看 | 超碰com| 麻豆三级视频 | 国产精品久久久久久久免费看 | 欧美黄色一级大片 | 欧美日本 | www.久久| 亚洲精品成人在线 | 春色导航 | 特级黄色大片 | 日本少妇中文字幕 | 精东影业一区二区三区 | 欧美在线视频免费 | 成人一区二区视频 | 欧美三级韩国三级日本三斤在线观看 |