【问题标题】:How do I initialize a double pointer in C? (array of pointers)如何在 C 中初始化双指针? (指针数组)
【发布时间】:2019-11-13 04:15:04
【问题描述】:

下面是我的代码。我收到一个错误,因为我在分配内存后没有初始化总线 - > BusStops,一个指针数组。要添加到总线的每个单独的 BusStop -> BusStops 数组将在 AddBusStop 方法中为其分配内存。

但是,最初,bus->BusStops 在其中。如何初始化 bus -> BusStops 数组并考虑到这一点,以免收到此错误?

我收到一个未初始化的值是由 Valgrind 上的堆分配错误和分段错误创建的

编辑 - 我尝试在我的 InitializeBus 方法中使用 calloc 而不是 malloc,但可惜,没有运气

//ONE METHOD I WROTE

void InitializeBus(Bus *bus, int maxBusStops) {
    bus -> numBusStops = 0; //initializing numBusStops element from struct to 0
    bus -> maxBusStops = maxBusStops; //initializing maxBusStops element from struct to parameter maxBusStops
    bus -> BusStops = (BusStop**)malloc(maxBusStops * sizeof(BusStop*)); //add memory to bus -> BusStops
}


//THE OTHER METHOD I WROTE

int AddBusStop(Bus *bus, const char* name) { //NOTE - the bus variable passed in is already initialized from the previous method. "name" is the name of the bus stop.
       for(int i = 0; i < bus -> numBusStops; i++) { //for each bus stop that is currently in the array
        if(strcmp(bus -> busStops[i] -> name, name) == 0) { //if that bus stop has the same name as the bus stop passed in as a parameter
           return 1; //return 1
        }

//ERROR ALERT - READ BELOW
// ***I am getting a segmentation fault error at this line. An uninitialized value was created using heap allocation** in the final line of the InitializeBus method. How do I initialize bus -> BusStops once I have added memory to it? Each individual BusStop that is being added has memory allocated to it in this method, but the bus -> busStops array is uninitialized and I am not sure what to initialize it to


    }   //OTHERWISE
    BusStop *newBus = (BusStop*)malloc(sizeof(BusStop) + 1); //create a new BusStop pointer to be added to the array of BusStop pointers, bu. I am not sure if I am allocating enough memory
    newBus -> name = (char*)name; //set the name of this newBus bus stop equal to the name passed in as 0
    newBus -> numDestinations = 0; //initialize numDestinations to 0
    newBus -> destinations = NULL; //set newBus -> destinations to NULL - I was specified to do this and can't change it
    if(bus -> numBusStops == bus -> maxBusStops) { //if there is no more space in bu to add the newly created bus
        bu = (BusStop**)realloc(bu, bus -> maxBusStops * 2 * sizeof(BusStop)); //allocate double the memory to bu
        bus -> maxBusStops *= 2; //make sure maxBusStops is now doubled. I am not sure if I am allocating enough memory
        }
        bus->numBusStops++; //increase the amount of bus stops the bus can hold by 1
        bu[bus -> numBusStops] = newBus; //add the newly created bus at the end of the array bu. **THIS LINE IS WHERE I AM GETTING THE "Invalid Write of Size 8" error
        bus->busStops = bu; //update the struct element bus -> busStops and set it equal to the updated bu value
    return 0; //return 0
}

//MAIN METHOD TO TEST CODE

int main() {
    Bus bus; //create a new bus
    InitializeBus(&bus, 10); //pass in its address and a maxBusStop value of 10
    const char *london = "London"; //create a new char pointer called london
    AddBusStop(&bus, london); //add the London Bus Stop to bus -> busStop
    const char *paris = "Paris"; //create a new char pointer called Paris
    AddBusStop(&bus, paris); //add the Paris Bus Stop to bus -> busStop
}


//STRUCTS

typedef struct Bus {

BusStop** BusStops; /*The array of all Bus stops managed by the bus -- this is an array of pointers, each with type BusStop*/

int numBusStops; /*The current number of bus stops managed by the company*/

int maxBusStops; /*The max number of bus stops the BusStop array can store*/

} Bus;

typedef struct BusStop{

char \* name; /\*The bus stop name\*/

int numDestinations; /\*The number of destinations the bus stop offers rides to\*/

struct BusStop \*\* destinations; /\*The array of destinations the bus stop offers rides to\*/

} BusStop;

【问题讨论】:

  • @AlejandroBlasco 我想通了。我的问题出在 AddBusStop。在增加 numBusStops 之后,我将 BusStop 添加到数组中。由于数组从索引 0 开始,这是错误的。
  • 我的问题现在解决了
  • 你应该问的问题不是如何,而是为什么你在这里使用指针到指针。见Correctly allocating multi-dimensional arrays

标签: c arrays pointers struct double-pointer


【解决方案1】:

首先,你初始化了 bus 吗?如果不对其进行初始化,您将无法获取其字段。 第二:检查 BusStops 是否属于 BusStop** 类型。 第三:因为 BusStop 是一个指针数组,如果你想在其中放入字符串 - 你需要使用 for 循环分配每一列 allocate BusStops[0], BusStops[1]...) 然后你可以放入字符串。 如果不分配列 - 当您尝试比较字符串时,您的程序将无法运行 - 程序将转到 BusStops[i] 但不会分配字符串/内存。

【讨论】:

  • 1.是的,总线是在 InitializeBus 方法中初始化的。总线的引用/指针从 main 方法传入。 2. 是的,BusStops 的类型是“BusStop**” 3. 根据我的分配说明,BusStop 指针数组的每个单独的 BusStop 指针都将在 AddBusStop 方法中分配给它的内存。如果我像你说的那样使用 for 循环,我是否仍然能够为 AddBusStop 方法中的每个单独的 BusStop 指针分配内存?
【解决方案2】:

我想通了!

我的问题出在 AddBusStop。我在增加 numBusStops 之后将 BusStop 添加到数组中。由于数组从索引 0 开始,这是错误的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 2010-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多