Highlighted
Contributor.
Contributor.
277 views

VuGen Script Error: Using malloc() to allocate memory to complex structure

Jump to solution

I am trying to allocate memory using malloc() to a structure that contain string and numbers. Below is an intercept of the relevant portion of the code. I am getting errors while compiling. Could someone pls help me?

Code (vuser_init.c):

typedef struct tagabc xyz;
struct tagabc {
   Char  Id[4];         
   long   Version;         
   long   Length;    
   long   Enc;     
   long   CCSId; 
   Char  Format[8]; 
   long   Flags; 
   long   ValueCCSId; 

 };

(Line 280): struct xyz *rfh1_ptr;

xyz rfh2 = {rfh2_default}; 

 

Code (Action.c)

Line 31: rfh1_ptr = (struct *)malloc(sizeof(xyz));

Error:

Error 31 missing struct tag Action.c 
Error 31 operands of = have illegal types `pointer to incomplete struct xyz defined at vuser_init.c: 280' and `pointer to incomplete struct defined at Action.c: 31' 
Error 31 Syntax error, unexpected '*', wrong token = * Action.c 

0 Likes
1 Solution

Accepted Solutions
Highlighted
Contributor.
Contributor.

Re: VuGen Script Error: Using malloc() to allocate memory to complex structure

Jump to solution

Thanks @JHF Remmelzwaal for your response. I am aware that malloc() doesn't initialize the memory, hence I have explicit memset implemented that initializes the memory. Thanks for pointing it though. And as a good coding practice, I have explicitly included globals header file, though LR looks for all header files in its standard installation directory.

View solution in original post

0 Likes
3 Replies
Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..

Re: VuGen Script Error: Using malloc() to allocate memory to complex structure

Jump to solution

@BSM 

Did you added an #include in globals.h for malloc() (Not strictly necessary, but a good habit).

Malloc() returns a (void*), i.e. void pointer. So your code should look like:

rfh1_ptr = (xyz *)malloc(sizeof(xyz));

because of your typedef.

P.S. do you know that the memory returned my malloc is not initialized (contains garbage). You might like to use the calloc() function instead (prototype):

void *calloc( size_t num_elems, size_t elem_size);

So in your case:

rfh1_ptr = (xyz *)calloc(1, sizeof(xyz));

Signature:
Reward community members who take time to respond and help.
0 Likes
Highlighted
Contributor.
Contributor.

Re: VuGen Script Error: Using malloc() to allocate memory to complex structure

Jump to solution

Thanks @JHF Remmelzwaal for your response. I am aware that malloc() doesn't initialize the memory, hence I have explicit memset implemented that initializes the memory. Thanks for pointing it though. And as a good coding practice, I have explicitly included globals header file, though LR looks for all header files in its standard installation directory.

View solution in original post

0 Likes
Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..

Re: VuGen Script Error: Using malloc() to allocate memory to complex structure

Jump to solution

I like to make a comment on your statement: "I have explicitly included globals header file, though LR looks for all header files in its standard installation directory."
Your interpretation is not what I intended. LR put indeed all source files into one large file in your script directory. Sometimes it is interested to look into this file 'pre_cci.c' when you do not understand an error or warning.

I intended with my remark "Did you added an #include in globals.h for malloc() (Not strictly necessary, but a good habit)." That you put into the file 'globals.h' at the bottom of the '// Include Files' section lines like:

// Include Files
#include "lrun.h"
.... 
#include <string.h>    // for strcmp(), memset()
#include <stdlib.h>    // for malloc() 

It is important to have your prototypes etc correctly defined. The compiler can do a better job for you. As you know each unknown function is expected to return an int value, this might lead to nasty bugs.

 

Signature:
Reward community members who take time to respond and help.
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.