简体   繁体   English

如何处理 float *array

[英]how can deal with float *array

I have array declared in something like this我在这样的东西中声明了数组

first way第一种方式

int size=256*10;
float *array=(float*)malloc(size * sizeof(float));
for(int i=0;i<2398;i++)
     data[i]=data[i]*900;

When I used this why in declaration my code run with out errors but当我使用这个为什么在声明中我的代码运行时没有错误但是

when I use the array as constant values like this :当我将数组用作这样的常量值时:

2nd way第二种方式

float array[]={120.0, 160.0, 255.0, 216.0, 255.0, 224.0, 0.0, 16.0, 
74.0, 70.0, 73.0, 70.0, 0.0, 1.0, 1.0};

I didn't get what was required from the code even if I took the array output in the first way and defined it as in 2nd way?!!即使我以第一种方式获取数组输出并将其定义为第二种方式,我也没有从代码中得到什么?! I thought defining an array in the 2nd way is correct and equivalent to the first definition?!我认为以第二种方式定义数组是正确的并且等同于第一种定义?! What is the correct way to declare an array that equivalent to the first way?声明与第一种方式等效的数组的正确方法是什么?

Both ways create valid arrays, and you can use them pretty much exchangeably if you pay attention to object life time.这两种方式都可以创建有效的数组,如果您注意对象的生命周期,您几乎可以互换使用它们。 The differences are:区别在于:

  • malloc dynamically allocates memory which exists until the program explicitly calls free() on the address returned by malloc. malloc动态分配内存,直到程序在 malloc 返回的地址上显式调用free()为止。 One of the typical use cases is to allocate and initialize some storage in a function and let the function return the address.典型的用例之一是在函数中分配和初始化一些存储,并让函数返回地址。 That's fine: The life time of objects in dynamically allocated memory (new, malloc) is independent of scope (eg, a function).这很好:动态分配的内存(new、malloc)中对象的生命周期与作用域(例如,函数)无关。

    By contrast, an array defined the second way exists until the variable goes out of scope.相比之下,以第二种方式定义的数组会一直存在,直到变量超出范围。 If it is a global variable it will exist until the program ends.如果它是一个全局变量,它将一直存在到程序结束。 If it is defined in a function it will only exist until the program returns from the function, or ends.如果它是在函数中定义的,那么它只会存在直到程序从函数返回或结束。 That may be your problem here.那可能是你这里的问题。 Returning the address of a function-local object and using it outside that function is a not uncommon error.返回函数本地对象的地址并在该函数之外使用它是一个并不少见的错误。 Newer compilers with sufficiently high warning levels should warn when they see such a mistake.具有足够高警告级别的较新编译器在看到此类错误时应发出警告。

  • The second array is an object with the number of elements indicated by the initializers, exactly.第二个数组是一个对象,其元素数量由初始值设定项指示。 You can call sizeof(array) and get a large number, for example 60. The first "array", by contrast, is not an array at all: It is a pointer to the first element of an array.您可以调用sizeof(array)并获得一个大数,例如 60。相比之下,第一个“数组”根本不是数组:它是指向数组第一个元素的指针。 Calling sizeof() on that pointer returns the size of that pointer, typically 4 or 8, no matter how much memory is allocated at the location the pointer points to.对该指针调用 sizeof() 将返回该指针的大小,通常为 4 或 8,无论在该指针指向的位置分配了多少内存。 There is no built-in way to keep track of the size of the allocated memory — the programmer must store it somewhere.没有内置的方法来跟踪分配的内存的大小——程序员必须将它存储在某个地方。 Typically, larger programs have a constant or a define somewhere in a header shared by "translation units" (source files) so that parts of the program that use the allocated memory don't overshoot its limits, and the part allocating it knows how much is needed.通常,较大的程序在“翻译单元”(源文件)共享的标头中的某处具有常量或定义,以便使用分配的内存的程序部分不会超出其限制,并且分配它的部分知道多少需要。

Remarks:评论:

  1. You don't use the entire allocated memory which is a bit strange: You allocate 2560 floats but initialize only 2398.您没有使用整个分配的内存,这有点奇怪:您分配了 2560 个浮点数,但仅初始化了 2398 个。
  2. The assignment data[i]=data[i]*900;赋值data[i]=data[i]*900; reads from the uninitialized element data[i] before assigning to it.在分配给它之前从未初始化的元素data[i]读取。 Reading uninitialized memory is verboten and kaput ("undefined behavior"), and the program is faulty.读取未初始化的内存是verbotenkaput (“未定义行为”),程序有问题。 In practice you'll probably simply have funny float values, but still.在实践中,您可能只会拥有有趣的浮点值,但仍然如此。 Don't do it.不要这样做。

If you have an array you want to initialize with a few dozen values known at compile time I'd strongly prefer a true array.如果你有一个数组,你想用编译时已知的几十个值进行初始化,我非常喜欢一个真正的数组。 If it needs to live longer than the function you can make it static and let the function return a pointer to it.如果它需要比函数存活更长时间,您可以将其设为静态并让函数返回指向它的指针。

Only if you need a lot of memory (2500 floats qualifies) or must create a number of arrays that is unknown at compile time consider creating them dynamically.仅当您需要大量内存(2500 个浮点数符合条件)或必须创建许多在编译时未知的数组时,才考虑动态创建它们。

As others have remarked in comments, real-world code would use a vector or a smart pointer and/or std::array .正如其他人在评论中所说的那样,现实世界的代码将使用向量或智能指针和/或std::array Explicit dynamic allocation has pretty much become a domain of beginner homework (I'm not condescending: It has its place there) or projects stuck with old compilers (don't ask).显式动态分配几乎已经成为初学者作业的领域(我不是居高临下:它在那里有它的位置)或使用旧编译器的项目(不要问)。

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

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