free和malloc

RichAndMerry 2018-06-11 02:42:11
功能没有问题了,可是卡在最后free(*(words + i));这里,运行到这里卡着不出来。。。
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define Length 20

int _tmain(int argc, _TCHAR* argv[])
{
size_t n_words = 0; //输入单词个数;
char * temp = (char*)malloc(50 * sizeof(char));//临时存放单词的;
size_t n_row = 0; //一个单词的长度;
printf("How many words do you want to enter?\n");
scanf_s("%d", &n_words);
printf("Enter %d words now:\n", n_words);
char **words = (char**)malloc(n_words * sizeof(char*));//存放输入单词的;
for (size_t i = 0; i < n_words; i++)
{
n_row = 0;
scanf_s("%s", temp, 20);
n_row = strlen(temp);
*(words + i ) = (char*)malloc(n_row * sizeof(char));
size_t j = 0;
for (; j < n_row; j++)
{
*(*(words + i) + j) = *(temp + j);
}
*(*(words + i) + j + 1) = '\0';
getchar();
}
printf("Here are your words:\n");
for (size_t i = 0; i < n_words; i++)
{
printf("%s", *(words + i));
printf("\n");
}

for (size_t i = 0; i < n_words; i++)
{
free(*(words + i));
}
free(words);
printf("GoodBye!");
system("PAUSE");
return 0;
}

...全文
770 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
RichAndMerry 2018-06-12
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define Length 20

int _tmain(int argc, _TCHAR* argv[])
{
	size_t n_words = 0; //输入单词个数;
	char * temp = (char*)malloc(50 * sizeof(char));//临时存放单词的;
	size_t n_row = 0;  //一个单词的长度;
	printf("How many words do you want to enter?\n");
	scanf_s("%d", &n_words);
	printf("Enter %d words now:\n", n_words);
	char **words = (char**)malloc(n_words * sizeof(char*));//存放输入单词的;
	for (size_t i = 0; i < n_words; i++)
	{
		n_row = 0;
		scanf_s("%s", temp, 20);
		n_row = strlen(temp);
		*(words + i ) = (char*)malloc(n_row * sizeof(char) + 1);
		size_t j = 0;
		for (; j < n_row; j++)
		{
			*(*(words + i) + j) = *(temp + j);
		}
		*(*(words + i) + j) = '\0';
		getchar();
	}
	printf("Here are your words:\n");
	for (size_t i = 0; i < n_words; i++)
	{
		printf("%s\n", *(words + i));
	}
	for (size_t i = 0; i < n_words; i++)
	{
		free(*(words + i));
	}
	free(words);
	printf("\nGoodBye!");
	system("PAUSE");
	return 0;
}
自信男孩 2018-06-11
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define Length 20

int _tmain(int argc, _TCHAR* argv[])
{
    size_t n_words = 0; //输入单词个数;
    char * temp = (char*)malloc(50 * sizeof(char));//临时存放单词的;
    size_t n_row = 0;  //一个单词的长度;
    printf("How many words do you want to enter?\n");
    scanf_s("%d", &n_words);
    printf("Enter %d words now:\n", n_words);
    char **words = (char**)malloc(n_words * sizeof(char*));//存放输入单词的;
    for (size_t i = 0; i < n_words; i++)
    {
        n_row = 0;
        scanf_s("%s", temp, 20);
        n_row = strlen(temp);
        //*(words + i ) = (char*)malloc(n_row * sizeof(char));
        words[i] = (char*)malloc(n_row * sizeof(char) + 1);
        size_t j = 0;
        for (; j < n_row; j++)
        {
            words[i][j] = temp[j];
            //*(*(words + i) + j) = *(temp + j);
        }
        //*(*(words + i) + j + 1) = '\0';
        words[i][j] = '\0';
        getchar();
    }
    printf("Here are your words:\n");
    for (size_t i = 0; i < n_words; i++)
    {
        //printf("%s", *(words + i));
        printf("%s", words[i]);
        printf("\n");
    }

    for (size_t i = 0; i < n_words; i++)
    {
        free(*(words + i));
    }
    free(words);
    printf("GoodBye!");
    system("PAUSE");
    return 0;
}
代码有问题,建议对比参考一下;
//*(words + i ) = (char*)malloc(n_row * sizeof(char));
        words[i] = (char*)malloc(n_row * sizeof(char) + 1);
申请空间时应该多申请一个字节,这个字节用于存放'\0'; 若不多申请一个字节,下面的操作就会越界;
//*(*(words + i) + j + 1) = '\0';
        words[i][j] = '\0';
另外,不需要再+1,因为上面的循环退出条件是j < n_row;那么j == n_row就会跳出循环。所以不需要再+1; 最后一点:
*(*(words + i) + j) = *(temp + j);
//用下面的替换:
words[i][j] = temp[j];
这样更好理解一些
赵4老师 2018-06-11
  • 打赏
  • 举报
回复
仅供参考:
//将c:\\tmp文件夹下的所有文件的内容全部放到用malloc分配的内存中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
struct FB {
    char fn[256];
    size_t fl;
    char *b;
    struct FB *next;
    struct FB *prev;
} *fh,*fb,*ft;
char ln[256];
char fpn[256];
FILE *af;
FILE *f;
int L,n;
int main() {
    system("dir /b /a-d c:\\tmp\\*.* >c:\\allfn.txt");
    af=fopen("c:\\allfn.txt","r");
    if (NULL==af) {
        printf("Can not open file c:\\allfn.txt!\n");
        return 1;
    }
    fh=NULL;
    fb=NULL;
    n=0;
    while (1) {
        if (NULL==fgets(ln,256,af)) break;
        L=strlen(ln);
        if ('\n'==ln[L-1]) ln[L-1]=0;
        printf("read %s\n",ln);
        strcpy(fpn,"c:\\tmp\\");
        strcat(fpn,ln);
        ft=(struct FB *)malloc(sizeof(struct FB));
        if (NULL==ft) {
            printf("Can not malloc ft!\n");
            fclose(af);
            return 2;//之前的malloc在main退出后由操作系统自动free
        }
        printf("ft[%d]==%p\n",n,ft);
        strcpy(ft->fn,fpn);
        f=fopen(fpn,"rb");
        if (NULL==f) {
            printf("Can not open file %s!\n",fpn);
            fclose(af);
            return 3;//之前的malloc在main退出后由操作系统自动free
        }
        ft->fl=_filelength(fileno(f));
        ft->b=malloc(ft->fl);
        if (NULL==ft->b) {
            printf("Can not malloc ft->b!\n");
            fclose(f);
            fclose(af);
            return 4;//之前的malloc在main退出后由操作系统自动free
        }
        printf("ft[%d]->b==%p\n",n,ft->b);
        if (ft->fl!=fread(ft->b,1,ft->fl,f)) {
            printf("fread error!\n");
            fclose(f);
            fclose(af);
            return 5;//之前的malloc在main退出后由操作系统自动free
        }
        fclose(f);
        ft->next=NULL;

        if (NULL==fh) {
            ft->prev=NULL;
            fh=ft;
        } else {
            fb->next=ft;
            ft->prev=fb;
        }
        fb=ft;
        n++;
    }
    fclose(af);
    printf("-----list-----\n");
    for (ft=fh;NULL!=ft;ft=ft->next) {
        printf("%8d %s\n",ft->fl,ft->fn);
        if (NULL!=ft) fb=ft;
    }
    printf("-----free-----\n");
    n--;
    if (NULL!=fh) {
        for (ft=fb->prev;NULL!=ft;ft=ft->prev) {
            if (NULL!=ft->next->b) {
                printf("ft[%d]->b==%p\n",n,ft->next->b);
                free(ft->next->b);
            }
            if (NULL!=ft->next) {
                printf("ft[%d]==%p\n",n,ft->next);
                free(ft->next);
            }
            n--;
        }
        if (NULL!=fh->b) {
            printf("ft[0]->b==%p\n",fh->b);
            free(fh->b);
        }
        printf("ft[0]==%p\n",fh);
        free(fh);
    }
    return 0;
}
//C:\tmp\tmp\Debug>dir /a-d c:\tmp
// 驱动器 C 中的卷是 C_HD5_1
// 卷的序列号是 1817-D526
//
// c:\tmp 的目录
//
//找不到文件
//
//C:\tmp\tmp\Debug>tmp
//找不到文件
//-----list-----
//-----free-----
//
//C:\tmp\tmp\Debug>dir /a-d c:\tmp
// 驱动器 C 中的卷是 C_HD5_1
// 卷的序列号是 1817-D526
//
// c:\tmp 的目录
//
//2011-06-30  18:04            44,840 my_c.rar
//2011-06-30  17:18             1,036 err.frm
//2011-06-30  14:32            14,243 出租.txt
//2011-06-28  12:08            23,681 MSDN98书签.txt
//             4 个文件         83,800 字节
//             0 个目录 17,041,870,848 可用字节
//
//C:\tmp\tmp\Debug>tmp
//read my_c.rar
//ft[0]==00421800
//ft[0]->b==00520068
//read err.frm
//ft[1]==00421670
//ft[1]->b==0052AFC0
//read 出租.txt
//ft[2]==00421530
//ft[2]->b==00378F28
//read MSDN98书签.txt
//ft[3]==004213F0
//ft[3]->b==0052B3F8
//-----list-----
// 44840 c:\tmp\my_c.rar
//  1036 c:\tmp\err.frm
// 14243 c:\tmp\出租.txt
// 23681 c:\tmp\MSDN98书签.txt
//-----free-----
//ft[3]->b==0052B3F8
//ft[3]==004213F0
//ft[2]->b==00378F28
//ft[2]==00421530
//ft[1]->b==0052AFC0
//ft[1]==00421670
//ft[0]->b==00520068
//ft[0]==00421800
//
//C:\tmp\tmp\Debug>

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧