简体   繁体   English

为什么我在写入“ptr”时收到“C6386”缓冲区溢出警告?

[英]Why am I getting warning 'C6386' Buffer overrun while writing to 'ptr'?

`Variable 'size' is 10 in this example.在此示例中,变量“大小”为 10。 If I were to hardcode 10 in place of 'int(size)' the overrun warning goes away.如果我用硬编码 10 代替“int(size)”,超限警告就会消失。 Any suggestions/reasoning why this is occurring?为什么会发生这种情况的任何建议/推理? I want to allocate 80 bytes for my pointer, each of the allocated values being a timestep from the given timespan.我想为我的指针分配 80 个字节,每个分配的值都是给定时间跨度的一个时间步长。

Thank you!谢谢!

int main() {
    const double h = 0.1;
    const double tspan[2] = { 0.0, 1.0 };
    const double size =round(tspan[1] / h);
    double *ptr = (double*)malloc(int(size) * sizeof(double));

    if (!ptr) {
        cout << "Memory Allocation Failed";
        exit(1);
    }

    double j = 0;
    for (int i = 0; i < size; i++) {
        ptr[i] = j;
            
        //cout << j << '\n';
        j++;
    }

    cout << '\n';
    for (int i = 0; i < size; i++) {
        cout << *(ptr + i) << endl;
        //cout << i << '\n';
    }
    
    
    free(ptr);
    return 0;
}

I have tried dereferencing the pointer and making sure it isn't NULL.我试过取消引用指针并确保它不是 NULL。 I have also printed out the result.我也打印了结果。 The result being a pointer that counts 0-9.结果是一个计数 0-9 的指针。 ` `

double size can be 10.1 , the condition i < 10.1 does not terminate the loop if i is 10, the allocated buffer size is int(10.1) , that is 10, ptr[10] causes the buffer overrun. double size可以是10.1 ,条件i < 10.1不终止循环如果i是 10,分配的缓冲区大小是int(10.1) ,即 10, ptr[10]导致缓冲区溢出。

As an addedum to what 273K has answered, you csn solve this problem by having size as a std::size_t variable.作为 273K 回答的补充,您 csn 通过将size作为std::size_t变量来解决这个问题。

Let's floor the result of that division and cast to int .让我们对该除法的结果进行 floor 并转换为int

int main() {
    const double h = 0.1;
    const double tspan[2] = { 0.0, 1.0 };
    const int size = static_cast<int>(std::floor(tspan[1] / h));
    double *ptr = (double*)malloc(size * sizeof(double));

    if (!ptr) {
        cout << "Memory Allocation Failed";
        exit(1);
    }

    double j = 0;
    for (int i = 0; i < size; i++) {
        ptr[i] = j;
            
        //cout << j << '\n';
        j++;
    }

    cout << '\n';
    for (int i = 0; i < size; i++) {
        cout << *(ptr + i) << endl;
        //cout << i << '\n';
    }
    
    free(ptr);
    return 0;
}

Somewhat better, let's use new and delete instead of malloc and free.更好的是,让我们使用 new 和 delete 而不是 malloc 和 free。

int main() {
    const double h = 0.1;
    const double tspan[2] = { 0.0, 1.0 };
    const int size = static_cast<int>(std::floor(tspan[1] / h));
    double *ptr = new double[size];

    if (!ptr) {
        std::cout << "Memory Allocation Failed";
        exit(1);
    }

    double j = 0;
    for (int i = 0; i < size; i++) {
        ptr[i] = j;
            
        //std::cout << j << '\n';
        j++;
    }

    std::cout << '\n';
    for (int i = 0; i < size; i++) {
        std::cout << *(ptr + i) << std::endl;
        //cout << i << '\n';
    }
    
    delete[] ptr;
    return 0;
}

Better still, use std::vector .更好的是,使用std::vector

int main() {
    const double h = 0.1;
    const double tspan[2] = { 0.0, 1.0 };
    const int size = static_cast<int>(std::floor(tspan[1] / h));
    std::vector<double> vec(size);

    double j = 0;
    for (auto &x : vec) {
        x = j;
            
        //std::cout << j << '\n';
        j++;
    }

    std::cout << '\n';
    for (auto x : vec) {
        std::cout << x << std::endl;
        //std::cout << i << '\n';
    }

    return 0;
}

round() rounds to the nearest integer number. round()四舍五入到最接近的整数。 If anything, you should use ceil() to round up .如果有的话,你应该使用ceil()四舍五入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 避免“缓冲区溢出” C6386警告 - Avoiding 'Buffer Overrun' C6386 warning VS2015:[C6386] 写入时缓冲区溢出(即使对于相同的索引值) - VS2015: [C6386] Buffer Overrun while writing (even for same index value) VS2019:[C6386] 缓冲区溢出而到 - VS2019: [C6386] Buffer Overrun while to 错误:C6386:写入“newArr”时缓冲区溢出:可写大小为“int current_size*1”字节,但可能写入“2”字节 - Error: C6386: Buffer overrun while writing to 'newArr': the writable size is 'int current_size*1' bytes, but '2' bytes might be written __m256 阵列上的 Visual Studio 2019 C6385 / C6386(缓冲区溢出警告) - Visual Studio 2019 C6385 / C6386 (buffer overrun warning) on __m256 array 使用动态分配的 arrays 导致来自代码分析的 C6386 缓冲区溢出警告 - Using dynamically-allocated arrays causes C6386 Buffer Overrun warning from Code Analysis 为什么使用strsafe.h StringCch函数会导致C6386缓冲区溢出错误? - Why error C6386 buffer overrun with strsafe.h StringCch functions? 为什么在Visual Studio 2012的代码分析中,此代码为什么发出缓冲区溢出警告(C6385 / C6386)? - Why does this code emit buffer overrun warnings(C6385/C6386) in code analysis on Visual Studio 2012? Visual Studio 2015 代码分析 C6386 警告缓冲区溢出 - Visual Studio 2015 Code Analysis C6386 warns of buffer overrun 如何解决 C6386 警告? - How to solve C6386 warning?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM