0

We have pointers inside a struct, do we need to initialize the pointers inside the struct?

I have tried the code below, include this sentence below or not, the code all run well. (Could some experts give help here? and I read some code, and found seems sometimes initialized, sometimes not, so confused and search/ask here.)

The answer in this link seems not mentioned initialize this pointer inside struct. initializing-a-member-of-a-structure-with-pointers-c

#include "stdio.h"
#include "stdlib.h"

struct part{
    int num;
    char *name;
};

int main()
{
    struct part *p = (struct part*)malloc(sizeof(struct part));

    //Include this or not, this code all run well
    p->name = (char*)malloc(sizeof(char)); 

    p->num = 1;
    p->name = "ss";

    printf("%d, %s\n", p->num, p->name);

    return 0;
}



Weather Vane
  • 33,872
  • 7
  • 36
  • 56
Kumar
  • 73
  • 7
  • 2
    If you ever dereference the pointers, you must ensure they are initialized before you do so. If you never dereference them, why is the pointer in the structure. Using an uninitialized pointer is a major problem — avoid it. – Jonathan Leffler Mar 19 '22 at 13:38
  • 2
    You are causing a (small) memory leak, by allocating one byte, and then overwriting the pointer obtained, with another pointer to a string literal. – Weather Vane Mar 19 '22 at 13:38
  • 2
    Remember: `p->name = "ss";` assigns a pointer — it is not a string copy! – Jonathan Leffler Mar 19 '22 at 13:38
  • @WeatherVane Thanks for mentioning, yeah, I need to allocate more bytes – Kumar Mar 19 '22 at 13:47
  • 2
    No, you'll leak even more. See what Jonathan wrote. Oh, please don't modify the code on-the-fly. The site isn't a "rolling tutorial". – Weather Vane Mar 19 '22 at 13:47
  • 2
    You must use `strcpy()` or a similar function to copy strings in C. – Jonathan Leffler Mar 19 '22 at 13:48
  • Got it, strcpy(p->name, "ss"); BTW, do I still need malloc p->name before using strcpy? – Kumar Mar 19 '22 at 13:50
  • Yes, and in *this* case, you must allocate enough bytes: `strlen("ss") + 1` – Weather Vane Mar 19 '22 at 13:51
  • Does [this](https://stackoverflow.com/a/62861399/775806) answer your question? – n. m. could be an AI Mar 19 '22 at 14:03
  • yeah, this helps, maybe I misleading here, i need to use int* here to ask the core question want to ask, that, if need to malloc pointer inside struct before using, or malloc the struct itself is enough, now the answer seems we need to malloc the pointer insides the struct besides malloc the struct itself. – Kumar Mar 19 '22 at 14:18

1 Answers1

1

No, you should not do that, you are creating a memory leak, because you allocate memory and then forget the pointer value and don't free it.

To remove the memory leak:

p->name = malloc(1); //sizeof char is 1 by definition, and cast not needed
free(p->name);
p->name = "ss";

However, if you look at that, it should be clear that allocating 1 byte of memory, then immediately freeing it, is pointless.


Perhaps you want a copy of the string?

const char *initdata = "ss";
p->name = malloc(strlen(initdata)+1);
strcpy(p->name, initdata);
// Remember to free this at some point

Alternatively, you could use initialize with the string literal, but then you should have const char pointer because string literals are read-only:

struct part{
    int num;
    const char *name;
};
hyde
  • 60,639
  • 21
  • 115
  • 176
  • Thanks, So directly p->name = "ss"; or the malloc+strcpy are all fine in this case? (if not string literal, say int* inside struct, we must malloc before using, right?) – Kumar Mar 19 '22 at 14:14
  • 1
    @Kumar If you want a pointer to original/source string, assign pointer. If you want a copy, use malloc+strcpy. Which, depends on the rest of the code, so usually only one is corredt/good choice. – hyde Mar 19 '22 at 14:18