内存释放

来源:互联网 发布:影子武士1优化补丁 编辑:程序博客网 时间:2024/04/28 20:36
栈区(stack)—由编译器自动分配释放,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。堆区(heap)—一般由程序员分配释放,若程序员不释放,程序结束时可能由OS回收。注意它与数据结构中的堆是两回事,分配方式倒是类似于链表全局区(静态区)(static)—全局变量和静态变量的存储是放在一块的,初始化的全局变量和静态变量在一块区域,未初始化的全局变量和未初始化的静态                                  变量在相邻的另一块区域。  程序结束后由系统释放。常量区—常量字符串就是放在这里的,直到程序结束后由系统释放。上面的问题就在这里!!!代码区—存放函数体的二进制代码。

直接搬运的代码

//main.cpp
int a = 0; //全局初始化区
char *p1; //全局未初始化区

main()
{
int b; //栈
char s[] = “abc”; //栈
char *p2; //栈
char *p3 = “123456”; //123456\0在常量区,p3在栈上。
static int c =0;//全局(静态)初始化区
p1 = (char *)malloc(10);
p2 = (char *)malloc(20);//分配得来得10和20字节的区域就在堆区。
strcpy(p1, “123456”); //123456\0放在常量区,编译器可能会将它与p3所指向的”123456”优化成一个地方。
}

复制代码

此外,还有realloc(重新分配内存)、calloc(初始化为0)、alloca(在栈上申请内存,自动释放)等。

http://blog.csdn.net/xukai871105/article/details/17485349

char remote_server[] = “192.168.1.106”; // 主机IP地址或主机域名
char remote_path[] = “/add.php”; // 文件地址
int value1 = 10;
int value2 = 20;
void tcpclient(const char* host_name, int port)
{
(void)port;
(void)host_name;

int sock, bytes_received;// HTTP请求和HTTP响应 缓冲区char* http_request = rt_malloc(256);if (http_request== RT_NULL){    rt_kprintf("No memory\r\n");return;}char* http_response = rt_malloc(512);if (http_response == RT_NULL){    rt_kprintf("No memory\r\n");return;}struct hostent *remote_host;remote_host = gethostbyname(remote_server);if( remote_host == NULL ){    rt_kprintf("DNS Failed\r\n");return;}struct sockaddr_in remote_sockaddr;remote_sockaddr.sin_family = AF_INET;remote_sockaddr.sin_port = htons(80);// remote_sockaddr.sin_addr.s_addr = inet_addr("192.168.1.106");remote_sockaddr.sin_addr.s_addr =                            *(unsigned long *)remote_host->h_addr_list[0];rt_memset(&(remote_sockaddr.sin_zero), 0, sizeof(remote_sockaddr.sin_zero));while(1){    // 第二步 创建套接字    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)    {        rt_kprintf("Socket error\n");        return;    }    // 第三步 连接remote    if (connect(sock, (struct sockaddr *)&remote_sockaddr, sizeof(struct sockaddr)) == -1)    {        rt_kprintf("Connect fail!\n");        lwip_close(sock);        return;    }    // Http内容,表单内容    char http_content[64] = {0,};    // 确定HTTP表单提交内容    sprintf( http_content , "value1=%d&value2=%d" , value1,value2);    // 确定 HTTP请求首部 例如POST /add.php HTTP/1.1\r\n    char http_header[64] = {0,};    sprintf( http_header , "POST %s HTTP/1.1\r\n",remote_path);    strcpy( http_request , http_header ); // 复制到请求缓冲区中    // Http属性    char http_attribute[64] = {0,};    // 增加属性 例如 Host:192.168.1.106\r\n    sprintf( http_attribute , "Host:%s\r\n" , remote_server);    strcat( http_request , http_attribute);    memset( http_attribute , 0 , sizeof(http_attribute));    // 增加提交表单内容的长度 例如 Content-Length:19\r\n    sprintf( http_attribute , "Content-Length:%d\r\n" ,strlen(http_content) );    strcat( http_request , http_attribute);    memset( http_attribute , 0 , sizeof(http_attribute));    // 增加表单编码格式 Content-Type:application/x-www-form-urlencoded\r\n    strcat( http_request , "Content-Type:application/x-www-form-urlencoded\r\n");    memset( http_attribute , 0 , sizeof(http_attribute));    // HTTP首部和HTTP内容 分隔部分    strcat( http_request , "\r\n");    // HTTP负载内容    strcat( http_request , http_content);    // 发送Http请求    send(sock,http_request,strlen(http_request), 0);    // 获得Http响应    bytes_received = recv(sock, http_response, 1024 - 1, 0);    http_response[bytes_received] = '\0';    // 分析和输出结果    char* presult = strstr( http_response , "\r\n\r\n");    int result_value = atoi( presult + strlen("\r\n\r\n") );    // value1和value2累加    rt_kprintf("value1:%d value2:%d result:%d\r\n" ,               value1++, value2++,result_value );    rt_memset(http_response , 0 , sizeof(http_response));    // 关闭套接字    closesocket(sock);    // 延时5S之后重新连接    rt_thread_delay( RT_TICK_PER_SECOND * 10 );}

}

0 0