在C的malloc()和free()的正确用法 [英] Proper usage of malloc() and free() in c

查看:113
本文介绍了在C的malloc()和free()的正确用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C所以请原谅我,如果这是太明显了,但我有一个问题,发现在我的code中的错误是导致分割故障。我相信这个问题可能在malloc()函数的用法,但我不积极。

I'm new to C so forgive me if this is too obvious, but I am having an issue finding the error in my code that is leading to a segmentation fault. I believe the issue may be in the usage of malloc(), but I am not positive.

下面是code:

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

#define         MAX_STRING      20

char*   getFirstName    (char*  firstName
                        )
{
  char* myfirstName = (char*)malloc(strlen(firstName)+1);
  printf("Please enter your first name: ");
  fgets(firstName,MAX_STRING,stdin);
  return(myfirstName);
}


char*   getLastName     (char* lastName
                    )
{
  char* mylastName = (char*)malloc(strlen(lastName)+1);

  printf("Please enter your last name: ");
  fgets(lastName,MAX_STRING,stdin);
  return(mylastName);
}


char*   getNickName     (char*  nickName
                        )
{
  char* mynickName = (char*)malloc(strlen(nickName)+1);
  printf("Please enter your nick name: ");
  fgets(nickName,MAX_STRING,stdin);
  return(mynickName);
}


char*   getCompleteName (const char*    firstName,
                         const char*    lastName,
                         const char*    nickName,
                         char*          completeName
                        )
{
  snprintf(completeName,MAX_STRING,"%s \"%s\"    %s",firstName,nickName,lastName);
}


int     main    ()
{
  char*         firstName;
  char*         lastName;
  char*         nickName;
  char*         completeName;

  firstName     = getFirstName(firstName);
  lastName      = getLastName(lastName);
  nickName      = getNickName(nickName);

  completeName  = getCompleteName(firstName,lastName,nickName,completeName);
  printf("Hello %s.\n",completeName);
 free(firstName);
 free(lastName);
 free(nickName);
 return(EXIT_SUCCESS);
}

它看起来,我以正确的方式使用malloc()?

Does it seem that I am using malloc() in the correct way?

推荐答案

您已经写输入数据不使用它们的参数的函数(或者他们错误地使用它们)。因此,这是没有意义的声明它们,如:

The functions you have written to enter data do not use their parameters (or they use them incorrectly). Thus, it makes no sense to declare them like:

char*   getFirstName    (char*  firstName );

内的功能,存储器分配和返回指针到存储器

Within the functions, memory is allocated and a pointer to the memory is returned.

此外,这种说法:

char* myfirstName = (char*)malloc(strlen(firstName)+1);

是无效的。参数参数的firstName 未初始化,并且不指向任何字符串。

is invalid. The argument for parameter firstName was not initialized and does not point to any string.

或者你尝试分配内存和保存变量对应的地址 myfirstName

Or you try to allocate memory and save the corresponding address in variable myfirstName:

char* myfirstName = (char*)malloc(strlen(firstName)+1);

但随后尝试读取使用指针的firstName 数据:

fgets(firstName,MAX_STRING,stdin);

功能 getCompleteName 也无效。同样,有应该由 completeName 在您尝试来连接其他字符串指出没有分配的内存。而该函数返回什么都没有。

The function getCompleteName is also invalid. Again, there was no allocated memory that should be pointed to by completeName where you try to concatenate other strings. And the function returns nothing.

char*   getCompleteName (const char*    firstName,
                         const char*    lastName,
                         const char*    nickName,
                         char*          completeName
                        )
{
  snprintf(completeName,MAX_STRING,"%s \"%s\"    %s",firstName,nickName,lastName);
}

要考虑到功能与fgets 还包括目标数组中的新行字符。

Take into account that function fgets also includes in the target array the new line character.

因此​​,正确的功能,可以像下面的定义如下:

Thus, correct functions can look like the following definition below:

char*   getFirstName()
{
    char* myfirstName = ( char* )malloc( MAX_STRING );

    printf( "Please enter your first name: " );

    fgets( myfirstName, MAX_STRING, stdin );

    size_t n = strlen( myfirstName );

    if ( n != 0 && myfirstName[n-1] == '\n' ) myfirstName[n-1] = '\0';

    return myfirstName;
}

char*   getCompleteName (const char*    firstName,
                         const char*    lastName,
                         const char*    nickName,
                        )
{
    const char *format = "%s \"%s\"    %s";

    size_t n = strlen( firstName ) + strlen( lastName ) + 
               strlen( nickName ) + strlen( format );

    completeName = ( char * )malloc( n );

    snprintf( completeName, n, format, firstName,nickName,lastName);

    return completeName;
}

定义以类似的方式等功能。

Define other functions in similar ways.

这篇关于在C的malloc()和free()的正确用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆