简体   繁体   English

将 float[] 作为 float* 参数传递的问题

[英]Issue passing a float[] as float* parameter

I will try to be as explicit as I can.我会尽量表达清楚。

I have an exercise to solve, I've searched a lot on the web but could not find what my issue is.我有一个练习要解决,我在 web 上搜索了很多,但找不到我的问题。

Because this is an 'as is' Exercise I cannot modify float* to float[], I must use the values given and the function signature given.因为这是一个“原样”练习,所以我不能将 float* 修改为 float[],我必须使用给定的值和给定的 function 签名。 All I can work with is the contains of setWeight function.我只能使用 setWeight function 的内容。

Consider the following:考虑以下:

class Train
{
    protected:
    float* weight;

    public:
    setWeight(float* weight)
    {
        this->weight = weight;
    }
}

Then in main I call the following:然后我主要调用以下内容:

main()
{
    float weights[] = { 30.5f, 20.0f, 12.7f, 15.88f };
    train.setWeight(weights);
}

When I look in debug mode only the first value gets passed.当我在调试模式下查看时,只有第一个值被传递。 Can someone help me out?有人可以帮我吗?

The program hasn't done anything wrong, but you are looking at it wrong.该程序没有做错任何事情,但是您看错了。

In the debugger instead of reading train.weight and letting the debugger follow the pointer, try reading train.weight[0] , train.weight[1] , and train.weight[3]在调试器中,不要读取train.weight并让调试器跟随指针,而是尝试读取train.weight[0]train.weight[1]train.weight[3]

If you cannot modify the signature of setWeight , you can provide the data as a dynamically allocated array.如果您无法修改setWeight的签名,您可以将数据作为动态分配的数组提供。

main()
{
    Train train;
    float* weights = new float[4]{ 30.5f, 20.0f, 12.7f, 15.88f };
    train.setWeight(weights);

    //...
    delete[] weights;
}

Be aware you must at some point delete[] weights , and the moment you do, train will no longer contain valid values.请注意,您必须在某些时候delete[] weights ,并且在您这样做的那一刻, train将不再包含有效值。

In the future, if you write a class similar to this, use RAII : Allocate with new in the constructor, and deallocate with delete in the destructor.以后如果你写一个类似这样的class,使用RAII :在构造函数中用new分配,在析构函数中用delete解除分配。

BACKGROUND : A pointer is a "special" type that stores a memory address.背景:指针是一种“特殊”类型,用于存储 memory 地址。 In this case, is a float value.在这种情况下,是一个浮点值。

The thing is that an array is just syntactical sugar for handling pointers, since the name of an array is a pointer to the first element.问题是数组只是处理指针的语法糖,因为数组的名称是指向第一个元素的指针。 If it's more general like a matrix, the compiler will run some fancy calculations to get it done.如果它像矩阵一样更通用,编译器将运行一些花哨的计算来完成它。 If you use a debugger, you will probably end up with that kind of situation since there is no way for the debugger to know that that pointer corresponds to a single value or the start of an array, a matrix, etc...如果您使用调试器,您可能最终会遇到这种情况,因为调试器无法知道该指针对应于单个值或数组、矩阵等的开头...

For example, this notation:例如,这个符号:

arr[1]

is translated as:被翻译为:

*(arr + 1)

PD: A cool trick you can retrieve from this, is that since the sum is commutative, you can transform the last expression in this: PD:你可以从中得到一个很酷的技巧,因为总和是可交换的,你可以转换最后一个表达式:

*(arr + 1)
*(1 + arr)
1[arr]

And still works.而且仍然有效。

It's because you cannot this way, and it's really dangerous.因为你不能这样,而且真的很危险。

Hard array have a fixe space in memory, and is meant to be free when the variable go out of the scope.your pointer (float *) is just an adress to a memory space, you make it point to your hard memory space that will be free when you get out of the function, you will crash when you will try to use your variable. Hard array have a fixe space in memory, and is meant to be free when the variable go out of the scope.your pointer (float *) is just an adress to a memory space, you make it point to your hard memory space that will当你离开 function 时是免费的,当你尝试使用你的变量时你会崩溃。

    void setWeight(float* weight)
{
  float* Weight;
  Weight = weight;

  for (int i = 0; i < 4; ++i)
  {
    std::cout << *Weight << std::endl;
    ++Weight;
  }
}

int main()
{
  float weights[] = { 30.5f, 20.0f, 12.7f, 15.88f };
  setWeight(weights);
}

As you can see, i can see each hard float because i already know the size of the hard float array.如您所见,我可以看到每个硬浮点数,因为我已经知道硬浮点数组的大小。 When i do ++Weight i'm incrementing the pointer adress to go to the next value.当我做 ++Weight 时,我将指向 go 的指针地址增加到下一个值。 (++Weight, is the same as Weight = Weight + 1) (++Weight,等于Weight = Weight + 1)

BUT, it's not a copy, you have just a pointer to a hard space that will be free, and i need to know the size of the float[] passed in parameter但是,它不是一个副本,你只有一个指向一个空闲的硬空间的指针,我需要知道传入参数的 float[] 的大小

if we were in C i would tell you to malloc some space and copy one to the other, but, in c++ if you cna change the type of variable in your class, i will suggest a vector of float: std::vector<float> weight; if we were in C i would tell you to malloc some space and copy one to the other, but, in c++ if you cna change the type of variable in your class, i will suggest a vector of float: std::vector<float> weight;

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

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