Arrays versus dynamic memory

Hi Community

is generating an Fix Array and fill the whole Array with values i.e. of 32K slower, as generating and fill a dynamic Memory using malloc etc. ???

Is using malloc in higher Blocksizes faster then fix Arrays?

Thank You !!!

The speed is the same, memory is memory ;-) There is only one kind of memory, how you get it is irrelevent. (I’m oversimplifying for the sake of clarity).

Performance may be affected if the memory isn’t aligned. For example if you have array of integer but each interger is on address 1,5,9 instead of address 0,4,8, there could be a performance penality depending on processor type. malloc will already return address on a 4 bytes (or is it 8 bytes) boundary.

Thank you mario

but I’ve read it gives varieties.

Stack management would be extremely faster then malloc/free
because the whole Stack-Management is implemented in the Processor (hardware),
and all will happend in 0 cycles of the Processor (>@1).
The Page allocation (4KB) is implemented on Processor on hardware side.
And if you leave a function, whole memory will be free automatically.

And if I make a test (Message Passing with big blocksizes) I find out the opposite of it, it seems faster to work with malloc

string = (char *)malloc(BLOCKSIZE * sizeof(char));

than with fix Arrays

unsigned char string[BLOCKSIZE];

so I’am confused and I have to check my code again

Ah you mean obtaining the memory, yes then in that case you are right. I assumed you meant accessing the memory, like doing the memset itsef.

If stack has to grow and allocated new pages there will be a performance hit I beleive (OS has to figure out where to physicaly allocated the stack).

Stack management (assuming no new pages are used) does use a couple of CPU instructions

Yes memory is free automaticaly for stack. But you may not want to do that if you need to return the data to the parent fonction.

Thank you mario

a possible error ist using malloc and pointers together with sizeof(string) and strlen(string)
I have made an new topic with this.

“sizeof(string) versus strlen(string)”

But if you put a block on stack, every time you get into that function you have to “allocate” it. Where with malloc(), you can allocate it once and keeps using it.

But then the function can’t be thread safe ;-) But I think mike’s got enough stuff to digest we don’t want to get into that ;)