0

For example I have a struct

struct s{
    char c;
    int x;
};

And I use calloc() to allocate memory.

s *sp = (s*) calloc(1, sizeof(s));

Now, what will be the values of sp->c and sp->x?

platinoob_
  • 151
  • 1
  • 11
  • 2
    `calloc()` initializes the memory to zeroes. `sp->c` and `sp->x` will be `0` (of type `char` and `int` respectively). BTW: casting the return value of `calloc()` is, at best, redundant (and may hide an error the compiler would warn you about if the cast wasn't there). – pmg May 06 '20 at 09:45
  • To answer the question in the title: chars are stored as numbers. Or did you mean the structure, not the char? The whole structure will be zeroed: all of its members, and any space left as padding. – Rup May 06 '20 at 09:46
  • If you do not want C++, add `#ifdef __cplusplus` / `#error bad compiler` / `#endif` at the top of your source file(s). – pmg May 06 '20 at 09:50

1 Answers1

2

"What will be the values of sp->c and sp->x?"

Since calloc() sets all bits of the allocated memory to 0, c and x will have the value of 0 if the 0 value representation of int and char is of all bits to 0 (which is common).

Note that in the case of pointers, the pointer might not be standard-compliant NULL pointer when just setting all bits to 0 as the C standard does not require the representation of NULL pointers to be all-zero-bits.


Side notes:

1.

struct s{
    char c;
    int x;
};

s *sp = (s*) calloc(1, sizeof(s));

can´t work as s isn´t a typedefd type; it is a structure tag. Therefore, You need to precede s by the struct keyword:

struct s *sp = (struct s*) calloc(1, sizeof(struct s));

2.

You do not need to cast the returned pointer from calloc() and other memory management functions and rather avoid it since it can add clutter to your code. -> Do I cast the result of malloc

So, just do:

struct s *sp = calloc(1, sizeof(struct s));
  • What if instead of `struct` I have `class`? And on my compiler at least this does not work, I have to do the cast, it says it cannot initialize a variable of type [type]* with a value of type void* – platinoob_ May 06 '20 at 11:03
  • @platinoob_ First of all, classes belong to C++, not C. C and C++ are very different languages. C code doesn´t have to work with a C++ compiler and vice versa. This question was tagged with C and also the provided code is C code. I answered the question you had for C. If you have an issue in your specific C++ code, please ask a separate question and tag it with C++, not C. This doesn´t belong to here. It also requires much more information than to just show a little bit of information in this comment section. We need to see the exact code and exact compiler invocation command. – RobertS supports Monica Cellio May 06 '20 at 11:38
  • @ RobertS supports Monica Cellio, I am sorry if my comment was not clear, the "class instead of struct" thing was just a question, the comment was about me having to do the cast or else : `cannot initialize a variable of type 's *' with an rvalue of type 'void *' s *sp = calloc(1, sizeof(struct s));` – platinoob_ May 06 '20 at 18:17
  • @platinoob_ The problem is: I can´t reproduce the problem. It is really better to make a separate question. – RobertS supports Monica Cellio May 06 '20 at 18:21