Bagaimanakah Saya Boleh Mencipta Tatasusunan Bersaiz Dinamik dalam C untuk Mengendalikan Fail Saiz Tidak Diketahui?

Barbara Streisand
Lepaskan: 2024-11-14 20:59:02
asal
960 orang telah melayarinya

How Can I Create Dynamically Sized Arrays in C to Handle Files of Unknown Size?

Dynamically Sized Arrays in C Language

Consider the following C code:

<br>int siz = 0;<br>int n = 0;<br>FILE* picture;</p>
<p>picture = fopen("test.jpg", "r");<br>fseek(picture, 0, SEEK_END);<br>siz = ftell(picture);</p>
<p>char Sbuf[siz]; // Error: Variable-length array<br>fseek(picture, 0, SEEK_SET);<br>while (!feof(picture)) {</p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">n = fread(Sbuf, sizeof(char), siz, picture);
// do stuff with the buffer
// memset(Sbuf, 0, sizeof(Sbuf));
Salin selepas log masuk

}

In this code, the goal is to read the contents of a file into a buffer, but the size of the buffer is unknown until the file is opened and its size is determined. However, C language does not allow declaring arrays with variable lengths.

Alternatives to Variable-Length Arrays

To address this issue, there are several alternatives:

  • std::vector: Use the C++ Standard Template Library (STL) to create a dynamic array.
    <br>std::vector<char> Sbuf;</li></ul>
    <p>Sbuf.push_back(someChar);<br>

    • Dynamic Allocation: Use the new operator to allocate memory at runtime for an array.
      <br>char* Sbuf = new char[siz];</li></ul>
      <p>delete [] Sbuf; // Deallocate memory when done<br>

      Considerations

      While dynamic allocation provides a way to create a variable-sized array, it comes with some caveats:

      • The memory allocation must be properly managed to avoid memory leaks.
      • Arithmetic operations cannot be performed directly on the array's index, as it is a pointer.
      • Different compilers may implement variable-length arrays differently, leading to compatibility issues.

      Conclusion

      Although variable-length arrays are not supported in C, there are several alternative approaches available to create dynamic arrays that can adapt to the size of the data to be stored. It is essential to choose the most appropriate option based on the specific requirements and constraints of the project.

      Atas ialah kandungan terperinci Bagaimanakah Saya Boleh Mencipta Tatasusunan Bersaiz Dinamik dalam C untuk Mengendalikan Fail Saiz Tidak Diketahui?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan