Blue Euro--lessonc11[dynamic memory allocation]

Source: Internet
Author: User


#import <Foundation/Foundation.h>
#import "Header.h"

typedef struct student{
Char name[20];
char sex;
int age;
Float score;

}student;

int maxValue (int a, int b);
int square (int n);

char * getString ();

int maxValue (int a, int b) {
int max = Square (a) > square (b)? Square (a): square (b);
return Max;

}

int square (int n) {
int number = n * n;
return number;
}

char * getString () {
char * str = malloc (10);
strcpy (str, "iphone");
return str;
}



int main (int argc, const char * argv[]) {
Dynamically allocating memory
int x = ten, y = 15;
int a = MaxValue (x, y);
printf ("max =%d", a);

char * pStr = getString ();
printf ("str =%s", pStr);

Stack memory function and management features:
Stack memory is the function called when the local variables and parameters to allocate memory, the stack memory is allocated by the operating system, when the function call is complete, the memory allocated to the function of all the stack memory will be collected by the operating system.


Static modified variables will only be initialized once
Statically modified variables are stored in the static area until the end of the program is run and released
for (int i = 0;i < i++) {
static int a = 0;
int * p = &a;
printf ("%p\n", p);
a++;
printf ("A =%d\n", a);
//    }

void * Generic pointer, can be used to pass any type of pointer
int a = 5;
void * p = &a;
printf ("A =%d", * ((int *) p));//coercion type conversion

The off-song pointer variable in the stack stores the first address of a heap (DUI) Area of memory
char * p = malloc (' a ');
*p = ' R ';
printf ("%c", *p);
Freeing memory
Free (P);//Just delete tag, (Mark Delete)

/*
Char str[10] = "Iphne";
Remember to release after you run out of heap memory, or cause a memory leak
char * p = malloc (10);
char * m = p;//prevents loss of first address, resulting in memory leaks
strcpy (P, "iphone");//Copy to heap area
p = "Android";
printf ("%s", p);
Free (m);//Must pass the first address of this heap of memory
Free (P);//Over-release, just release once
p = null;//After release, the pointer is empty to prevent wild pointer anomalies
printf ("%s", p);//Wild Pointer exception
*/

Apply to the operating system for a section capable of storing n integer heap memory space, as an integer one-dimensional array to use
int * Parray = malloc (sizeof (int) * 10);
for (int i = 0; i < i++) {
* (Parray + i) = Arc4random ()% 101;
printf ("%d\t", * (Parray + i));
//    }
Free (Parray);
Parray = NULL;

Request a piece of memory in the heap and use it as a two-dimensional array
int (*p) [3] = malloc (sizeof (int) * 2 * 3);
for (int i = 0; i < 2; i++) {
for (int j = 0; J < 3; J + +) {
* (* (p + i) + j) = Arc4random ()% 51;
printf ("%d\t", * (* (p + i) + j));
//        }
printf ("\ n");
//    }
Free (*p);
p = NULL;


STUDENT stu = {"Zhangyishan", ' M ', 24,86.55};
STUDENT * stu = malloc (sizeof (STUDENT));
strcpy (Stu->name, "Zhangyishan");
(*stu). Sex = ' M ';
Stu->age = 24;
(*stu). score = 86.9;
printf ("%s%c%d%.2f", Stu->name, Stu->sex,stu->age,stu->score);
Free (STU);
Stu = NULL;

There is a string that contains a number that extracts the number from it. Requires dynamic allocation of memory storage
Char temp[100] = {0};
printf ("Please enter a string:");
scanf ("%s", temp);
//
int i = 0;
int count = 0;
while (* (temp + i)! = ') ' {
if (* (temp + i) >= ' 0 ' && * (temp + i) <= ' 9 ') {
count++;//when recording the number of input numbers
//        }
i++;
//    }
Char *pnumber = malloc (count + 1);//define New memory space
i = 0;
int j = 0;
while (* (temp + i)! = ') ' {
if (* (temp + i) >= ' 0 ' && * (temp + i) <= ' 9 ') {
* (Pnumber + j) = * (temp + i);//Assign New memory space
j + +;
//            }
i++;
//        }
* (Pnumber + j) = ' + ';//Last Assignment
printf ("%s", Pnumber);
Free (pnumber);//Release memory
Pnumber = null;//pointer empty

Enter the name of 3 words, dynamically allocate memory to save the word, and at the end of the output
char * words[3] = {0};
printf ("Input:");
for (int i = 0; i < 3; i++) {
Char temp[100] = {0};
scanf ("%s", temp);
unsigned long lenth =sizeof (temp);
char * word = malloc (lenth + 1);
strcpy (Word, temp);
Words[i] = Word;
//    }
for (int i = 0; i < 3; i++) {
printf ("%s\t", Words[i]);
Free (words[i]);
Words[i] = NULL;
//    }

Calloc
Allocate contiguous storage space of n size bytes
The difference from malloc is that Calloc will definitely clear the allocated memory 00
int * p = calloc (n, sizeof (int));
for (int i = 0; i < i++) {
* (p + i) = Arc4random ()% 101;
printf ("%d\t", * (P+i));
//    }

ReAlloc Memory reallocation function
If the check is started from the given function address, if the following is sufficient allocation, the direct append
If the allocation is not enough, a continuous storage space will be found, and the data in the original memory is copied and released.
char * p = malloc (7);
strcpy (P, "iphone");
printf ("%p\n", p);
char * p1 = ReAlloc (P, 17);//Auto release P
printf ("%p\n", p1);
strcpy (p1 + 6, "Android");
printf ("%s", p1);
Free (p1);//Just release P1 to OK
P1 = NULL;

memcpy
int a[3] = {n/a};
int b[4] = {4,5,6,7};
int *p = malloc (sizeof (a) + sizeof (b));
Memory copy function, starting from the address of a, copy to P address, copy n bytes
memcpy (P, a, sizeof (a));
memcpy (p + sizeof (a)/sizeof (int), B, sizeof (b));
for (int i = 0; i < (sizeof (a) + sizeof (b))/sizeof (int); i++) {
printf ("%d\t", * (P + i));
//    }

Memset memory reset function, starting with the given address, resetting to n, resetting the number of bytes
Typically used to purge in-memory data
memcpy (< #void *#>, < #const void *#>, < #size_t #>)
memset (p, 0, sizeof (a) +sizeof (b));
for (int i = 0; i < sizeof (a) + sizeof (b); i++) {
printf ("%d\t", * ((char *) p + i));
//    }

MEMCMP Memory comparison function
Starting from a given two address, compare n bytes, the content of each storage unit, if not equal, returns the difference value.
Char str[5] = "ASDF";
Char str1[5] = "ASDC";
int result = MEMCMP (str, STR1, 4);
printf ("%d", result);

return 0;
}

This article is from the "ios--Training" blog, so be sure to keep this source http://zys2007.blog.51cto.com/8207599/1599934

Blue Euro--lessonc11[dynamic memory allocation]

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.