首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > SSD6::内存处理不当分析例程
【标  题】:SSD6::内存处理不当分析例程
【关键字】:SSD6
【来  源】:http://www.cublog.cn/u/17503/showart.php?id=110262

SSD6::内存处理不当分析例程

作业中的分析程序
 

/*
 Author : stoneJam
*/

#include <stdio.h>
#include <stdlib.h>
//you can change for more letters
#define MAXLETTER 1000
void main() {
        char *str, *input;
        int *ilist;
        int i, size1, size2;
       
        printf("Number of letters in word: ");
        scanf("%d", &size1);                    /* user inputs an integer */
        printf("Number of integers: ");
        scanf("%d", &size2);                    /* user inputs an integer */
       
  //  ERROR #1
  //  str = (char *) malloc(size1);
  //  should malloc size1+1 for str
  //  because a string need extra 1 byte to stroe a '\0' ending
  str = (char *) malloc(size1+1);
    ilist = (int *) malloc(size2);  
  input = (char*) malloc(MAXLETTER);  // we will use input as a buffer
       
    printf("Word: ");
    // ERROR #2
    //  scanf("%s", str);                       /* user inputs a string */
    //  may input more letters than size1 and overwrite 
    //  ending of str
  scanf("%s", input);    //store user'input to input
  for ( i = 0 ; i < size1; i++){      
   str[i] = input[i];   //copy size1 characters to str
  }
  str[size1] = '\0';    //put an leagal ending to str
        for(i = 0; i < size2; i++) {
                printf("Number %d of %d: ", i + 1, size2);
                scanf("%d", ilist + i);         /* user inputs an integer */
        }
}

/*
 Author : stoneJam*/

//----------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* return 1 if str is "1", 0 otherwise */
int checkIf1(char *str) {
        char *newstr = (char*)malloc(strlen(str) + 1);

  // ERROR #1
        // strcat(newstr, str); /* set newstr to str */
  // to set newstr to str we should use
  // strcpy(newstr, str);
  strcpy(newstr, str); /* set newstr to str */

        if (strcmp(newstr, "1") == 0) { /* newstr is "1" */
    //a memory leak occured here
    free(newstr);  // add this statement to free newstr before return
                return 1;
        }
        free(newstr);
        return 0;
}

void main() {
        char *strArr[4] = {"1", "2", "3", "4"};
        int i;
       
        for(i = 0; i < 4; i++) {
                printf("%d\n", checkIf1(strArr[i]));
        }
}


/*
 Author : stoneJam*/

//----------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct data {
        char *str1, *str2;
};

/* returns two strings concatenated if they are not the same, NULL otherwise */
char *mergeSingleIfDifferent(char *s1, char *s2) {
        char *str = (char *) malloc(strlen(s1) + strlen(s2) + 1);
        if (strcmp(s1, s2) == 0) { /* strings are equal */
   // ERROR #2
   // if str is assigned to NULL
   // the memory malloc to it will never be freed
   // free str before set str to NULL
   free(str);
            str = NULL;    
        }
        else {
                strcpy(str, s1);
                strcat(str, s2);
        }
        return str;
}

/* copies merged strings (or NULL) into array of strings passed in (results) */
void mergeArrayIfDifferent(char *results[], char *strA1[], char *strA2[], int size) {
        int i;
       
        for(i = 0; i < size; i++) {
                results[i] = mergeSingleIfDifferent(strA1[i], strA2[i]);               
        }
}

void printAndFree(int c, char *str) {
        if (str != NULL) {
                printf("%d: %s\n", c, str);
                free(str);
        }
}

void main() {
        char *strArr1[8] = {"1", "2", "3", "4", "5", "6", "7", "8"};
        char *strArr2[8] = {"a", "2", "c", "4", "e", "6", "g", "8"};
        char *results[8];
        int i;
       
        mergeArrayIfDifferent(results, strArr1, strArr2, 8);
        for(i = 0; i < 8; i++) {
   // ERROR #1
            // printAndFree(i, results);
   // results's type is  char *[8]
   // while param2 of printAndFree is char*
   printAndFree(i, results[i]);
        }
}


 

个人收藏的编程下载资源:【上一篇】
Linux下C语言头文件的包含:【下一篇】
【相关文章】
  • SSD6::Performance Measurement and Improvement
  • 笔记(51)::SSD6::Memory Layout and Allocation
  • SSD6::UNIT3::TESTFILE
  • 【随机文章】
  • 安装配置telnet服务
  • 什么是JAVA内容仓库(Java Content Repository)(3)
  • JAVA,JSP入门问题集锦1(原创)
  • How CGI interacts with C ? zz
  • 没有需求分析的工程,真困难啊
  • Creating tablespaces with multiple block sizes
  • 手工添加系统服务
  • 数学之美 系列一 -- 统计语言模型
  • 如何手动删除 SQL Server 2000 默认实例、命名实例或虚拟实例
  • 在Word中为三位数字设置带圈字符
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.