| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 567 人关注过本帖
标题:动态分配内存的问题
只看楼主 加入收藏
jh275208498
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2012-7-19
结帖率:33.33%
收藏
已结贴  问题点数:10 回复次数:3 
动态分配内存的问题
大牛们帮帮  , 小弟看到指针的时候,遇到了动态分配内存的 问题, pbuffer = (char*)malloc(BUFFER_LEN);这样的我懂 但是下面用红的标出来的,不明白,为什么要这么做呢?完全可以用 pbuffer = (char*)malloc(BUFFER_LEN);这种方式啊。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_LEN 5          /* Initial length of input buffer     */
#define BUFFER_LEN_INCR 2     /* Increment to buffer length         */
#define CAPACITY_INCR 2       /* Increment to capacity for proverbs */

int main(void)
{
   char **pProverbs = NULL;        /* Pointer to string pointer           */
   char **temp = NULL;             /* Temporary pointer to string pointer */
   int capacity = 0;               /* Number of proverbs that can be stored */
   int count = 0;                  /* Number of proverbs read               */
   char *pbuffer = NULL;           /* Pointer to buffer        */
   char *pstart = NULL;            /* Pointer to buffer start  */
   char *pstr = NULL;              /* Pointer to a string      */
   int buffer_length = BUFFER_LEN; /* Buffer length            */

   pbuffer = (char*)malloc(BUFFER_LEN);  /* Initial buffer size   这个明白*/
   pstart = pbuffer;                     /* Store start of buffer */

   for(;;)
   {
     if(count==capacity)
     {
       capacity += CAPACITY_INCR;
       temp = (char**)malloc(capacity*sizeof(char*));///这句话不明白
       if(!temp)              /* If memory was not allocated */
       {                      /* Output a message  and end   */
         printf("Memory allocation failed. Terminating program.");
         exit(1);
       }

       if(!pProverbs)         /* Are there any proverbs?                 */
         pProverbs = temp;    /* No - so just copy address of new memory */
       else                   /* Yes - so copy data from old to new      */
       {
         for(int i = 0 ; i<count ; i++)
           *(temp+i) = *(pProverbs+i);
         free(pProverbs);     /* Free the old memory */
         pProverbs = temp;    /* Copy address of new */
       }
       temp = NULL;           /* Reset pointer       */
     }
      printf("Enter a proverb or press Enter to end:\n");

     /* Read a proverb */
     while((*pbuffer++ = getchar()) != '\n')
     {
       if(pbuffer-pstart == buffer_length)     /* Check for buffer full  */
       {
         buffer_length += BUFFER_LEN_INCR;     /* Increase buffer length */
         pstr = (char*)malloc(buffer_length);  /* Allocate new buffer    */

         /* Copy old buffer contents to new */
         for(int i = 0; i<pbuffer-pstart ; i++)
           *(pstr+i) = *(pstart+i);

         pbuffer = pstr+(pbuffer-pstart);      /* Address of next position in new */
         free(pstart);                         /* Release old buffer memory       */
         pstart = pstr;                        /* Set to start of new buffer      */
         pstr = NULL;                          /* Reset pointer                   */
       }
     }

     /* check for empty line indicating end of input */
     if((pbuffer-pstart)<2)
       break;

     /* Check for string too long */
     if((pbuffer - pstart) == BUFFER_LEN && *(pbuffer-1)!= '\n')
     {
       printf("String too long ?maximum %d characters allowed.",
                                                       BUFFER_LEN);
       pbuffer = pstart;
       continue;
     }
     *(pbuffer-1) = '\0';                  /* Add string terminator */
     pbuffer = pstart;

     *(pProverbs+count) = (char*)malloc(strlen(pstart)+1);
     strcpy(*(pProverbs+count++),pstart);
   }

   /* Order the proverbs from shortest to longest */
  for(int i = 0 ; i<count-1 ; i++)
    for(int j = i+1 ; j<count ; j++)
      if(strlen(*(pProverbs+i))>strlen(*(pProverbs+j)))
      {
        pstr = *(pProverbs+i);
        *(pProverbs+i) = *(pProverbs+j);
        *(pProverbs+j) = pstr;
      }

   /* Output all the strings */
   printf("\nIn ascending length order, the proverbs you entered are:\n");
   for (int i = 0 ; i<count ; i++ )
   {
      printf("\n%s", *(pProverbs + i));
      free(*(pProverbs + i));   /* Release the memory for the proverb  */
      *(pProverbs + i) = NULL;  /* Set pointer back to NULL for safety */
   }
   free(pProverbs);             /* Release memory for the pointers     */
   free(pstart);                /* Release memory for the buffer       */
}

搜索更多相关主题的帖子: 内存 capacity include 动态 
2012-07-27 16:44
demonleer
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:10
帖 子:483
专家分:1225
注 册:2012-6-4
收藏
得分:5 
temp = (char**)malloc(capacity*sizeof(char*))

temp是个二级指针,所以要加(char**),分配的内存里存放的都是char *类型,一般为4字节,数量为capacity。

当指针数组去理解。
2012-07-27 16:57
爱闹的娃
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:3
帖 子:265
专家分:975
注 册:2011-10-23
收藏
得分:5 
首先你要明白二级指针的作用,这里的temp = (char**)malloc(capacity*sizeof(char*))是对temp进行分配内存空间,然后你才能对temp[i]进行其他的操作.......
2012-07-27 17:03
jh275208498
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2012-7-19
收藏
得分:0 
谢谢两位大牛,这种用法真的有点蒙啊,一级还能理解,我当指针数组在去理解下谢谢啊
2012-07-27 17:58
快速回复:动态分配内存的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013200 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved