简体   繁体   中英

Does a function's pointer to char needs memory allocation in C

Defining a function in c using this structures of data:

typedef struct {
   int number;
   char name[25];
} person;
person rep[100];
int key=0;

Is there any problem with memory allocation when defining this function: (i'm just a beginner: i don't know what memory allocation means)

void add_person(int num, char *name){
   if (key<100){
      rep[key].number = num;
      strcpy(rep[key].name, name);
      key++;
   }
}

There is no need for any explicit allocations in your code (as shown). The definition

person rep[100];

makes the compiler allocate space for 100 person structures. And with the member-field definition

char name[25];

each person structure have space for 25 characters in name (ie a string with up to 24 characters, plus the string terminator).

For your add_person, you don't need to worry about memory leaking. But you need to either add key as a global variable (next to rep) or define it as a static variable inside of add_person like this:

static int key = 0;

But you have an issue with this line

strcpy(rep[key].name, name);

If the input name to add_person is longer than 24 characters, it will result in strcpy writing outside of rep.name. At length 25, the name no longer is null-terminated, causing any function reading on that to read past it and (best case) into the next person (their number showing up as either garbage or another random character), or (worst case) outside of the rep array and your program segfaults.

You should check that name is shorter than 25.

The correct thing to do would to have char* name; in function, then do the following copy in add_person:

rep[key].name = malloc( strlen(name)+1 );
strcpy( rep[key].name, name );

In this case, you should make a function to free persons

free_person(struct person* p)
{
  free(p->name);
}

A function's pointer is something completely different than this.

What you have is a function with a char * ( char pointer). If you call this function, you pass it the contents for a "new" person struct.

This person struct contains a (rather small) array where the name can be held, so memory allocation (which means reserving a memory area for dedicated use) is implied there.

But be warned: what do you think will happen if the caller passes a string with more than 25 characters? Right, it will "destroy" the contents of the memory area behind it.

So you'll have to pay attention and use strncpy(rep[key].name, name, sizeof rep[key].name) . As the creator of strncpy() seems to have slept ("No null-character is implicitly appended at the end of destination if source is longer than num"), you'll have to add this null-character - which indicates the end of a string in C - by yourself:

rep[key].name[sizeof rep[key].name - 1] = '\0'

If the string was shorter, strcpy() has added its own '\\0' , if not, we cut it here.

your function definition is good

just a thing concerning the key variable: It should be defined as global variable

The line person rep[100]; reserves space for you to store 100 person objects, so no, there isn't a problem with memory allocation.

In real life, you would also need to be more careful that you didn't write a name longer than 24 characters into the name member.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM