Difference between data types (int8 and float)

Because the sentence

does not make any sense.

please understand me, I mean, because of the limited amount of memory and it is very little, so it cannot deal with large data ... Based on this idea, when I deal with an array containing 2000 elements of type int8 , this means when defining the float array it is supposed to take the least number of elements less than 2000 because the float takes up more memory than the int8 .

If you are short of 8k RAM on an ESP32, you probably wasted tons of memory to other things.

This is the result of a compilation for an e-paper weather display

Sketch uses 1018429 bytes (77%) of program storage space. Maximum is 1310720 bytes.
Global variables use 43180 bytes (13%) of dynamic memory, leaving 284500 bytes for local variables. Maximum is 327680 bytes.

I don't regard 327680 as very little.

You are talking nonsense. int8_t is 1 byte, float is 4 bytes. You are allocating 2000 bytes of memory with int8_t but 8000 bytes with float. Why do you think float will take less space? You tell the program to allocate 2000 elements not more not less and this is what it will try to do.

less in number of elements and not space !

how? the number is 2000, you tell malloc to make 2000 elements


float *arr=(float*)malloc(size*sizeof(float));

2000*4^^^

you need to either change the size or the type for example


float *arr=(float*)malloc(size*sizeof(int8_t));

will return pointer to 500 elements of type float. still no idea what you are doing but Iā€™m already annoyed enough to not care

I am working on a very large code and everything is working correctly, I am now increasing the size of the array until it reached to the size that cannot be implemented on esp because of the large size of array .. It is clear until now?!
Now I'm wondering and I was expecting that when increase the size of an array with float , it will take fewer elements than to be acceptable because float takes 4 bytes and this is a bigger size compared to int. "for example" if the array element in int8 =[2000], in float should = [1000]

Great.
Please mark the topic "solved", and we can all move on.

1 Like

Do yourself a favour and learn how memory allocation works, you seem to lack essential basic knowledge. Kthxbye

1 Like

I think I understand what's going on. OP assumed that an array will somehow be constrained to a certain memory footprint. If the datatype is bigger, then it will take fewer elements to fill up that fixed space in memory. This isn't how things work however.

Not always: it depends upon the platform. I'm not 100% sure, but on an esp32, an int may well be 4 bytes.

This conclusion is not even coherent with your wrong assumptions. The ratio, if anything, should be 2000:500

It is.

Fun fact: double is computed in software on a ESP32, float has hardware support.