473,320 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Allocating memory to nested Structures within a Structure

Dear Friends

I have the follwoing function "tss_fe_get_own_info" which has the
arguments as shows below
tss_fe_get_own_info(char *user_id, tss_user_profile_t
**p_buf_UserSecInfo, error_status_t *retStatus)

// Structure tss_user_profile_t from the function

typedef struct {
tss_user_info_t user_info;
odss_attribute_value_list_t *ProcAttr;
odss_attribute_value_list_t *BkOffAttr;
odss_attribute_value_list_t *WkClusterAttr;
odss_attribute_value_list_t *CSUAttr;
} tss_user_profile_t;

// Structure tss_user_info_t
typedef struct
{
char user_uuid[USER_UUID_LEN+1];
char user_sec_info_status[USER_ACC_STATUS_LEN+1];
char user_id[USER_ID_LEN+1];
char user_name[USER_NAME_LEN+1];
char employee_id[EMPLOYEE_ID_LEN+1];
char job_title[JOB_TITLE_LEN+1];
char job_type[JOB_TYPE_LEN+1];
char loc_addr[LOC_ADDR_LEN+1];
char loc_country_code[LOC_CTRY_CODE_LEN+1];
char loc_city_code[LOC_CITY_CODE_LEN+1];
char telephone_number[TELE_NUM_LEN+1];
char expense_code[EXPENSE_CODE_LEN+1];
char proc_locn_csu[PROC_LOCN_CSU_LEN+1];
char proc_locn_rpf[RPF_CODE_LEN+1];
char prim_wk_cluster[PRM_WK_CLUS_LEN+1];
char experience[EXPERIENCE_LEN+1];
char oracle_id[ORACLE_ID_LEN+1];
char oracle_pwd[ORACLE_PWD_LEN+1];
char user_group[USER_GROUP_LEN+1];
char date_modified[DATE_STR_LEN+1];
char date_valid[DATE_STR_LEN+1];
} tss_user_info_t;

// odss_attribute_value_list_t
typedef struct odss_attribute_value_list_t
{
[ptr] odss_attribute_value_t *attribute_value;
[ptr] struct odss_attribute_value_list_t *next;
} odss_attribute_value_list_t;

// Structure odss_attribute_value_t

typedef struct
{
unsigned32 length;
[string, ptr] char *attribute;
} odss_attribute_value_t;

If I want to allocate the memory to nested structures in that case
I have done the follwoing
tss_user_profile_t **p_buf_UserSecInfo;
*p_buf_UserSecInfo = new tss_user_profile_t;
memset((void*) *p_buf_UserSecInfo, '\0',
sizeof(tss_user_profile_t)); // at this point the profile is
allocated, as well as the user_info

unsigned32 len1 = 0;
char *attr1 = NULL;

odss_attribute_value_list_t *tmp1 = (*p_buf_UserSecInfo)->ProcAttr; //
Allocates the ProcAttr pointer :

ProcAttr = new odss_attribute_value_list_t;
ProcAttr->attribute_value = new odss_attribute_value_t;
ProcAttr->attribute_value->length = len1;
ProcAttr->attribute_value->attribute = attr1;
ProcAttr->next = tmp1;

unsigned32 len2 = 0;
char *attr2 = NULL;

odss_attribute_value_list_t *tmp2 = (*p_buf_UserSecInfo)-
>BkOffAttr; // Allocates the BkOffAttr pointer :
BkOffAttr = new odss_attribute_value_list_t;
BkOffAttr->attribute_value = new odss_attribute_value_t;
BkOffAttr->attribute_value->length = len2;
BkOffAttr->attribute_value->attribute = attr2;
BkOffAttr->next = tmp2;

unsigned32 len3 = 0;
char *attr3 = NULL;

odss_attribute_value_list_t *tmp3 = (*p_buf_UserSecInfo)-
>WkClusterAttr; // Allocates the WkClusterAttr pointer :
WkClusterAttr = new odss_attribute_value_list_t;
WkClusterAttr->attribute_value = new odss_attribute_value_t;
WkClusterAttr->attribute_value->length = len3;
WkClusterAttr->attribute_value->attribute = attr3;
WkClusterAttr->next = tmp3;

unsigned32 len4 = 0;
char *attr4 = NULL;

odss_attribute_value_list_t *tmp4 = (*p_buf_UserSecInfo)->CSUAttr; //
Allocates the CSUAttr pointer :

CSUAttr = new odss_attribute_value_list_t;
CSUAttr->attribute_value = new odss_attribute_value_t;
CSUAttr->attribute_value->length = len4;
CSUAttr->attribute_value->attribute = attr4;
CSUAttr->next = tmp4;

Please let me know if this is the correct way to allocate the memory
thanks
Amit

May 25 '07 #1
3 6646
Hi,
Dear Friends

I have the follwoing function "tss_fe_get_own_info" which has the
arguments as shows below
tss_fe_get_own_info(char *user_id, tss_user_profile_t
**p_buf_UserSecInfo, error_status_t *retStatus)

// Structure tss_user_profile_t from the function
tss_user_profile_t **p_buf_UserSecInfo;
*p_buf_UserSecInfo = new tss_user_profile_t;
memset((void*) *p_buf_UserSecInfo, '\0',
Here you got a pointer to a pointer. If the first thing you do after
declaration is dereferencing the pointer like this:
*p_buf_UserSecInfo, I think you are running into trouble, because
p_buf_UserSecInfo is not initialized and is pointing nowhere (or
anywhere) in memory. Dereferencing it like you do has no sense... Who
says pointer says array. If you got a pointer of pointer like this
it's because you want a array of pointers. You'd better do something
like:

tss_user_profile_t **p_buf_UserSecInfo;
p_buf_UserSecInfo = new *tss_user_profile_t[SOME_INTERGER_VALUE]; //
With SOME_INTERGER_VALUE possibly equal to 1
for (unsigned int i = 0; i < SOME_INTERGER_VALUE; ++i)
{
p_buf_UserSecInfo[i] = new tss_user_profile_t;
memset((void*) p_buf_UserSecInfo[i], 0, sizeof(tss_user_profile_t));
}
>
unsigned32 len1 = 0;
char *attr1 = NULL;

odss_attribute_value_list_t *tmp1 = (*p_buf_UserSecInfo)->ProcAttr;
Again, I don't get what you do. (*p_buf_UserSecInfo)->ProcAttr is NULL
here (remember the memset), so tmp1 is NULL too.
>
ProcAttr = new odss_attribute_value_list_t;
ProcAttr is not declared anywhere, so it should not compile.
ProcAttr->attribute_value = new odss_attribute_value_t;
ProcAttr->attribute_value->length = len1;
ProcAttr->attribute_value->attribute = attr1;
attr1 is NULL anyway so you'better write
ProcAttr->attribute_value->attribute = NULL;
ProcAttr->next = tmp1;
Here too:
ProcAttr->next = NULL;

I guess, what do you want to do is:

(*p_buf_UserSecInfo)->ProcAttr = new odss_attribute_value_list_t;
(*p_buf_UserSecInfo)->ProcAttr->attribute_value = new
odss_attribute_value_t;
(*p_buf_UserSecInfo)->ProcAttr->attribute_value->length = 0;
(*p_buf_UserSecInfo)->ProcAttr->attribute_value->attribute = NULL;
(*p_buf_UserSecInfo)->ProcAttr->next = NULL;
>
Please let me know if this is the correct way to allocate the memory
Or maybe I just didn't understand your post at all :)

JD

May 25 '07 #2
On May 25, 3:59 pm, jean.daniel.mich...@gmail.com wrote:
Hi,
Dear Friends
I have the follwoing function "tss_fe_get_own_info" which has the
arguments as shows below
tss_fe_get_own_info(char *user_id, tss_user_profile_t
**p_buf_UserSecInfo, error_status_t *retStatus)
// Structure tss_user_profile_t from the function
tss_user_profile_t **p_buf_UserSecInfo;
*p_buf_UserSecInfo = new tss_user_profile_t;
memset((void*) *p_buf_UserSecInfo, '\0',

Here you got a pointer to a pointer. If the first thing you do after
declaration is dereferencing the pointer like this:
*p_buf_UserSecInfo, I think you are running into trouble, because
p_buf_UserSecInfo is not initialized and is pointing nowhere (or
anywhere) in memory. Dereferencing it like you do has no sense... Who
says pointer says array. If you got a pointer of pointer like this
it's because you want a array of pointers. You'd better do something
like:

tss_user_profile_t **p_buf_UserSecInfo;
p_buf_UserSecInfo = new *tss_user_profile_t[SOME_INTERGER_VALUE]; //
With SOME_INTERGER_VALUE possibly equal to 1
for (unsigned int i = 0; i < SOME_INTERGER_VALUE; ++i)
{
p_buf_UserSecInfo[i] = new tss_user_profile_t;
memset((void*) p_buf_UserSecInfo[i], 0, sizeof(tss_user_profile_t));

}
unsigned32 len1 = 0;
char *attr1 = NULL;
odss_attribute_value_list_t *tmp1 = (*p_buf_UserSecInfo)->ProcAttr;

Again, I don't get what you do. (*p_buf_UserSecInfo)->ProcAttr is NULL
here (remember the memset), so tmp1 is NULL too.
ProcAttr = new odss_attribute_value_list_t;

ProcAttr is not declared anywhere, so it should not compile.
ProcAttr->attribute_value = new odss_attribute_value_t;
ProcAttr->attribute_value->length = len1;
ProcAttr->attribute_value->attribute = attr1;

attr1 is NULL anyway so you'better write
ProcAttr->attribute_value->attribute = NULL;
ProcAttr->next = tmp1;

Here too:
ProcAttr->next = NULL;

I guess, what do you want to do is:

(*p_buf_UserSecInfo)->ProcAttr = new odss_attribute_value_list_t;
(*p_buf_UserSecInfo)->ProcAttr->attribute_value = new
odss_attribute_value_t;
(*p_buf_UserSecInfo)->ProcAttr->attribute_value->length = 0;
(*p_buf_UserSecInfo)->ProcAttr->attribute_value->attribute = NULL;
(*p_buf_UserSecInfo)->ProcAttr->next = NULL;
Please let me know if this is the correct way to allocate the memory

Or maybe I just didn't understand your post at all :)

JD
Hi JD thanks for your comments.
here is the background of what Im doiong This is SOAP Interface that
Im writing and method tss_fe_get_own_info calls to DCE ( Distributed
Computing Environment ) and DCE internally calls Oracle DB using
pro*c. Now in the function
char *user_id = IN parameter which is passed to SOAP from Front End
( J2EE)
and tss_user_profile_t **p_buf_UserSecInfo , error_status_t
*retStatus are OUT parameters sent from DCE to Frront End via SOAP.
I have to stored the return object from DCE to the structure
tss_user_profile_t
Since its a linked list type , I have to allocate memory to
tss_user_profile_t structure dynamically using new operator so that I
can store the return object. Structure tss_user_profile_t has nested
structures with pointers , things got complicated.
This is what I have now and its compiling
tss_user_profile_t **p_buf_UserSecInfo;
*p_buf_UserSecInfo = new tss_user_profile_t[1];
for (unsigned int i = 0; i < 1; ++i)
{
p_buf_UserSecInfo[i] = new tss_user_profile_t;
memset((void*)p_buf_UserSecInfo[i],0,
sizeof(tss_user_profile_t));

}

(*p_buf_UserSecInfo)->ProcAttr = new odss_attribute_value_list_t;
(*p_buf_UserSecInfo)->ProcAttr->attribute_value = new
odss_attribute_value_t;
(*p_buf_UserSecInfo)->ProcAttr->attribute_value->length = 0;
(*p_buf_UserSecInfo)->ProcAttr->attribute_value->attribute = NULL;
(*p_buf_UserSecInfo)->ProcAttr->next = NULL;

Now If I want to free the allocated memory then do I need to write
delete for all allocated structures ?
How it could be done
Please let me know
Thanks for your comments
Amit

May 25 '07 #3
Hi,

[snip]
This is what I have now and its compiling
tss_user_profile_t **p_buf_UserSecInfo;
*p_buf_UserSecInfo = new tss_user_profile_t[1];
for (unsigned int i = 0; i < 1; ++i)
{
p_buf_UserSecInfo[i] = new tss_user_profile_t;
memset((void*)p_buf_UserSecInfo[i],0,
sizeof(tss_user_profile_t));

}
If you are just allocating one element, you don't need a for loop...

tss_user_profile_t **p_buf_UserSecInfo;
*p_buf_UserSecInfo = new tss_user_profile_t[1];
p_buf_UserSecInfo[0] = new tss_user_profile_t;
memset((void*)p_buf_UserSecInfo[0], 0, sizeof(tss_user_profile_t));

(*p_buf_UserSecInfo)->ProcAttr = new odss_attribute_value_list_t;
(*p_buf_UserSecInfo)->ProcAttr->attribute_value = new
odss_attribute_value_t;
(*p_buf_UserSecInfo)->ProcAttr->attribute_value->length = 0;
(*p_buf_UserSecInfo)->ProcAttr->attribute_value->attribute = NULL;
(*p_buf_UserSecInfo)->ProcAttr->next = NULL;

Now If I want to free the allocated memory then do I need to write
delete for all allocated structures ?
How it could be done
Well, yes, for every new, you shall have a delete, otherwise you
would end up with memory leaks. And to delete a linked list a while
loop seems appropriate...

odss_attribute_value_list_t *list = (*p_buf_UserSecInfo)->ProcAttr;
while (list)
{
odss_attribute_value_list_t *tmp = list->next;
delete list->attribute_value;
delete list;
list = tmp;
}

Here you go.
I would advise you to have a look to some C tutorial before playing
around with pointers and dynamic memory allocation:
http://einstein.drexel.edu/courses/C....html#pointers

JD
May 26 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: fix | last post by:
Hi all, I am writing a program using some structs, it is not running and I believe it is because there's some memory leak - the debugger tells me that the code causes the problem is in the malloc...
5
by: mikegw | last post by:
Hello all. I am currently using an implementation of sysV shared memory. The entire shared memory is allocated is one continuous block of which I get the pointer to the head, everything should...
9
by: Alfonso Morra | last post by:
Hi, I am having some probs with copying memory blocks around (part of a messaging library) and I would like some confirmation to make sure that I'm going about things the right way. I have...
9
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers,...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
1
by: 4Ankit | last post by:
Hey guys i am having some trouble with nesting one selection structure within another selection structure. At the moment i am unclear what selection structures are and just need a simple example of...
3
by: ranjeetasharma81 | last post by:
Hi all, I have a big C-cod, in which there are lots of dynamic memory allocation used. I want to replace dynamic memroy allocation by static arrays. The following are the problems that i am...
9
by: uday | last post by:
Hi All, I need one clarification regarding memory allocation for structure. The details are as given below : let us consider one structure struct { uit32 len0; uint8 *pointer0; uit32 len1;
11
by: vivek | last post by:
Hello, I have a pointer to a main structure which again consists of structures, enums, char, int, float and again complex structures. When i free all the contents of the main structure, it...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.