Reallocating Memory realloc function to reallocate, this is done with the realloc function. The realloc function takes two parameters, the base address of memory you want to resize, and the amount of space you want to reserve and returns a pointer to the base address

Files Recovery Software
Home Contact Details Customer Support Download Demo Products  

 
 

Reallocating Memory

It is possible many times while you are programming that you want to reallocate memory. This is done with the realloc function. The realloc function takes two parameters, the base address of memory you want to resize, and the amount of space you want to reserve and returns a pointer to the base address.

Suppose we have reserved space for a pointer called msg and we want to reallocate space to the amount of space it already takes up, plus the length of another string then we could use the following.

msg = (char *)realloc(msg, (strlen(msg) + strlen(buffer) + 1)*sizeof(char));

The following program illustrates the use of malloc, realloc and free. The user enters a series of strings that are joined together. The program stops reading strings when an empty string is entered.

#include <string.h>
#include <malloc.h>

int main()
{
char buffer[80], *msg;
int firstTime=0;

do
{
printf("\nEnter a sentence: ");
gets(buffer);

if (!firstTime)
{
msg = (char *)malloc((strlen(buffer) + 1) *
sizeof(char));
strcpy(msg, buffer);
firstTime = 1;
}

else
{
msg = (char *)realloc(msg, (strlen(msg) +
strlen(buffer) + 1) * sizeof(char));

strcat(msg, buffer);
}

puts(msg);

} while(strcmp(buffer, ""));

free(msg);
return 0;
}

The output of the program will be as follows:

Enter a sentence: Once upon a time
Once upon a time

Enter a sentence: there was a king
Once upon a timethere was a king

Enter a sentence: the king was
Once upon a timethere was a kingthe king was

Enter a sentence:
Once upon a timethere was a kingthe king was


Previous page

page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20

 
 

page 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37

 
 

page 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54

 
 

page 55 | 56 | 57 | 58 | 59 | 60

Next page
 
 
Data Recovery Book
 
Chapter 1 An Overview of Data Recovery
Chapter 2 Introduction of Hard Disks
Chapter 3 Logical Approach to Disks and OS
Chapter 4 Number Systems
Chapter 5 Introduction of C Programming
Chapter 6 Introduction to Computer Basics
Chapter 7 Necessary DOS Commands
Chapter 8 Disk-BIOS Functions and Interrupts Handling With C
Chapter 9 Handling Large Hard Disks
Chapter 10 Data Recovery From Corrupted Floppy
Chapter 11 Making Backups
Chapter 12 Reading and Modifying MBR with Programming
Chapter 13 Reading and Modifying DBR with Programming
Chapter 14 Programming for “Raw File” Recovery
Chapter 15 Programming for Data Wipers
Chapter 16 Developing more Utilities for Disks
Appendix Glossary of Data Recovery Terms
 
 

Pro Data Doctor

Home

Products

Contact Details

Customer Support

Download Demo

Terms and Conditions

 
Pro Data Doctor