ios-c_day12___ Memory

Source: Internet
Author: User

2015.2.3

Memory

. Text: Executable program

. Data: is divided into read-only data segments, as well as readable writable data segments, read-only data segments hold constants such as: "Hello World" readable data can be initialized global variables and static keyword modified variables

. BSS: Uninitialized global variables and static modified variables

. Heap: Also known as heap memory, manual application required, manual release, large heap space, running less efficient than stack memory

. Stack: Also known as the stack area, storing local variables, the system automatically frees up memory, memory is relatively small, running the most efficient

Static

1. Modifying global variables, valid only in the current. c file

2. Modifies the local variable, scope is the scope of the local variable, but is stored in the. Data area, only the first call to the use of open memory

3. Modifier function, this function can only be used within the current. c file

#include "Static.h"

static int num=100;

int main (int argc,const char *argv[])

{

printf ("num =%d\n", num);

num++;

printf ("num =%d\n", num);

printf ("value =%d\n", value);

Print ();

ChangeValue (300);

Print ();

for (int i=0; i<10; i++) {

Printscore ();

}

Printhello ();

Hello ();

return 0;

}

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

Malloc

void *memset (void *, int, size_t);

void *malloc (size_t);

int main (int argc, const char * argv[]) {

1. Request Memory

Char *pstr = (char *) malloc (100*sizeof (char));//possible memory request failed, memory request failed returning null;

int *score = malloc (50*sizeof (int));

2. Determine if memory is successfully applied

if (!PSTR)

//    {

return-1;

//    }

3. The requested memory value is undefined, the memory needs to be assigned to the initial

for (int i=0; i<100; i++) {

printf ("%c", * (Pstr+i));

////    }

//    //

memset (pstr,0,100);//Memory Clear 0

for (int i=0; i<100; i++) {

printf ("%d", * (Pstr+i));

////    }

4. Use memory,

scanf ("%s", pstr);

printf ("%s\n", pstr);

5. Release the memory, tell the operating system that the current memory can be used by other programs

Free (PSTR);

6. Pointer to this memory pointer variable is assigned a value of NULL

Pstr = NULL;

//

return 0;

//}

Calloc

void *calloc (size_t count, size_t size);

1. Memory Size = count * Size

2. The requested memory is initialized to 0

The Calloc () function contiguously allocates enough space for count objects that is size bytes of memory each and retur  NS A pointer to the allocated memory. The allocated memory is filled with bytes of value zero.

int main (int argc,const char *argv[])

//{

int *score = (int *) calloc (ten, sizeof (int));

if (!score) {

return-1;

//    }

for (int i=0; i<10; i++) {

scanf ("%d", &score[i]);

//    }

for (int i=0; i<10; i++) {

printf ("%d", score[i]);

//    }

printf ("\ n");

Free (score);

Score = NULL;

return 0;

//}

Using malloc and memset to achieve My_calloc

void *my_calloc (size_t count, size_t size)

//{

void *p = malloc (count*size);

if (!p) {

return NULL;

//    }

memset (p, 0, count*size);

return p;

//}

//

int main (int argc, const char *argv[])

//{

Char *pstr = My_calloc (n, sizeof (char));

if (!PSTR) {

return-1;

//    }

scanf ("%s", pstr);

printf ("%s\n", pstr);

Free (PSTR);

Pstr = NULL;

//

return 0;

//}

Free ()

The free () function deallocates the memory allocation pointed to by PTR. If ptr is a NULL pointer, no operation is performed.

1. Cannot release a null pointer

2. The same piece of memory cannot be freed multiple times

int main (int argc,const char *argv[])

//{

Char *ptr = (char *) malloc (100*sizeof (char));

if (!ptr) {

return-1;

//    }

Free (PTR);

Free (PTR);

ptr = NULL;

return 0;

//}

ReAlloc

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

ReAlloc (NULL, a) <==> malloc (100)

ReAlloc (PTR, 0) <==> free (PTR)

If the PTR pointer points to a memory space that is sufficient to extend the new memory, PTR points to a memory address that is not changed, otherwise, re-creates a new block of memory in the heap memory, copies the contents of the original memory block into a new memory block, and frees the original memory block, and the expanded memory does not necessarily have an initial value of 0

The ReAlloc () function tries to change the size of the allocation pointed to by PTR to size, and returns PTR. If There is not enough guest enlarge the memory allocation pointed to by PTR, realloc () creates a new allocation, copies As much of the old data pointed to by PTR as would fit to the new allocation, frees the old allocation, and returns a Poin  ter to the allocated memory.  If ptr is Null,realloc () are identical to a call to malloc () for size bytes.  If size is zero and PTR are not NULL, a new, minimum sized object is allocated and the original object is freed. When extending a region allocated with Calloc (3), realloc (3) does isn't guarantee that the additional memory is also zero-f Illed.

int main (int argc,const char *argv[])

//{

Char *ptr = malloc (10*sizeof (char));

if (!ptr) {

return-1;

//    }

printf ("%p\n", PTR);

scanf ("%s", PTR);

printf ("%s\n", PTR);

int len = (int) strlen (PTR);

//

ptr = ReAlloc (ptr, 1000);

printf ("%p\n", PTR);

scanf ("%s", Ptr+len);

printf ("%s\n", PTR);

//

Free (PTR);

ptr = NULL;

return 0;

//}

ReAlloc (NULL, a) <==> malloc (100)

ReAlloc (PTR, 0) <==> free (PTR)

int main (int argc,const char *argv[])

//{

Char *ptr = (char *) realloc (NULL, 100*sizeof (char));//malloc (100)

if (!ptr) {

return-1;

//    }

scanf ("%s", PTR);

printf ("%s\n", PTR);

ReAlloc (PTR, 0);//<==> free (PTR)

ptr = NULL;

return 0;

//}

Memchr

void *MEMCHR (const void *s, int c, size_t n);

S: the memory address to find

C: the character to find

N: Find the size of a memory block

Returns the address of the first occurrence of a character in memory and cannot find a return null

/*int Main (int argc,const char *argv[])

{

Char *pstr = (char *) malloc (100*sizeof (char));

if (!PSTR) {

return-1;

//    }

memset (pstr, 0, 100);

scanf ("%s", pstr);

printf ("%s\n", MEMCHR (pstr, ' A ', 100));

Free (PSTR);

Pstr = NULL;

int *pint = malloc (100*sizeof (int));

if (!pint) {

return-1;

}

memset (pint, 0, 100*sizeof (int.));

pint[10]=97;

printf ("%c\n", * (char *) MEMCHR (pint, ' a ', 100*sizeof (int)));

return 0;

}

*/

Compare the size of two memory blocks

int memcmp (const void *S1, const void *S2, size_t n);

S1,s2 points to two pieces of memory, respectively.

N: Compare the number of bytes in memory

int main (int argc,const char *argv[])

//{

int a[5]={1,2,-1,4,5};

int b[5]={1,2,3,4,5};

//

int ret = MEMCMP (A, B, 20);

printf ("ret =%d\n", ret);

//

return 0;

//}

void *memcpy (void *dst, const void *SRC, size_t n);

The memcpy () function copies n bytes from memory area src to memory area DST.  If DST and src overlap,behavior is undefined. Applications in which DST and SRC might overlap should use memmove (3) instead

Copy the value of the memory space that SRC points to the memory space that the DST pointer points to, and DST cannot overlap with the memory space that SRC points to;

/*int Main (int argc,const char *argv[])

{

Char str[100]= "Helloworldqianfengjiaoyu";

Char *str=malloc (100*sizeof (char));

strcpy (str, "Helloworldqianfengjiaoyu");

Char buf[100]={};

memcpy (buf, str, 10);

printf ("%s\n", buf);

memcpy (&str[1], str, 20);

printf ("%s\n", str);

return 0;

}*/

void *memmove (void *dst, const void *SRC, size_t n);

Copy the value of SRC to the memory space by copying n bytes into the memory that DST points to

The memory space that DST and src point to can overlap

int main (int argc,const char *argv[])

//{

Char dst[100]={};

Char src[100]= "Qianfengjiaoyu";

src[20]= ' A ';

Memmove (DST, SRC, 21);

printf ("%s\n", DST);

int len = (int) strlen (SRC);

Memmove (SRC, &src[1], len-1);

* (src+len-1) = ' + ';

printf ("%s\n", SRC);

//

return 0;

//}

void *memmem (const void *big, size_t big_len, const void *little, size_t little_len)//c89 Standard does not have this function

int main (int argc,const char *argv[])

{

Char big[100]= "Hellowordlqianfengjiaoyu";

Char little[20]= "Feng";

char *pstr = NULL;

PSTR = Memmem (big, Little, 3);

printf ("%s\n", pstr);

return 0;

}

ios-c_day12___ Memory

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.