标签: realloc

关于在C++代码中使用realloc实现的问题

在我们的C++中,Iam目前使用realloc方法来调整malloc分配的内存大小.realloc()的使用如下所示

my_Struct *strPtr =(my_struct*)malloc(sizeof(my_Struct));

/* an later */

strPtr = (my_struct*)realloc(strPtr,sizeof(my_Struct)*NBR);
Run Code Online (Sandbox Code Playgroud)

现在wikipeadia(_http://en.wikipedia.org/wiki/Malloc)说

如果相反的话

void *p = malloc(orig_size);

/* and later... */

p = realloc(p, big_size);
Run Code Online (Sandbox Code Playgroud)

然后,如果不可能获得big_size字节的内存,p将具有值NULL并且我们不再有指向先前为p分配的内存的指针,从而产生内存泄漏

它还说纠正上述错误的正确方法是

void *p = malloc(orig_size);

/* and later... */

void *tmp = realloc(p, big_size); 

if (tmp != NULL)
 {

p = tmp; /* OK, assign new, larger storage to p */

} 

else 

{

/* handle the problem somehow */

}
Run Code Online (Sandbox Code Playgroud)

你能告诉我使用realloc()的最佳方法是什么

一旦我有一个结构的指针,然后在使用realloc后,我可以使用指针到一个空格???

非常感谢

c++ memory-management realloc

2
推荐指数
2
解决办法
3265
查看次数

realloc只会扩展内存还是会导致内存问题?

我有以下代码:

#include <stdio.h>
#include <stdlib.h>
#define OUT

void getDataFromServer(OUT int** array, OUT int* size)
{
    static int tmpArr[] = {0x00, 0x01, 0x02, 0x03,  0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
                        0x10, 0x11, 0x12, 0x13,  0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
    *size = sizeof tmpArr / sizeof(int);
    printf("Before realloc: %p\n", *array);
    *array = realloc(*array, sizeof(*array) * *size);
    printf("After realloc : %p\n", *array);
    int i=0;
    for (; i < *size; i++) …
Run Code Online (Sandbox Code Playgroud)

c memory-management realloc

2
推荐指数
1
解决办法
1079
查看次数

缩小指针所占用的内存时的realloc行为

困惑1:
man realloc说,如果它做新的分配对象调整到新的大小的物体将被移动.然而,在缩小尺寸时,有许多地方表示可以将内存中的数据移动到新的地方(SO).这让我感到困惑.我们如何发现缩小规模将在新的分配中失败,因为在手册页和gnu页面中没有明确提到减小大小的情况.

困惑2:
当我们做以下时:

void * ptr1 = malloc(SOMEBIGSIZE);
void * ptr2 = realloc(ptr1, SOMESMALLSIZE);
Run Code Online (Sandbox Code Playgroud)

评估ptr1==ptr2结果是真的.这意味着ptr2指向相同的ptr1.那么,哪个2.1或2.2是真的?
(2.1) ptr1没有缩小尺寸,如果SOMEBIGSIZE >>> SOMESMALLSIZE并且我们在内存方面没有任何优势,那可能会很糟糕.
(2.2)如果ptr1收缩,那么地址范围ptr1 + SOMESMALLSIZE到ptr1 + SOMEHUGESIZE的内存会发生什么变化?它被释放或标记为免费吗?

c realloc

2
推荐指数
1
解决办法
286
查看次数

具有realloc的程序在Valgrind中表现不同

我编写了一个函数来读取带有fgets的字符串,该函数使用realloc()使缓冲区在需要时增长:

char * read_string(char * message){
    printf("%s", message);
    size_t buffsize = MIN_BUFFER;
    char *buffer = malloc(buffsize);
    if (buffer == NULL) return NULL;
    char *p;
    for(p = buffer ; (*p = getchar()) != '\n' && *p != EOF ; ++p)
        if (p - buffer == buffsize - 1) {
            buffer = realloc(buffer, buffsize *= 2) ;
            if (buffer == NULL) return NULL;
        }
    *p = 0;
    p = malloc(p - buffer + 1);
    if (p == NULL) return NULL;
    strcpy(p, buffer); …
Run Code Online (Sandbox Code Playgroud)

c valgrind realloc

2
推荐指数
1
解决办法
1685
查看次数

C动态内存分配

我正在学习C但我仍然是一个菜鸟.我正在编写一个程序作为动态内存分配练习,它从未知长度的用户获取文本,并返回此文本,没有空格,制表符,特殊字符或数字.该程序似乎工作正常,但有些文本似乎由于未知原因而被更改为未知字符.这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char *pstring;
    pstring = (char *)malloc( sizeof(char));

    char c = '$';

    int i = 0;
    while(c != '\n')
    {
        c = getchar();
        if(isalpha(c))
        {
            pstring[i] = c;
            realloc(pstring, (i+2)*sizeof(char));
            i++;
        }
    }

    int j;
    for(j = 0; j < i; j++)
    {
        printf("%c", pstring[j]);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

工作正常: 在此输入图像描述

问题是: 在此输入图像描述

c malloc realloc calloc dynamic-memory-allocation

2
推荐指数
1
解决办法
132
查看次数

每个循环C展开1个元素的数组

n=-1我需要结束循环时,我必须告诉用户他需要多少输入以及扩展数组以让其他人输入所需的每个循环.

这是我的代码:

void extend(int *a)
{
    int *pt;
    pt = (int*) realloc(a , sizeof(int));   
}

int main()
{
    int *a;
    a = (int*)malloc(sizeof(int));
    int i=0;
    int n=0;

    while(n!=-1) {
        scanf("%d", &n);
        if(n != -1) {
            a[i] = n;
            extend(a);
            i++;        
        }
    }       
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它使用3个值然后它停止工作,我可以循环我想要但当它n变得-1崩溃.我在某些编译器中出现"无效的下一个大小"错误.

编辑:

此代码有效:

int main()
{
    int *a = (int*)malloc(0);
    int i=0;
    int n;

    do {
        scanf("%d", &n);
        if(n!=-1) {
            i++;
            a = (int*) realloc(a , i*sizeof(int));
            a[i-1] = …
Run Code Online (Sandbox Code Playgroud)

c arrays pointers loops realloc

2
推荐指数
1
解决办法
955
查看次数

我的指针错误在哪里?

我已经完成了以下代码,但我找不到有什么问题.该函数getsxnremem()使用字符串获取字符串,len使用fgets()空终止符覆盖换行符(如果有的话),然后重新调整内存大小以适应字符串.无论如何,这是个主意.

以下代码有时会起作用,有时会崩溃.我过去经常发生这种情况,我经常发现问题,但这次我花了太长时间.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

unsigned getsxnremem(char **str, unsigned len){
    unsigned l, flag = 1;
    free(*str);
    char *buff;
    if ((*str = malloc(len)) == NULL) return 0;
    if(fgets(*str, len, stdin) == NULL) { free(*str); return 0; }
    l = strlen(*str);
    if (l && ((*str)[l-1] == '\n')) { *(str)[l-1] = '\0'; flag = 0; }
    if ((buff = realloc(*str, l + flag)) == NULL){ free(*str); return 0; }
    *str = buff;
    return (l …
Run Code Online (Sandbox Code Playgroud)

c realloc

2
推荐指数
1
解决办法
101
查看次数

正确使用Realloc

这是我被教导使用的方式realloc():

int *a = malloc(10);
a = realloc(a, 100); // Why do we do "a = .... ?"
if(a == NULL) 
//Deal with problem.....
Run Code Online (Sandbox Code Playgroud)

这不是多余的吗?我不能这样做吗?:

if(realloc(a, 100) == NULL) //Deal with the problem
Run Code Online (Sandbox Code Playgroud)

我发现的其他realloc示例也是如此,例如:

int *oldPtr = malloc(10);
int * newPtr = realloc(oldPtr, 100);
if(newPtr == NULL) //deal with problems
else oldPtr = newPtr;
Run Code Online (Sandbox Code Playgroud)

我不能这样做吗?:

int *oldPtr = malloc(10);
if(realloc(oldPtr, 100) == NULL)  //deal with problems
//else not necessary, oldPtr has already been reallocated and has now 100 …
Run Code Online (Sandbox Code Playgroud)

c realloc

2
推荐指数
2
解决办法
1293
查看次数

重新分配函数后的分段错误

我已经创建了这个代码来测试一个错误,我在主代码中得到了它,并且它共享同样的问题.我总是得到分段错误或损坏的数据(零或奇数).

这是代码:

int *p=NULL;
int func (int **point);

int main() {
    int num = 5647;
    p = malloc(sizeof(int)*2);
    p[0] = num;
    p[1]= 657;
    printf("%d\n", p[0]);
    printf("%d\n", p[1]);
    func(&p);
    printf("%d\n", p[0]);
    printf("%d\n", p[1]);
    printf("%d\n", p[2]);
    printf("%d\n", p[3]);
    return 0;
}

int func (int **point){
    *point = realloc(*point,sizeof(int)*4);
    if (*point==NULL){
        printf("\n abort \n");
        exit(0);
    }
    *point[0] = 867;
    *point[1]= 777;
    *point[2] = 67;
    *point[3]= 77;
}  
Run Code Online (Sandbox Code Playgroud)

我正在获得分段错误*point[1]=777;.如果我试图这样做,point[1]=777;我会得到错误的数据.随着任何变化int func (int **point);func(&p);我正在进行分段故障realloc.

请指教,我已阅读有关双指针的信息,并试图按照我找到的所有解决方案,但每次我收到此错误.

c pointers realloc double-pointer

2
推荐指数
1
解决办法
381
查看次数

如果在函数内部调用了free(),它是否仅在本地释放?

仍在学习分配和释放的绳索,我对某些事情感到好奇。

假设您为数组分配内存:

int *array = malloc(5 * sizeof(int));
Run Code Online (Sandbox Code Playgroud)

您将数组发送给一个函数,然后在其中重新分配它。函数参数必须是指向数组(或&array)的双指针,否则重新分配只能在本地进行。

如果您还想释放(释放)另一个函数中的数组,它还必须是双指针吗?释放功能只会在本地发生吗?如果没有,在这方面它与重新分配有何不同?

c free memory-management function realloc

2
推荐指数
1
解决办法
69
查看次数