C programming
Trainer : Nisha Dingare
Email :
[email protected] Sunbeam Infotech www.sunbeaminfo.com
2-D arrays
Sunbeam Infotech www.sunbeaminfo.com
2-D Arrays :
Sunbeam Infotech www.sunbeaminfo.com
Array of pointers :
Sunbeam Infotech www.sunbeaminfo.com
Passing 2-D array to functions :
• 2-D array is passed to function by address.
• It can be collected in formal argument using array notation or pointer notation.
• While using array notation, giving number of rows is optional. Even though mentioned, will be ignored
by compiler.
Sunbeam Infotech www.sunbeaminfo.com
Command Line Arguments :
Sunbeam Infotech www.sunbeaminfo.com
Standard main() prototypes :
• int main();
• int main(int argc, char *argv[]);
• int main(int argc,char*argv[],char*env[]);
• argc represents number of arguments passed to program when it is executed from command line.
• argv represents argument vector or argument values.
• envp represents system information.
Sunbeam Infotech www.sunbeaminfo.com
Command Line Arguments :
Sunbeam Infotech www.sunbeaminfo.com
Dynamic Memory Allocation :
• Dynamic memory allocation allow allocation of memory at runtime as per requirement.
• This memory is allocated at runtime on Heap section of process.
• Library functions used for Dynamic memory allocation are
• malloc() – allocated memory contains garbage values.
• calloc() – allocated memory contains zero values.
• realloc() – allocated memory block can be resized (grow or shrink).
• All these function returns base address of allocated block as void*.
• If function fails, it returns NULL pointer.
• The memory block allocated by malloc() has a garbage value.
• The memory block allocated by calloc() is initialized by zero.
Sunbeam Infotech www.sunbeaminfo.com
Dynamic Memory Allocation :
• Dynamic memory can be requested using malloc,calloc,realloc function
• Such requested memory will be in control of user programmer.
• On demand programmer request memory utilse same and once job is finished programmer has to
release such dynamic memory.
• We can shrink or grow dynamic memory at runtime.
• malloc,calloc,realloc function provides memory from heap section.
• If request is successful they will return base address of memory else return NULL.
• void* malloc(int size);
• void* calloc(int count,int ele size);
• void* realloc(void *mem block,int size);
• The address returned by these functions should be type-casted to the required pointer type.
Sunbeam Infotech www.sunbeaminfo.com
Difference between malloc() and calloc() :
Sunbeam Infotech www.sunbeaminfo.com
Memory Leakage :
• If memory is allocated dynamically, but not released is said to be "memory leakage".
• Such memory is not used by OS or any other application as well, so it is wasted.
• In modern OS, leaked memory gets auto released when program is terminated.
• However for long running programs (like web-servers) this memory is not freed.
• More memory leakage reduce available memory size in the system, and thus slow down whole
system.
• In Linux, valgrind tool can be used to detect memory leakage.
• int main() {
int *p = (int*) malloc(20);
int a = 10;
p = &a; // here addr of allocated block is lost, so this memory can never be freed.
// this is memory leakage
return 0;
}
Sunbeam Infotech www.sunbeaminfo.com
Dangling Pointer :
• Pointer keeping address of memory that is not valid for the application, is said to be "dangling pointer".
• Any read/write operation on this may abort the application. In Linux it is referred as "Segmentation
Fault".
• Examples of dangling pointers
• After releasing dynamically allocated memory, pointer still keeping the old address.
• Uninitialized (local) pointer
• Pointer holding address of local variable returned from the function.
It is advised to assign NULL to the pointer instead of keeping it dangling.
int main() {
int *p = (int*) malloc(20);
free(p); // now p become dangling
return 0;
}
Sunbeam Infotech www.sunbeaminfo.com
Thank you!!
Sunbeam Infotech www.sunbeaminfo.com