473,770 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_profil e_t
**p_buf_UserSec Info, error_status_t *retStatus)

// Structure tss_user_profil e_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_profil e_t;

// Structure tss_user_info_t
typedef struct
{
char user_uuid[USER_UUID_LEN+1];
char user_sec_info_s tatus[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_cod e[LOC_CTRY_CODE_L EN+1];
char loc_city_code[LOC_CITY_CODE_L EN+1];
char telephone_numbe r[TELE_NUM_LEN+1];
char expense_code[EXPENSE_CODE_LE N+1];
char proc_locn_csu[PROC_LOCN_CSU_L EN+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_valu e;
[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_profil e_t **p_buf_UserSec Info;
*p_buf_UserSecI nfo = new tss_user_profil e_t;
memset((void*) *p_buf_UserSecI nfo, '\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_UserSec Info)->ProcAttr; //
Allocates the ProcAttr pointer :

ProcAttr = new odss_attribute_ value_list_t;
ProcAttr->attribute_valu e = new odss_attribute_ value_t;
ProcAttr->attribute_valu e->length = len1;
ProcAttr->attribute_valu e->attribute = attr1;
ProcAttr->next = tmp1;

unsigned32 len2 = 0;
char *attr2 = NULL;

odss_attribute_ value_list_t *tmp2 = (*p_buf_UserSec Info)-
>BkOffAttr; // Allocates the BkOffAttr pointer :
BkOffAttr = new odss_attribute_ value_list_t;
BkOffAttr->attribute_valu e = new odss_attribute_ value_t;
BkOffAttr->attribute_valu e->length = len2;
BkOffAttr->attribute_valu e->attribute = attr2;
BkOffAttr->next = tmp2;

unsigned32 len3 = 0;
char *attr3 = NULL;

odss_attribute_ value_list_t *tmp3 = (*p_buf_UserSec Info)-
>WkClusterAtt r; // Allocates the WkClusterAttr pointer :
WkClusterAttr = new odss_attribute_ value_list_t;
WkClusterAttr->attribute_valu e = new odss_attribute_ value_t;
WkClusterAttr->attribute_valu e->length = len3;
WkClusterAttr->attribute_valu e->attribute = attr3;
WkClusterAttr->next = tmp3;

unsigned32 len4 = 0;
char *attr4 = NULL;

odss_attribute_ value_list_t *tmp4 = (*p_buf_UserSec Info)->CSUAttr; //
Allocates the CSUAttr pointer :

CSUAttr = new odss_attribute_ value_list_t;
CSUAttr->attribute_valu e = new odss_attribute_ value_t;
CSUAttr->attribute_valu e->length = len4;
CSUAttr->attribute_valu e->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 6679
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_profil e_t
**p_buf_UserSec Info, error_status_t *retStatus)

// Structure tss_user_profil e_t from the function
tss_user_profil e_t **p_buf_UserSec Info;
*p_buf_UserSecI nfo = new tss_user_profil e_t;
memset((void*) *p_buf_UserSecI nfo, '\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_UserSecI nfo, I think you are running into trouble, because
p_buf_UserSecIn fo 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_profil e_t **p_buf_UserSec Info;
p_buf_UserSecIn fo = new *tss_user_profi le_t[SOME_INTERGER_V ALUE]; //
With SOME_INTERGER_V ALUE possibly equal to 1
for (unsigned int i = 0; i < SOME_INTERGER_V ALUE; ++i)
{
p_buf_UserSecIn fo[i] = new tss_user_profil e_t;
memset((void*) p_buf_UserSecIn fo[i], 0, sizeof(tss_user _profile_t));
}
>
unsigned32 len1 = 0;
char *attr1 = NULL;

odss_attribute_ value_list_t *tmp1 = (*p_buf_UserSec Info)->ProcAttr;
Again, I don't get what you do. (*p_buf_UserSec Info)->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_valu e = new odss_attribute_ value_t;
ProcAttr->attribute_valu e->length = len1;
ProcAttr->attribute_valu e->attribute = attr1;
attr1 is NULL anyway so you'better write
ProcAttr->attribute_valu e->attribute = NULL;
ProcAttr->next = tmp1;
Here too:
ProcAttr->next = NULL;

I guess, what do you want to do is:

(*p_buf_UserSec Info)->ProcAttr = new odss_attribute_ value_list_t;
(*p_buf_UserSec Info)->ProcAttr->attribute_valu e = new
odss_attribute_ value_t;
(*p_buf_UserSec Info)->ProcAttr->attribute_valu e->length = 0;
(*p_buf_UserSec Info)->ProcAttr->attribute_valu e->attribute = NULL;
(*p_buf_UserSec Info)->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.mic h...@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_profil e_t
**p_buf_UserSec Info, error_status_t *retStatus)
// Structure tss_user_profil e_t from the function
tss_user_profil e_t **p_buf_UserSec Info;
*p_buf_UserSecI nfo = new tss_user_profil e_t;
memset((void*) *p_buf_UserSecI nfo, '\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_UserSecI nfo, I think you are running into trouble, because
p_buf_UserSecIn fo 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_profil e_t **p_buf_UserSec Info;
p_buf_UserSecIn fo = new *tss_user_profi le_t[SOME_INTERGER_V ALUE]; //
With SOME_INTERGER_V ALUE possibly equal to 1
for (unsigned int i = 0; i < SOME_INTERGER_V ALUE; ++i)
{
p_buf_UserSecIn fo[i] = new tss_user_profil e_t;
memset((void*) p_buf_UserSecIn fo[i], 0, sizeof(tss_user _profile_t));

}
unsigned32 len1 = 0;
char *attr1 = NULL;
odss_attribute_ value_list_t *tmp1 = (*p_buf_UserSec Info)->ProcAttr;

Again, I don't get what you do. (*p_buf_UserSec Info)->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_valu e = new odss_attribute_ value_t;
ProcAttr->attribute_valu e->length = len1;
ProcAttr->attribute_valu e->attribute = attr1;

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

Here too:
ProcAttr->next = NULL;

I guess, what do you want to do is:

(*p_buf_UserSec Info)->ProcAttr = new odss_attribute_ value_list_t;
(*p_buf_UserSec Info)->ProcAttr->attribute_valu e = new
odss_attribute_ value_t;
(*p_buf_UserSec Info)->ProcAttr->attribute_valu e->length = 0;
(*p_buf_UserSec Info)->ProcAttr->attribute_valu e->attribute = NULL;
(*p_buf_UserSec Info)->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_profil e_t **p_buf_UserSec Info , 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_profil e_t
Since its a linked list type , I have to allocate memory to
tss_user_profil e_t structure dynamically using new operator so that I
can store the return object. Structure tss_user_profil e_t has nested
structures with pointers , things got complicated.
This is what I have now and its compiling
tss_user_profil e_t **p_buf_UserSec Info;
*p_buf_UserSecI nfo = new tss_user_profil e_t[1];
for (unsigned int i = 0; i < 1; ++i)
{
p_buf_UserSecIn fo[i] = new tss_user_profil e_t;
memset((void*)p _buf_UserSecInf o[i],0,
sizeof(tss_user _profile_t));

}

(*p_buf_UserSec Info)->ProcAttr = new odss_attribute_ value_list_t;
(*p_buf_UserSec Info)->ProcAttr->attribute_valu e = new
odss_attribute_ value_t;
(*p_buf_UserSec Info)->ProcAttr->attribute_valu e->length = 0;
(*p_buf_UserSec Info)->ProcAttr->attribute_valu e->attribute = NULL;
(*p_buf_UserSec Info)->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_profil e_t **p_buf_UserSec Info;
*p_buf_UserSecI nfo = new tss_user_profil e_t[1];
for (unsigned int i = 0; i < 1; ++i)
{
p_buf_UserSecIn fo[i] = new tss_user_profil e_t;
memset((void*)p _buf_UserSecInf o[i],0,
sizeof(tss_user _profile_t));

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

tss_user_profil e_t **p_buf_UserSec Info;
*p_buf_UserSecI nfo = new tss_user_profil e_t[1];
p_buf_UserSecIn fo[0] = new tss_user_profil e_t;
memset((void*)p _buf_UserSecInf o[0], 0, sizeof(tss_user _profile_t));

(*p_buf_UserSec Info)->ProcAttr = new odss_attribute_ value_list_t;
(*p_buf_UserSec Info)->ProcAttr->attribute_valu e = new
odss_attribute_ value_t;
(*p_buf_UserSec Info)->ProcAttr->attribute_valu e->length = 0;
(*p_buf_UserSec Info)->ProcAttr->attribute_valu e->attribute = NULL;
(*p_buf_UserSec Info)->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_UserSec Info)->ProcAttr;
while (list)
{
odss_attribute_ value_list_t *tmp = list->next;
delete list->attribute_valu e;
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
6735
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 function. Is there any general rules that tell me when to allocate memory? I thought I don't have to if it is a variable that's not a pointer, and I have to if it is. I am a bit confused about the arrays particularly. In normal situations,...
5
1908
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 be done as offsets from this. At the moment I have two data structures, a head to a linked list which in it contains the pointer to the first element in the linked list( this is redundant as far as I can work out) and the list itself. The data...
9
1955
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 some data types defined thus: typedef enum { ONE ,
9
2440
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, strings for logging, and structures for data points. I want to use one function to read data from this block and one function to write data, for example: sram_read(OBJECT_IDENTIFIER) would return a pointer to the appriate object and
94
4771
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 that I found inside of a string. Any ideas?
1
2326
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 a selection structure within another selection structure. I am supposed to use function definitions with parameters to validate a form. my code for what i have done so far is as follows(cropped): function mainFunction() { var dateFormat =...
3
2996
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 facing: 1- From structure and dynamic memory allocation point of view, the code is very complicated. The code has various “nested structures” with a number of levels. The size of memory allocated for pointer to structure or its pointer...
9
688
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
2012
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 takes me a lot of time (since i have to loop determining the data type and freeing it). Is there any idea to free all the contents of the structure in shortest possible time.
0
9595
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10232
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2822
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.