1

I have done some research on allocating memory for a struct already on stackoverflow and the answers that i found did not work for me. With that said, here is my question and problem. For the most part a majority of them had the same bit of code that i currently have. Although my compiler is giving me an error.

Problem: I would like to allocate the memory for a Struct(an array of structs) and not a Pointer(pointer to a struct). Below is the definition of my Struct.

 #define INIT_MEMO_SIZE

typedef struct Memo
{
   struct HugeInteger *F; 
   int length; 

}Memo; 

Required Solution: Within memo struct i would like to dynamically allocate an array of INIT_MEMO_SIZE number of huge integer structs

Attempted Solution: below right at "malloc" I am getting an error stating "Error: An entity of type 'void *' cannot be used to initialize an entity of type 'HugeInteger *' this underlines the keyword "malloc" in red. Compiler being used is Microsoft Visual Studio 2012

// Creates and initializes a Memo Struct
Memo *createMemo(void)
{ 
    HugeInteger *ptr = malloc(sizeof(HugeInteger) * INIT_MEMO_SIZE); 

    if(ptr == NULL)
        return NULL; 

} // End of *createMemo

Question: What am i doing wrong or what is the proper way to allocate the array of structs?

Rex Charles
  • 1,292
  • 1
  • 16
  • 33

2 Answers2

2

malloc returns a void*. You need to cast the returned value.

 HugeInteger *ptr = (HugeInteger*)malloc(sizeof(HugeInteger) * INIT_MEMO_SIZE); 
Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

Your comment says that this code "creates and initializes a Memo struct" however, I see no place where it either creates or initializes a Memo struct. In fact, the function doesn't even return such a struct (unless memory allocation failed). Perhaps you want something like this:

Memo *createMemo(void)
{ 
    Memo *memo = malloc(sizeof(Memo));

    if(memo == NULL)
        return NULL;

    memo->F = malloc(sizeof(struct HugeInteger) * INIT_MEMO_SIZE); 

    if(memo->F == NULL)
    { // Oops, memory allocation failed.
        free(memo);
        return NULL;
    }

    // other initialization here -- perhaps set memo->length 
    // to some sensible value
    memo->length = INIT_MEMO_SIZE;

    // and return the new memo
    return memo;
} // End of *createMemo

Generally speaking, you should not need to typecast the result of a malloc operation in C. Indeed, doing so can often cause subtle issues that can be difficult to diagnose. So, frankly, if the compiler is complaining, this would suggest that you are compiling as C++.

However, you say that not casting results in malloc being underlined in red and having some kind of "error" associated with it. This suggests to me what what you are seeing is not a compiler error, but a warning from an overzealous editor (via Intellisense, perhaps in combination with the SDL checks that VS2012 implements) that's trying to preemptively warn you about what it perceives to be an issue before you even compile.

Nik Bougalis
  • 10,495
  • 1
  • 21
  • 37