Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Despite the performances issues, I currently don't use the arithmetic of pointers in the block of memory allocated dynamically such:
int* foo =malloc(sizeof(int)*100);
     *(foo+1)=100; //I do not use this form
     foo[1]=100;//but this

this because I am afraid of the possible fragmentation of the memory. That randomly could crush and raise an infamous SIGSEV.
But are my fears justified? Can I use the arithmetic of pointers with allocated memory?

What I have tried:

I cannot try, since the problem comes from the way the program allocated the memory
Posted
Updated 28-Jun-16 21:42pm

There is no difference between the methods. The compiler will usually generate the same assembly code for both methods.

This is also not related to memory fragmentation. Heap memory fragmentation occurs when allocating and freeing memory frequently which may result in unused freed blocks.

Getting a segmentation fault is the result of accessing memory out of bounds. For your example this happens when using a negative index or an index greater or equal to 100 or - even worse - using casted pointers to a differnt type. Because C has not implemented bound checking, you must do it by writing corresponding code to ensure that it can not happen.

If you got segmentation faults you have to check and debug your code to find the source.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Jun-16 4:00am    
The inquirer never mentioned segmentation fault. The question was about fragmentation, which is totally different thing. Fragmentation is unrelated to the access to memory; it happens as a result of allocations and deallocations.
—SA
Jochen Arndt 29-Jun-16 4:11am    
Quote:
"That randomly could crush and raise an infamous SIGSEV."

I tried also to explain that fragmentation is not related to memory access in my 2nd paragraph.

I just tried to "read into" his question and answer to all mentioned aspects.
Sergey Alexandrovich Kryukov 29-Jun-16 9:01am    
Oh, right. I can see your point.
—SA
No, your fears are not justified. It does not matter whether you use *(foo+1) or foo[1], they both result in exactly the same object code.
 
Share this answer
 
"But are my fears justified?"
No. Not even slightly!
Memory fragmentation is not caused by how you access the memory, it's caused by allocating and freeing chunks of memory in such a way that lots of smaller, useless holes appear that can't be reused as a "bigger chunk" because they aren't contiguous.
There is a good description here: Developer Library » Memory Fragmentation, your worst nightmare[^] - and it has pictures, without which it's really, really difficult to explain what is happening!

But accessing your pointers via pointer arithmetic doesn't have anything to do with fragmentation, as it doesn't cause any new allocation of memory or return any memory to the pool - those have to be explicit in C via malloc and free
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900