C Programming language

C Standard Library Function

Day 1: Introduction and brief history of C Programming language

Day 1: Advantages and Disadvantages of C, C Keywords, Data type modifiers in C

Day 1: Data types in C Programming language

Day 1: Secondary data types, Primitive and Non-primitive data types

Day 1: C Variables, C Constant, Format Specifiers in C

Day 2: Write first C program

Day 2: Flow of C program with example, main(), printf(), scanf()

Day 2: Operaters in C Programming language, Arithmetic operators

Day 2: Relational operators and Logical Operators in C Programming language

Day 2: Assignment, Increments and Decrement Operators in C Programming language

Day 3: Conditional statement: if else statement in C Programming language

Day 3: Conditional statement: switch statement in C Programming language

Day 3: Jump statements: return statement in C Programming language

Day 3: Jump statements: go to statement in C Programming language

Day 3: Jump statements: break statement in C Programming language

Day 3: Jump statements: continue statement in C Programming language

Day 4: Loops OR Iteration statement in C Programming language: for Loop

Day 4: Loops OR Iteration statement in C Programming language: while Loop

Day 4: Loops OR Iteration statement in C Programming language: do while Loop

Day 5: Array in C Programming language

Day 5: Access elements of Array in C Programming language

Day 5: One dimensional Array representation in memory using C Programming language

Day 5: Two dimensional Array representation in memory using C Programming language

Day 5: Multidimensional Array in C Programming language

Day 6: Function in C Programming language

Day 6: Definition, Declaration and Calling a Function in C Programming language

Day 6: Passing array to a function in C Programming language

Day 6: Calling Function in C Programming language : Call by value

Day 6: Calling Function in C Programming language : Call by reference

Day 6: Recursive Function in C Programming language

Day 6: Adding function to the library in C Programming language

Day 7: Pointer in C Programming language, How to use Pointer, Pointer declaration

Day 7: NULL Pointers in C Programming language

Day 7: Array of Pointers in C Programming language

Day 7: Pointer arithmetic in C Programming language

Day 7: Pointer to Pointer in C Programming language

Day 7: Pointer to Function in C Programming language: Passing pointers to functions

Day 7: Pointer to Function in C Programming language: Return pointer from functions

Day 8: Strings in C Programming language, Declaring String in C Programming language

Day 8: String functions in C Programming language

Introduction of Structure

Accessing the members of Structure

Structure With typedef Keyword and Use of sizeof function

Example of Structure

Dynamic memory allocation in C: Introduction

adplus-dvertising
Dynamic Memory Allocation in C: Introduction and Example
Previous Home Next

Introduce to dynamic memory allocation

In some programming contexts, you want to process data but don't know what size of it is. For example, you read a list of students from file but don't know how many students record there. specify the enough maximum size for the the array but it is not efficient in memory management. C provides you a powerful and flexible way to manage memory allocation at runtime. It is called dynamic memory allocation. Dynamic means you can specify the size of data at runtime. C programming language provides a set of standard functions prototype to allow you to handle memory effectively. With dynamic memory allocation you can allocate and free memory as needed. Getting to know the size of data before allocating memory, we need to know the way to identify the size of each data so we can allocate memory correctly. We can get the size of data by using sizeof() function. sizeof() function returns a size_t an unsigned constant integer.

For example

sizeof(int); 

It returns 4 bytes in typical 32 bit machines.

#include <stdio.h>  
typedef struct __address{ 
int house_number; 
char street[50]; 
int zip_code; 
char country[20]; 
} address; 
void main() 
{  
printf("size of int is %d byte(s)\n",sizeof(int)); 
printf("size of unsigned int is %d byte(s)\n",sizeof(unsigned int)); 
printf("size of short is %d byte(s)\n",sizeof(short)); 
printf("size of unsigned short is %d byte(s)\n",sizeof(unsigned short)); 
printf("size of long is %d byte(s)\n",sizeof(long)); 
printf("size of char is %d byte(s)\n",sizeof(char)); 
printf("size of float is %d byte(s)\n",sizeof(float)); 
printf("size of double is %d byte(s)\n",sizeof(double)); 
printf("size of address is %d byte(s)\n",sizeof(address)); 
}

[an error occurred while processing this directive]

 

Output:
size of int is 4 byte(s)
size of unsigned int is 4 byte(s)
size of short is 2 byte(s)
size of unsigned short is 2 byte(s)
size of long is 4 byte(s)
size of char is 1 byte(s)
size of float is 4 byte(s)
size of double is 8 byte(s)
size of address is 80 byte(s)
Previous Home Next