#include <stdio.h>
#include<malloc.h>
#define IN
#define OUT
// Get file size
int FileSize(IN char *file)
{
FILE *fil;
fil = fopen(file,"rb");
fseek(fil,0L,SEEK_END);
int filesize = ftell(fil);
fseek(fil,0,0);
return filesize;
}
// read the file
int ReadFileData(IN char *fileName, OUT char *filedata)
{
FILE *fpIN;
int fileSizes = FileSize(fileName);
fpIN = fopen(fileName,"rb");
fread(filedata,1,fileSizes,fpIN);
fclose(fpIN);
}
// write the file
int WriteToFile(char *filedata, int size, OUT char *outFileName)
{
FILE *fpOUT;
fpOUT = fopen(outFileName,"w+");
fwrite(filedata,1,size,fpOUT);
fclose(fpOUT);
}
int main()
{
char *origin_file = "test.cpp";
int orgfilesize = FileSize(origin_file); // Get file size
char *file_data= malloc(orgfilesize); // Allocate file size memory
if (file_data == NULL)
return NULL;
ReadFileData(origin_file, file_data); // read the file
char *outFile = "test.txt";
WriteToFile(file_data,orgfilesize,outFile); // write the file
return 0;
}
The following line of code
char *file_data= malloc(orgfilesize);
Malloc function is used to allocate space in C language. The return type is void*. Void* indicates a pointer of undetermined type. C. C++ specifies that the void* type can cast any other type of pointer.
The malloc() function actually finds a space of a specified size in memory and ranges the first address of that space to a pointer variable.
Here the pointer variable can be a single pointer or the first address of an array.
It depends on the size of the malloc() function.
Use GCC compilation to directly pass and print out the following results
Original String: testing.
When compiling with g++, an error and warning will appear, as follows
error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive]
warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
The reason for the error is that c++ is designed to be more secure than C, and it cannot automatically convert void * to other pointer types.
The reason for warning is that the program attempts to convert the string literal (const char [] in c++ and char [] in C language) to char * type,,
char *file_data= (char*) malloc(orgfilesize);
# The return value of the malloc function is a void*, which is assigned to a variable by adding a forced conversion in front of malloc
Introduction to malloc function
malloc function is often used in C language and c++ to dynamically allocate memory space for variables. Malloc requests the system to allocate memory space of the specified size bytes
function void malloc(int size)
explain:
Malloc requests the system to allocate memory space of the specified size bytes. If the allocation is successful, a pointer to the allocated memory is returned; otherwise, a null pointer is returned
this function is included in the header file: \include < malloc.h> you should import the header file *< malloc.h> or < stdlib.h>** when you are using
Note: when the memory is no longer used, the free() function should be used to free the memory block
Common usage
1. When you do not know the definite memory required by a variable
For example, when defining an array, the size of the array is not known until the program is compiled. In this case, you can use the malloc function
int main()
{
int n;
scanf("%d",&n);
int *m=(int *)malloc(sizeof(int)*n); //Defining a pointer variable that points to n int is equivalent to opening an array of n int elements.
// If n is very large, more than 1000000, then opening an int array of this size will cause a stack overflow.
int m[1000000]; //Stack overflow will occur.
return 0;
}
2. Allocate space for structural variables
define a common variable of structure type. You can dynamically apply for memory without malloc. The CPU will allocate memory for structure variables.
typedef struct
{
int n;
char *p;
}node;
int main()
{
node a; //The definition is a structured ordinary variable, you can request memory without using malloc, the CPU will allocate memory for this structured variable
a.n=4;
printf("%d",a->n); //can output successfully
node *b; //defines a structure pointer variable, the CPU will open up memory for this pointer with a size of 4 bytes. But to store the data members of the structure this space is not enough, it will raise a segment error, at this time you must malloc request a structure type size of dynamic memory to store the data members.
//b=(node *)malloc(sizeof(node));
printf("%d",sizeof(b)); // use sizeof(b) to see the size of b is 4
char p[]="abcd";
printf("%d",b->n);
(a->p)=p;
printf("%c",a->p[0]);
return 0;
}
If malloc is not used to allocate space for structure pointer variable B, warning: ‘B’ is used uninitialized in this function [-wuninitialized]|.
3. When defining a structure, you need to pay attention to allocating space for its members in turn
in normal use, after allocating space for a structure with malloc function, operate on its member variable (pointer type).
For example, when the pointer p=null, it will always report “program received signal SIGSEGV, segmentation fault.”
use malloc function