简体   繁体   English

使用malloc创建一个int *,然后在满时使用realloc

[英]Creating a int * with malloc and then use realloc when full

In a function I created am I trying to allocate a int array dynamically to store some index values. 在创建的函数中,我试图动态分配一个int数组来存储一些索引值。 First I create the int * with the malloc function and then let the loop store som values in it and increament the pointer each time. 首先,我使用malloc函数创建int *,然后让循环在其中存储som值,并每次使指针失效。 The problem I run in to starts when I try to use the realloc to increase the memory allocation. 当我尝试使用realloc增加内存分配时,我遇到的问题就开始了。 When I do this VS tells me it runs in to undefined behaviour and breaks the program. 当我这样做时,VS告诉我它会以未定义的行为运行并破坏程序。

The code looks like this 代码看起来像这样

void showAvailable(CabinHolder *holder, Booking *booking)
{
    system("cls");

    printf("Choose cabin number \n");
    printf("Start week: &d \t End week: %d", booking->rentPeriod[0], booking->rentPeriod[1]);
    printf("------------------------------------------\n");

    int memory = 5;
    int *indexOfCabin = (int *)malloc(sizeof(int)*memory);
    int counter = 1;

    for (int i = 0; i < CABINS; i++)
    {
        if (counter == memory)
        {
            memory *= 2;
            int *expanded = realloc(indexOfCabin, (memory * sizeof(int)));
            indexOfCabin = expanded;
            expanded = NULL;
        }

        if (booking->cabin->typeOfCabin == holder->arrofCabin[i].typeOfCabin)
        {
            printf("%d. \t Cabin with number %d \t cost: %d per week\n", counter, holder->arrofCabin[i].nr, holder->arrofCabin[i].cost);
            counter++;
            indexOfCabin = &i;
            indexOfCabin++;
        }
    }

    free(indexOfCabin);
    system("pause");
}

When I debugg in VS i also se that my pointer indexOfCabin seems to be undefined inside the if statement, which I don't understand. 当我在VS中调试时,我还发现我的指针indexOfCabin似乎在if语句中未定义,我不理解。 What have I missed here? 我在这里错过了什么?

Okay after some help in the comment section I solved the problem with this edited code segment. 好的,在注释部分获得一些帮助之后,我解决了此已编辑代码段的问题。

void showAvailable(CabinHolder *holder, Booking *booking)
{
    system("cls");

    printf("Choose cabin number \n");
    printf("Start week: %d \t End week: %d\n", booking->rentPeriod[0], booking->rentPeriod[1]);
    printf("------------------------------------------\n");

    int memory = 5;
    int *indexOfCabin = malloc(sizeof(int)*memory);
    int counter = 1;
    int items = 0;
    int choice = 0;

    for (int i = 0; i < CABINS; i++)
    {
        if (counter-1 == memory)
        {
            memory *= 2;
            indexOfCabin = realloc(indexOfCabin, (memory * sizeof(int)));
        }

        if (booking->cabin->typeOfCabin == holder->arrofCabin[i].typeOfCabin)
        {
            printf("%d. \t Cabin with number %d \t cost: %d per week\n", counter, holder->arrofCabin[i].nr, holder->arrofCabin[i].cost);
            counter++;
            indexOfCabin[items++] = i;
        }
    }
    free(indexOfCabin);
    system("pause");
}

First: Problem was indexOfCabin = &i throws away the memory you allocated, and puts the address of i into the pointer instead of what I wanted to do. 首先:问题是indexOfCabin =&i会丢弃分配的内存,并将i的地址放入指针而不是我想做的事情。 Now we store the index in i in the pointer. 现在我们将索引存储在指针的i中。

Second: indexOfCabin can be used like an array, eg indexOfCabin[counter] = i;. 第二:indexOfCabin可以像数组一样使用,例如indexOfCabin [counter] = i;。 But counter needs to start at 0, and should be incremented after being used. 但是计数器需要从0开始,并且在使用后应该增加。 And indexOfCabin should not be incremented 并且indexOfCabin不应增加

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM