久久久久久久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(發布版本與調試版本的運行方式不同的一些原因是什么)
主站蜘蛛池模板: 精品国产欧美一区二区 | 久久国产精品久久久久久久久久 | 一区二区三区国产精品 | 91在线一区二区 | 日中文字幕在线 | 九七午夜剧场福利写真 | 久久久久无码国产精品一区 | 国产一区不卡 | www.日韩 | 日韩国产免费观看 | 国产精品视频网站 | 国产一区二区三区色淫影院 | 日韩欧美亚洲 | 精品一区二区观看 | www.日韩系列 | 精品欧美 | 日韩一级精品视频在线观看 | 福利社午夜影院 | av第一页 | 99这里只有精品视频 | 性一交一乱一透一a级 | 国产高清视频一区 | 亚洲一卡二卡 | h在线| 国产高清视频一区二区 | 蜜桃视频在线观看免费视频网站www | 国产在线中文字幕 | 免费在线a视频 | 久久高清| 国产乱码久久久久久 | 成年人免费网站 | 久久不卡视频 | 在线观看国产视频 | 久久久久国产精品一区 | 一区二区三区视频播放 | 91原创视频在线观看 | 懂色tv| 欧美综合在线视频 | 波多野结衣在线观看一区二区三区 | 日韩精品三区 | 国产精品久久久久永久免费观看 |