Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Other

Memory management in C

Rating
-
Sold
-
Pages
6
Uploaded on
16-03-2023
Written in
2022/2023

Providing a topic of memory management and Error Handling in C Language

Institution
Course

Content preview

Memory Address :-

When a variable is created in C, a memory address is assigned to the variable. The memory address is the
location of where the variable is stored on the computer. When we assign a value to the variable, it is stored in
this memory address.

To access it, use the reference operator (&), and the result represents where the variable is stored:

Example :- int myAge = 43;
printf("%p", &myAge); // Outputs 0x7ffe5367e044

Note :-The memory address is in hexadecimal form (0x..). You will probably not get the same result in your
program, as this depends on where the variable is stored on your computer.

Why We Uses memory address:- Pointers are important in C, because they allow us to manipulate the data in
the computer's memory - this can reduce the code and improve the performance.

The Importance of Memory Management in C :-

This chapter explains dynamic memory management in C. The C programming language provides several
functions for memory allocation and management. These functions can be found in the <stdlib.h> header file.

If you want more control over all this, you need dynamic storage allocation. C supports dynamic storage
allocation, which is the ability to reserve memory as you need it and free that memory as soon as you're finished
using it. Many programming languages have automatic memory allocation and garbage collection that handle
these memory management tasks. C, though, allows (and in some cases requires) you to be explicit about
memory allocation with the following key functions from the standard C library:


Sr.No. Function Description

1 void *calloc(int num, int  Alternate memory allocation options in C are calloc, which
size); also clears the memory when it's reserved.

2 void free(void *address); This function releases a block of memory block specified by address.



3 void *malloc(size_t size); This function allocates an array of num bytes and leave them
uninitialized

, 4 void *realloc(void *address, This function re-allocates memory extending it upto newsize.
int newsize);



Example :- #include <stdio.h>
int main(void)

{

// declare variables

int a;

float b;

char c;

printf("Address of a: %p\n", &a);

printf("Address of b: %p\n", &b);

printf("Address of c: %p\n", &c);

return 0;

}

Output :- Address of a: 0x7ffee63c8620

Address of b: 0x7ffee63c8624

Address of c: 0x7ffee63c861f

File Handling :-

So far the operations using the C program are done on a prompt/terminal which is not stored anywhere. But in
the software industry, most programs are written to store the information fetched from the program. One such
way is to store the fetched information in a file.

Different operations that can be performed on a file are :-

 Creation of a new file (fopen() with attributes as “a” or “a+” or “w” or “w+”)
 Opening an existing file (fopen())
 Reading from file (fscanf() or fgets())
 Writing to a file (fprintf() or fputs())
 Moving to a specific location in a file (fseek(), rewind())

Written for

Course

Document information

Uploaded on
March 16, 2023
Number of pages
6
Written in
2022/2023
Type
OTHER
Person
Unknown

Subjects

$5.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
bantykumar511

Get to know the seller

Seller avatar
bantykumar511 none
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
3 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions