简体   繁体   English

从函数返回指向全局数组的指针

[英]Returning pointer to global array from function

I am not experienced enough in C/C++ programming, so I am asking for an explanation. 我对C / C ++编程没有足够的经验,所以我要求解释。

I have global array declared as following. 我有以下声明的全局数组。 ASAK it is located in seperate memory part of initialized global memory in context of process memory. ASAK位于过程存储器的上下文中,位于已初始化全局存储器的单独存储器部分中。

Sensor sensorsArray[SENSORS_COUNT] = {dhtTempSensor, dhtHumSensor, dallasTempSensor, waterLevelSensor};

I need to find element in this array and return pointer to it (because I am going to change its value). 我需要在此数组中找到元素并返回指向它的指针(因为我将更改其值)。 I have written such function. 我已经写了这样的功能。

Sensor* getSensorById(uint32_t id) {
  for (int i = 0; i < SENSORS_COUNT; i++) {
    Sensor* current = &sensorsArray[i];
    if (current->sensorId == id) {
      return current;
    }
  }
}

Will it work properly, I am not sure about current pointer, it is allocated on the stack so it is in function scope, will it be poped from the stack after function ends ? 它是否可以正常工作,我不确定current指针,它是在堆栈上分配的,所以它在函数范围内,函数结束后是否会从堆栈中弹出? Or it will work properly. 否则它将正常工作。

I mean not pointer(address of array element which is taken using &sensorsArray[i]), but current pointer variable which contains address of erray element, will it be poped or not. 我的意思不是指针(使用&sensorsArray [i]获取的数组元素的地址),而是当前指针变量(包含erray元素的地址)是否会弹出。

Please suggest best way how to do in such situation. 请提出在这种情况下的最佳方法。 Thx. 谢谢。

You aren't covering all the possible returning cases of the function, namely, the case when the id does not match with any of the id s of the array. 你是不是涵盖所有功能的可能恢复的情况下,即情况下,当id不匹配任何的id阵列的秒。

Currently the pointer will return the last element of the array if there is no match. 当前,如果不匹配,则指针将返回数组的最后一个元素。

You could correct that by defining the pointer Sensor* sensor_found = nullptr outside the for loop such that if there is no sensor found the return value is still valid, ie nullptr and assigning the found value of current to sensor_found , only if there is a match. 您可以通过在for循环外定义指针Sensor* sensor_found = nullptr来更正该错误,以便如果没有找到传感器,则返回值仍然有效,即nullptr并且仅当存在匹配项时才将current的找到的值分配给sensor_found 。 。

Sensor* getSensorById(uint32_t id) {

    Sensor* sensor_found = nullptr;

    for (int i = 0; i < SENSORS_COUNT; i++) {

        Sensor* current = &sensorsArray[i]; 

        if (current->sensorId == id) {
             sensor_found = current;  
             break;    
        }
    }

    return sensor_found;
}

If the id found return current , otherwise, if there is no match return nullptr . 如果找到的id返回current ,否则返回nullptr

you want to make sure that the function has a valid return statement on its every execution path. 您想确保该函数在每个执行路径上都有一个有效的return语句。 In you current implementation if the id is not matched then the return value of Sensor* is not set and will contain random bytes. 在当前的实现中,如果id不匹配,则未设置Sensor *的返回值,它将包含随机字节。 One wau to deal with this situation is to return the nullptr to indicate that the Sensor was not found. 解决这种情况的一种方法是返回nullptr以指示未找到传感器。 Other than that, ythe function will work properly. 除此之外,该功能将正常运行。

Sensor* getSensorById(uint32_t id) {
  for (int i = 0; i < SENSORS_COUNT; i++) {
    Sensor* current = &sensorsArray[i];
    if (current->sensorId == id) {
      return current;
    }
  }
  return nullptr;  // id not matched
}

Your code is fine (as the comments suggest). 您的代码很好(如注释所示)。 The reason why you don't need to worry about the current pointer becoming invalid is because the memory that it points to (ie, the global array) stays valid beyond the scope of the function. 您不必担心current指针变为无效的原因是因为它所指向的内存(即全局数组)在函数范围之外仍然有效。 Just because you happen to create a pointer (and remember, a pointer is really just a number that corresponds to some place in memory) to that memory doesn't mean that it becomes invalid when used elsewhere. 仅仅因为您碰巧创建了一个指向该内存的指针(记住,指针实际上只是一个对应于内存中某个位置的数字),并不意味着该指针在其他地方使用时将变得无效。

When you say Sensor *current = &sensorArray[i]; 当您说Sensor *current = &sensorArray[i]; , then if sensorArray[i] is stored at, say, position 0x10 in memory, then current = 0x10 , and no matter where it is used, then sensorArray[i] will still be at memory location 0x10 . ,然后,如果sensorArray[i]存储在内存中的位置0x10处,那么current = 0x10 ,无论使用在哪里,那么sensorArray[i]仍将位于内存位置0x10 When you assign a value to current , you are not copying the value from the sensor, you are merely getting a pointer to it. 当您为current分配一个值时,您并没有从传感器复制该值,而只是获得了指向它的指针。

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

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