0% found this document useful (0 votes)
28 views

Dynamic Memory Allocation

Dynamic memory allocation allows programs to dynamically manage memory during runtime. It involves allocating memory from the heap using functions like malloc(), calloc(), and realloc() and freeing it using free(). Malloc allocates uninitialized memory, calloc initializes to zeros, and realloc changes the size of pre-allocated memory. Proper usage prevents memory leaks. Byte manipulation functions like memset(), memcpy() operate on memory. Conversions may be needed between host and network byte order.

Uploaded by

Miranda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Dynamic Memory Allocation

Dynamic memory allocation allows programs to dynamically manage memory during runtime. It involves allocating memory from the heap using functions like malloc(), calloc(), and realloc() and freeing it using free(). Malloc allocates uninitialized memory, calloc initializes to zeros, and realloc changes the size of pre-allocated memory. Proper usage prevents memory leaks. Byte manipulation functions like memset(), memcpy() operate on memory. Conversions may be needed between host and network byte order.

Uploaded by

Miranda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Dynamic Memory Allocation

Abdul Ghafoor
Faculty Member
Contents
• Memory Allocation
• Dynamic Memory Allocation
• Byte Manipulation functions
• Byte Ordering functions
Memory Allocation
• When a program is loaded into memory, it is
organized into three areas of memory, called
segments:
– text segment: where the compiled code of the
program itself resides.

– stack segment: where memory is allocated for


automatic variables within functions

– heap segment: where memory is allocated at run


time and remains in existence for the duration of a
program. It enables us to create data types and
structures of any size and length
Stack and Heap Based
Memory
• Run-time stack Heap
free
push
top
pop

heap
stack

Dynamic Memory Allocation


Contents
• Memory Allocation
• Dynamic Memory Allocation
• Byte Manipulation functions
• Byte Ordering functions

Dynamic Memory Allocation
• Dynamically allocating N bytes of memory using
library functions.

• The memory is allocated from the program heap.

• The dynamically allocated memory should be


returned to the operating system to prevent memory
leaks.

• All memory allocation functions return generic void


pointers.
DMA Functions
• void *malloc(size_t size)

• void *calloc(size_t nmemb, size_t size)

• void* realloc(void *ptr, size_t size)

• void free(void* ptr)

<stdlib.h>
DMA Functions: malloc()
• The malloc() allocates un-initialized memory.
void *malloc(size_t requestedSize);
T *p;
p = (T *)malloc(sizeof(T));

• You should always remember to check if a call to a


memory allocation function was successful.
• Always pass sizeof(type) as a parameter to a
malloc() call, rather than the absolute value.
For example, use
malloc(sizeof(int))
instead of
malloc(2)
Example: malloc()
• int *p;
• if((p = (int *)malloc(sizeof(int))) == NULL)
exit(1);
• *p = 12;

int *q;
if((q = (int *) malloc(3*sizeof(int))) == NULL)
exit(1);
*q = 5;
DMA: calloc()
void *calloc(size_t nmemb, size_t size)
T *p;
p = (T *)calloc(10,sizeof(T));

• It initializes each bit of memory with zero.

• You should always remember to check if a call to a memory


allocation function was successful.

• Always pass sizeof(type) as a parameter to a calloc() call,


rather than the absolute value.

For example, use


calloc(sizeof(int))
instead of
calloc(2)
Example: calloc()
• int *p;
• if((p = (int *)calloc(1, sizeof(int))) == NULL)
exit(1);
• *p = 12;

int *q;
if((q = (int *)calloc(3, sizeof(int))) == NULL)
exit(1);
*q = 5;
DMA: realloc()

• The realloc() is used to change the size of


pre-allocated buffer
– the buffer allocated by the malloc or calloc
function.
– This function returns null if the buffer can not be
extended.
– The previously stored values are copied to the
new extended buffer.
void* realloc(void *ptr, size_t size)
Example: realloc()
#include <stdlib.h>
int main()
{
int *q;
if((q = (int *)malloc(3*sizeof(int))) == NULL)
exit(1);
*q = 5;
q=(int *)realloc(q, 6*(sizeof(int)));
}

What is wrong with above code?


DMA: Memory Deallocation
void free(void* ptr)
• The free() is used to deallocate memory
• Always use after memory allocation

Example
• Example:

int *q;
if((q = (int *)calloc(3, sizeof(int))) == NULL)
exit(1);
*q = 5;
free(q);
DMA: alloca()
• The alloca() also allocates memory just like malloc
but memory is allocated on the stack rather than
heap.

• The memory is automatically de-allocated

• Programmer does not explicitly need to free up the


stack.

• This function is usually used for small and temporary


allocations.
void* alloca(size_t size)
Contents
• Memory Allocation
• Dynamic Memory Allocation
• Byte Manipulation functions
• Byte Ordering functions

Byte Manipulation functions
Following Berkeley-derived functions are defined in
the “strings.h”
• void bzero(void *dest, size_t nbytes);
• void bcopy(const void *src, void *dest, size_t
nbytes):
– bcopy moves the specified number of bytes from
the source to the destination
• int bcmp(const void *ptr1, const void *ptr2, size_t
nbytes);
– bcmp compares two strings. The return value is
zero if the two byte strings are identical; otherwise,
it is nonzero.
Byte Manipulation functions
Following ANSI C functions are defined in “string.h”
• void *memset(void *dest, int c, size_t len)
– memset sets the specified number of bytes to the
value c in the destination
• void *memcpy(void *dest, const void *src,
size_t nbytes): memcpy is similar to bcopy
• int memcmp(const void *ptr1, const void
*ptr2, size_t nbytes);
Contents
• Memory Allocation
• Dynamic Memory Allocation
• Byte Manipulation functions
• Byte Ordering functions
Byte ordering functions
• There are two ways to store the two bytes in memory:
• little-endian byte order : low-order byte at the
starting address
• big-endian byte order: high-order byte at the
starting address
A+1 A
High-byte order Low-byte order little-endian byte order

MSB LSB
High-byte order Low-byte order big-endian byte order
A A+1
The same discussion applies to a 32 bit integer
Machines and Byte order

PowerPC-AIX Big-Endian
DEC-OSf4 Little-Endian
SUN-Solaris Big-Endian
HP-HPUX Big-Endian
586-Linux Little-Endian
Example
Byte Order
• Some systems use big-endian notation and some
systems use little-endian notation.

• The byte order of a machine in consideration is


referred as host byte order.

• Network sending protocol stack and the receiving


protocol stack must agree on the order in which the
bytes of these multi-byte fields are transmitted.

• The internet protocol uses big-endian byte ordering


for these multi-byte integers
Conversion functions
#include <netinet/in.h>

//to return value in network byte order


uint16_t htons(uint16_t host16bitvalue)
uint32_t htonl(uint32_t host32bitvalue)

//to return value in the host byte order


uint16_t ntohs(uint16_t net16bitvalue)
uint32_t ntohl(uint32_t nett32bitvalue)
Lab Session
• Perform the following steps
– Create a structure Paper
Paper
– Define a variable * A1
–int length
– Store A1 in the heap (malloc) –int width
– Display the status of structure –int height
– Assign values to each element –Color
– Display the status of structure •int red
– Deallocate memory •int green
•int blue
• Repeat above steps using calloc
• Write a program in C that will display the
byte-order of the operating system
Analyze the output…
Assignment # 2
• Construct the TCP packet using structures
and bitfields (Read RFC for TCP packet
format)
– Allocate memory to the packet
– Fill this packet with arbitrary values
– Display value in the form of bits (binary)

Deadline: Wednesday 26-04-2006 (Demonstrate in the lab session)


TCP, UDP
&
Socket
>>>

You might also like