C column of Pointer <2>
来源:互联网 发布:淘宝网男装毛衫 编辑:程序博客网 时间:2024/04/30 17:02
Ok, let us go on the pointer, previous chapters we get the definition of pointer and how the difference variables allocate in the memory,this chapter i will introduce how to allocate a fixed size of block in the memory(called heap) and then move the pointer in this fixed space.
In "C column of Pointer <0>", we have introduced a function called malloc() to allocate fixed block in the heap , if you don't know just refer to that chapter .Here we go, the prototype is
/* size_t is unsigned integer type * you'd better not define 0 or larger number. * return a void type pointer */void *malloc(size_t size);
int *sp = malloc(4*sizeof(int));if(sp == NULL){ //Allocated failure printf("Out of memory"); exit(1); }/*go on the program manipulation*/
My pc is 32bit, and integer type is 32 bits. So this segment of program allocate a 4*4 bytes block in the memory (called heap). The newly allocated block is not initialized. So any data acquired from this block has nothing means, on the contrary, these data maybe confused you if you ignored this issue. So the best way is to initialize them by hand and added this sentence
memset(sp,0,4*sizeof(4));//Initialize zero
Here i draw a schematic to help you understands this process as below
When return pointer sp is not null, so allocate successfully, this position is the show by "Arrow 0" ,if you want reload a integer number to this address. Just like using the pointer assignment
*sp = 100;So the first value has been assigned in the allocated block, if you want to set the second , third etc, just move the pointer sp++, then assign the new value to the new address.
sp++; //point to “Arrow 1”*p = 200;//sp++; //point to "Arrow 2"*p = 300;
(NOT END)
Time limited . Have a nice day!- C column of Pointer <2>
- C column of Pointer <2> malloc() free()
- C column of Pointer <0>
- C column of Pointer <1>
- C column of Pointer <3> malloc() free()
- c++: the hint of pointer
- type conversion of array and pointer in C(2)
- cast of Objective-C pointer type 'NSString *' to C pointer type 'CFStringRef' (aka 'const struct __C
- Parse query on pointer column
- c pointer
- The amazing efficiency of pointer in C Programming Language
- type conversion of array and pointer in C(1)
- 【IOS异常汇总】之 implicit conversion of Object-C pointer type"NSString *" to C pointer type "const void *"
- A example of pointer
- The pointer of function
- the application of pointer
- rename column of oracle
- Mad Pointer in C
- 安装PIG
- 梁静茹罗琦同天产子
- 递归(一)
- 从Unicode到UTF-8的编码
- 再识MinGW和MSYS(二)
- C column of Pointer <2>
- ibatis动态查询条件
- Objective-C ,ios,iphone开发基础:JSON解析(使用苹果官方提供的JSON库:NSJSONSerialization)
- GEM5简介
- 回车与换行的区别
- 权力游戏“紫婚”毁誉参半 童话镇入佳境
- SPOJ 220 . Relevant Phrases of Annihilation(后缀数组)
- HttpURLConnection和HttpClient获取Json数据
- UVA The Mad Numerologist