473,408 Members | 1,749 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,408 software developers and data experts.

Memory allocation in Structure to Structure

Hi all,
While coding, I got struck up in memory allocation for structure to
structure..
For instance..I wrote the following pgm..

#include <iostream.h>
#include <stdlib.h>

typedef struct A
{
int b;
int c;
struct E {
int d;
int e;
} *E;
} A;

void main()
{
A* i = (A * ) calloc(1,sizeof(A);
}

I need to allocate memory for the internal structure E. Iam getting
errors whatever format I follow.. i tried the following..
A.E = (E * ) calloc(1,sizeof(E);
A.E = (A.E * ) calloc(1,sizeof(E);
and other combinations..

Can anybody help me to sort out this problem???

Cheers,
Nick

Mar 8 '06 #1
2 2660
TB
pr*******@rediffmail.com skrev:
Hi all,
While coding, I got struck up in memory allocation for structure to
structure..
For instance..I wrote the following pgm..

#include <iostream.h>
#include <stdlib.h>

typedef struct A
Are you writing C or C++?
{
int b;
int c;
struct E {
int d;
int e;
} *E;
} A;

struct A {
struct E { };
E * f;
};
void main()
{
A* i = (A * ) calloc(1,sizeof(A);
A * i = new A;
i.f = new A::E;
}

I need to allocate memory for the internal structure E. Iam getting
errors whatever format I follow.. i tried the following..
A.E = (E * ) calloc(1,sizeof(E);
A.E = (A.E * ) calloc(1,sizeof(E);
and other combinations..

Can anybody help me to sort out this problem???


Make sure the code is at least syntactically correct before you post.

--
TB @ SWEDEN
Mar 8 '06 #2
I modified to the code as following and now it is working
#include <iostream.h>
#include <stdlib.h>

typedef struct A
{
int b;
int c;
struct E {
int d;
int e;
} *F;
}A;
A b;
void main()
{
b.F = new A::E; //mem alloc using new for a Nested Struct
b.F = (A::E* ) calloc(1,sizeof(A)); //second way.c the declartion
of struct
}

Mar 9 '06 #3

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

Similar topics

8
by: Berk Birand | last post by:
Hi all, I have to use C-style structures for an assignement. I cannot have any methods or constructors for it. What has surprised me is that in my code, I have to allocate memory for an array of...
9
by: Andrew Au | last post by:
Dear all, I am trying to write a piece of software that use Object Oriented design and implement it with C, I did the following == In Object.h == typedef struct ObjectStructure* Object; ...
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,...
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
2
by: nayannovellus | last post by:
As per my knowledge i know that when a structure is defined, no memory is assigned to its members but when a variable of that structure is declared memory is allocated and also this allocation takes...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
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...
5
by: cham | last post by:
Hi, I am working on c++ in a linux system ( Fedora core 4 ), kernel version - 2.6.11-1.1369_FC4 gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 ) In my code i am creating a vector to store...
12
by: David Given | last post by:
I have a situation where I need to be able to allocate chunks on unmapped virtual memory. Because the memory I'm allocating isn't mapped, I can't use a normal memory allocator, as most of them...
13
by: eternalLearner | last post by:
i am writing an client application that connects to the server and then sends data to server. but, the problem is that i get a java.net.ConnectException: Connection refused error. The code i have...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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...

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.