0

I have a structure and a enum,

    struct __value
    {
       int a;
       enum xsd__boolean *ptr;
    } 
    enum xsd__boolean
    {
      __true = 1,
      __false = 0
    };

Is this the right way of doing a malloc to enum and assigning the value,I want to assign the value of __true or __false to *__StructPtr->ptr.Will the size of __true be same as that of int ?

struct __value *__StructPtr;
__StructPtr->ptr = (int *)malloc(sizeof(int)); 
*__StructPtr->ptr = __true;

Is this is the right way, please provide me some insight about this with some example.

Manu
  • 5,534
  • 6
  • 32
  • 42
  • 1. You need to allocate memory for `__value` first, as it's a pointer. 2. Why would you allocate the memory for the enum using `malloc`? Why not just `enum xsd__boolean sth;`? 3. Why so many `__`? – Kiril Kirov Feb 02 '13 at 09:58

3 Answers3

1
struct __value *__StructPtr;
__StructPtr->ptr = (int *)malloc(sizeof(int)); 
*__StructPtr->ptr = __true;

This is incorrect. Your __StructPtr pointer is not pointed at allocated memory, and thus assigning through the pointer leads to memory corruption.

The correct way is something like:

struct __value __StructPtr;
__StructPtr.ptr = malloc(sizeof(*__StructPtr.ptr)); 
*__StructPtr.ptr = __true;

In this case, you're allocating a struct __value on the stack, and then dynamically creating the memory pointed to by __StructPtr->ptr.

If you wanted to allocate __StructPtr and the memory __StructPtr->ptr points at both dynamically, try:

struct __value *__StructPtr;
__StructPtr = malloc(sizeof(*__StructPtr));
__StructPtr->ptr = malloc(sizeof(*__StructPtr->ptr)); 
*__StructPtr->ptr = __true;
sheu
  • 5,653
  • 17
  • 32
  • 2
    In C, there should be no casting of the result of malloc. It may hide compile-time detectable bugs (such as "forgot to include the right headerfile"). – Mats Petersson Feb 02 '13 at 10:03
  • 1
    [Do I cast the result of malloc?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) No you shouldn't. @MatsPetersson – Alvin Wong Feb 02 '13 at 10:05
  • @MatsPetersson, @AlvinWong: alright then, we'll do that. And clean up some of the `sizeof()`s as well. – sheu Feb 02 '13 at 10:06
0

First of all, when you have two pointers (struct __value *__StructPtr and enum xsd__boolean *ptr) you need to memory allocations.

To your question, as described here, the size of an enum is defined by the compiler so it's not given it has the size of an int, and you should instead allocate the memory of the size of the enum.

struct __value *__StructPtr = malloc(sizeof(struct __value));
__StructPtr->ptr = (int *)malloc(sizeof(enum xsd_boolean)); 
*__StructPtr->ptr = __true;

Also, you probably shouldn't be using type names starting with two underscores as they are reserved for the system or compiler according to the C standard.

I'm not sure why you'd want to have a pointer to the enum xsd__boolean, but if there is no good reason for it you might prefer not having the pointer at all and instead allocating the enum along with the struct. Then your code would look like this:

struct __value
{
   int a;
   enum xsd__boolean b;
}
...
struct __value *__StructPtr = malloc(sizeof(struct __value));
__StructPtr->ptr = __true;
Community
  • 1
  • 1
Antti
  • 11,944
  • 2
  • 24
  • 29
0

I struggle to know where to start... First of all, two underscores at the beginning of a symbol is reserved for the compiler/library implementation. If you are writing a compiler or C library, I expect you'd know how to allocate memory. So please remove the __ at the beginning of your names.

struct __value *__StructPtr;
__StructPtr->ptr = (int *)malloc(sizeof(int)); 
*__StructPtr->ptr = __true;

Now, let's pick this appart.

  1. You haven't allocated any memory for __StructPtr - so it points to somewhere unknown.
  2. Don't cast the result of malloc - it shouldn't be needed if you compile the code as C. If you are compiling it as C++, then use new - but you tagged this as C, so just remove the `(int *).
  3. You should use sizeof(enum xsd__boolean), as that's the name of your enum. int may be larger or smaller than the enum xsd_boolean.
  4. Why you allocate memory for a single boolean value in the first place is slightly beyond me - the pointer takes up AT LEAST as much space as a boolean. Don't use a pointer at all in this example.
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227