如何归零后的realloc新内存

编程入门 行业动态 更新时间:2024-10-18 08:23:35
本文介绍了如何归零后的realloc新内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

什么是调用realloc的同时保持初始分配的内存完好无损后零出新的记忆的最佳方式?

的#include<&stdlib.h中GT;#包括LT&;&ASSERT.H GT;#包括LT&;&string.h中GT;#包括LT&;&stdio.h中GT;为size_t COLORCOUNT = 4;typedef结构rgb_t {    INT R;    INT克;    INT B:} rgb_t;rgb_t **颜色;无效addColor(为size_t我,INT R,诠释克,INT B){    rgb_t *色;    如果(ⅰ&GT = COLORCOUNT){        //新的内存不会是NULL        颜色= realloc的(颜色,sizeof的(rgb_t *)* I);       //凌乱的东西像这样...        // memset的(颜色[COLORCOUNT-1],0,sizeof的(rgb_t *)*(I - COLORCOUNT - 1));         // ...或只是做到这一点(编辑)        为(J = COLORCOUNT; J< I; J ++){            色彩[J] = NULL;        }        COLORCOUNT = I;    }    颜色=的malloc(sizeof的(rgb_t));    color-> R = R;    color-> G =克;    color-> B =;    颜色由[i] =颜色;}无效freeColors(){    为size_t我;    对于(i = 0; I< COLORCOUNT;我++){        的printf(%X \\ n,颜色[I]);        //不能做到这一点,如果内存是不是NULL       //如果(颜色[I])         //自由(颜色[I]);    }}诠释主(){    颜色=的malloc(sizeof的(rgb_t *)* COLORCOUNT);    memset的(颜色,0,sizeof的(rgb_t *)* COLORCOUNT);    addColor(0,255,0,0);    addColor(3,255,255,0);    addColor(7,0,255,0);    freeColors();    的getchar();}

解决方案

有可能是没有必要做 memset的:您可能没有使用颜色[K] 与后来的一些有效的设置它。例如,您的code组颜色[I] 来一个新分配的颜色指针,所以你没'吨需要设置颜色[I] 到 NULL 。

不过,即使你想零出来,所以一切是好的,还是真的需要新的指针是 NULL :C标准不保证所有位归零是空指针常数(即 NULL ),所以 memset的()不是反正合适的解决方案。

你唯一可以做的便携式就是设置每个指针 NULL 在一个循环:

为size_t K表;对于(K = COLORCOUNT; K<我+ 1; + K)/ *请参阅下面我为什么+ 1 * /    颜色[K] = NULL;

您首要的问题是,你的的realloc()电话是错误的。 的realloc()返回一个指针大小的内存,它不(一定)就地调整它的大小。

所以,你应该做的:

/ * I + 1,因为你以后分配给颜色[I] * /rgb_t ** TMP = realloc的(颜色,第(i + 1)* sizeof的* TMP);如果(TMP!= NULL){    / * realloc的成功,不能用的颜色了* /    颜色= tmp目录;}其他{    / * realloc的失败,颜色仍然是有效的* /}

如果你真的想知道 memset的()通话应该是什么,你需要设置为零的存储器中,起始颜色+ COLORCOUNT ,并设定第i + 1 COLORCOUNT 成员为零:

memset的(色彩+ COLORCOUNT,0,(第i + 1 COLORCOUNT)* * sizeof的颜色);

但正如我上面所说的,所有字节零不能保证是一个 NULL 指针,所以你的 memset的()也没用呢。你如果想 NULL 指针使用循环。

What is the best way to zero out new memory after calling realloc while keeping the initially allocated memory intact?

#include <stdlib.h> #include <assert.h> #include <string.h> #include <stdio.h> size_t COLORCOUNT = 4; typedef struct rgb_t { int r; int g; int b; } rgb_t; rgb_t** colors; void addColor(size_t i, int r, int g, int b) { rgb_t* color; if (i >= COLORCOUNT) { // new memory wont be NULL colors = realloc(colors, sizeof(rgb_t*) * i); //something messy like this... //memset(colors[COLORCOUNT-1],0 ,sizeof(rgb_t*) * (i - COLORCOUNT - 1)); // ...or just do this (EDIT) for (j=COLORCOUNT; j<i; j++) { colors[j] = NULL; } COLORCOUNT = i; } color = malloc(sizeof(rgb_t)); color->r = r; color->g = g; color->b = b; colors[i] = color; } void freeColors() { size_t i; for (i=0; i<COLORCOUNT; i++) { printf("%x\n", colors[i]); // can't do this if memory isn't NULL // if (colors[i]) // free(colors[i]); } } int main() { colors = malloc(sizeof(rgb_t*) * COLORCOUNT); memset(colors,0,sizeof(rgb_t*) * COLORCOUNT); addColor(0, 255, 0, 0); addColor(3, 255, 255, 0); addColor(7, 0, 255, 0); freeColors(); getchar(); }

解决方案

There is probably no need to do the memset: you may not be using colors[k] before setting it with something valid later. For example, your code sets colors[i] to a newly allocated color pointer, so you didn't need to set colors[i] to NULL.

But, even if you wanted to "zero it out so everything is nice", or really need the new pointers to be NULL: the C standard doesn't guarantee that all-bits-zero is the null pointer constant (i.e., NULL), so memset() isn't the right solution anyway.

The only portable thing you can do is to set each pointer to NULL in a loop:

size_t k; for (k=COLORCOUNT; k < i+1; ++k) /* see below for why i+1 */ colors[k] = NULL;

Your primary problem is that your realloc() call is wrong. realloc() returns a pointer to the resized memory, it doesn't (necessarily) resize it in-place.

So, you should do:

/* i+1 because you later assign to colors[i] */ rgb_t **tmp = realloc(colors, (i+1) * sizeof *tmp); if (tmp != NULL) { /* realloc succeeded, can't use colors anymore */ colors = tmp; } else { /* realloc failed, colors is still valid */ }

If you really want to know what the memset() call should be, you need to set to zero the memory starting at colors+COLORCOUNT, and set i+1-COLORCOUNT members to zero:

memset(colors+COLORCOUNT, 0, (i+1-COLORCOUNT) * sizeof *colors);

But as I said above, all bytes zero is not guaranteed to be a NULL pointer, so your memset() is useless anyway. You have to use a loop if you want NULL pointers.

更多推荐

如何归零后的realloc新内存

本文发布于:2023-11-13 02:13:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583133.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内存   归零后   realloc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!