typedef struct
{
int a;
int b;
int c;
} ABC;
typedef struct
{
int d;
int e;
int f;
ABC *abc;
} DEF;
Create function should use a single call to malloc to allocate
memoryfor structure DEF and its constituting structure ABC. In other
words, after a call to the CreateDEF() function, the following
statements should be valid and should work without segmentation
violations:
DEF* pDEF = CreateDEF();
pDEF-->abc-->a = 100 ;
DEF *CreateDEF ( void )
{
}
void FreeDEF (ABC *pABC)
{
} 22 2930
friend.05 wrote:
typedef struct
{
int a;
int b;
int c;
} ABC;
typedef struct
{
int d;
int e;
int f;
ABC *abc;
} DEF;
Create function should use a single call to malloc to allocate
memoryfor structure DEF and its constituting structure ABC. In other
words, after a call to the CreateDEF() function, the following
statements should be valid and should work without segmentation
violations:
DEF* pDEF = CreateDEF();
pDEF-->abc-->a = 100 ;
DEF *CreateDEF ( void )
{
}
void FreeDEF (ABC *pABC)
{
}
Take your best stab at it, in a complete program. Then we'll help. We
don't do homework.
Brian
typedef struct
{
int a;
int b;
int c;
} ABC;
typedef struct
{
int d;
int e;
int f;
ABC *abc;
} DEF;
DEF *CreateDEF (void)
{
DEF strDEF;
strDEF.abc=(ABC *)malloc(sizeof (ABC));
strDEF.abc->a = 10;
printf("%d\n",s trDEF.abc->a);
retDEF=&strDEF;
return retDEF;
}
void FreeDEF (ABC *pABC)
{
}
void main()
{
DEF *pdef = CreateDEF();
pdef->d = 7;
printf("%d\n",p def->d);
pdef->abc->a = 10;
printf("%d\n",p def->abc->a);
}
But it is giving segmentation fault
On May 8, 5:28 pm, "Default User" <defaultuse...@ yahoo.comwrote:
friend.05 wrote:
typedef struct
{
int a;
int b;
int c;
} ABC;
typedef struct
{
int d;
int e;
int f;
ABC *abc;
} DEF;
Create function should use a single call to malloc to allocate
memoryfor structure DEF and its constituting structure ABC. In other
words, after a call to the CreateDEF() function, the following
statements should be valid and should work without segmentation
violations:
DEF* pDEF = CreateDEF();
pDEF-->abc-->a = 100 ;
DEF *CreateDEF ( void )
{
}
void FreeDEF (ABC *pABC)
{
}
Take your best stab at it, in a complete program. Then we'll help. We
don't do homework.
Brian- Hide quoted text -
- Show quoted text -
In article <11************ **********@y5g2 000hsa.googlegr oups.com>,
friend.05 <hi**********@g mail.comwrote:
>DEF *CreateDEF (void) {
DEF strDEF;
strDEF.abc=(ABC *)malloc(sizeof (ABC));
strDEF.abc->a = 10;
printf("%d\n",s trDEF.abc->a);
retDEF=&strDEF;
return retDEF; }
You can't return a pointer to a local variable (DEF). (Or rather,
you can't do anything with such a pointer once it has been
returned.)
Consider allocating a memory area big enough to hold both
structures.
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
friend.05 wrote:
Please don't top post!
typedef struct
{
int d;
int e;
int f;
ABC *abc;
} DEF;
DEF *CreateDEF (void)
{
DEF strDEF;
strDEF.abc=(ABC *)malloc(sizeof (ABC));
Avoid casting the return of malloc.
strDEF.abc->a = 10;
printf("%d\n",s trDEF.abc->a);
retDEF=&strDEF;
return retDEF;
Think about what you are returning, a local variable. What happens to
strDEF after the function returns.
>
void main()
Don't write this unless you have good grounds to do so. main returns int.
>
But it is giving segmentation fault
Explained above.
--
Ian Collins.
friend.05 wrote:
typedef struct
{
int a;
int b;
int c;
} ABC;
typedef struct
{
int d;
int e;
int f;
ABC *abc;
} DEF;
DEF *CreateDEF (void)
{
DEF strDEF;
strDEF.abc=(ABC *)malloc(sizeof (ABC));
strDEF.abc->a = 10;
printf("%d\n",s trDEF.abc->a);
retDEF=&strDEF;
return retDEF;
}
void FreeDEF (ABC *pABC)
{
}
void main()
{
DEF *pdef = CreateDEF();
pdef->d = 7;
printf("%d\n",p def->d);
pdef->abc->a = 10;
printf("%d\n",p def->abc->a);
}
But it is giving segmentation fault
On May 8, 5:28 pm, "Default User" <defaultuse...@ yahoo.comwrote:
friend.05 wrote:
typedef struct
{
int a;
int b;
int c;
} ABC;
typedef struct
{
int d;
int e;
int f;
ABC *abc;
} DEF;
Create function should use a single call to malloc to allocate
memoryfor structure DEF and its constituting structure ABC. In
other words, after a call to the CreateDEF() function, the
following statements should be valid and should work without
segmentation violations:
DEF* pDEF = CreateDEF();
pDEF-->abc-->a = 100 ;
DEF *CreateDEF ( void )
{
}
void FreeDEF (ABC *pABC)
{
}
Take your best stab at it, in a complete program. Then we'll help.
We don't do homework.
Brian- Hide quoted text -
- Show quoted text -
--
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html >
Default User wrote:
friend.05 wrote:
So he did :)
>typedef struct { int a; int b; int c; } ABC;
typedef struct { int d; int e; int f; ABC *abc; } DEF;
DEF *CreateDEF (void) { DEF strDEF;
strDEF.abc=(ABC *)malloc(sizeof (ABC)); strDEF.abc->a = 10; printf("%d\n",s trDEF.abc->a); retDEF=&strDEF; return retDEF; }
void FreeDEF (ABC *pABC) {
}
void main() { DEF *pdef = CreateDEF(); pdef->d = 7; printf("%d\n", pdef->d); pdef->abc->a = 10; printf("%d\n", pdef->abc->a); }
But it is giving segmentation fault
On May 8, 5:28 pm, "Default User" <defaultuse...@ yahoo.comwrote:
>>friend.05 wrote: typedef struct { int a; int b; int c; } ABC; typedef struct { int d; int e; int f; ABC *abc; } DEF; Create function should use a single call to malloc to allocate memoryfor structure DEF and its constituting structure ABC. In other words, after a call to the CreateDEF() function, the following statements should be valid and should work without segmentati on violations: DEF* pDEF = CreateDEF(); pDEF-->abc-->a = 100 ; DEF *CreateDEF ( void ) { } void FreeDEF (ABC *pABC) { } Take your best stab at it, in a complete program. Then we'll help. We don't do homework.
Brian- Hide quoted text -
- Show quoted text -
--
Ian Collins.
friend.05 wrote:
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html >
I'll rearrange things.
Create function should use a single call to malloc to allocate
memoryfor structure DEF and its constituting structure ABC. In
other words, after a call to the CreateDEF() function, the
following statements should be valid and should work without
segmentation violations:
DEF* pDEF = CreateDEF();
pDEF-->abc-->a = 100 ;
DEF *CreateDEF ( void )
{
}
void FreeDEF (ABC *pABC)
{
}
Take your best stab at it, in a complete program. Then we'll help.
We don't do homework.
Add (see below for reason):
#include <stdlib.h>
typedef struct
{
int a;
int b;
int c;
} ABC;
typedef struct
{
int d;
int e;
int f;
ABC *abc;
} DEF;
DEF *CreateDEF (void)
{
DEF strDEF;
strDEF.abc=(ABC *)malloc(sizeof (ABC));
We don't recommend casting the results of malloc(). It can hide a
dangerous problem, that of not including the proper header, which you
did not.
strDEF.abc->a = 10;
printf("%d\n",s trDEF.abc->a);
retDEF=&strDEF;
return retDEF;
Returning a pointer to a local variable is not allowed.
Based on the requirements, which are very bad, you'll have to do some
unpleasant stuff. The obvious way is to allocate a chunk of memory
large enough for both structs. Then you set the "ABC" pointer to the
extra memory.
What's troublesome is the alignment issue. The reason you normally use
malloc() and friends is that you get a pointer properly aligned for any
type.
I have a sort of solution that "works" as in doesn't crash on either
VC++ or gcc/Solaris, which both have four-byte ints and struct pointers.
I think it's probably ok. The DEF struct should pad the end to get back
on a good boundary as needed for things like arrays of the structs.
DEF *CreateDEF (void)
{
char *p;
DEF *strDEF;
strDEF = malloc(sizeof(D EF) + sizeof(ABC));
p = (char*)strDEF;
p += sizeof(strDEF);
strDEF->abc = (void*)p;
strDEF->abc->a = 10;
printf("%d\n",s trDEF->abc->a);
return strDEF;
}
The gang will gently point out (ouch!!) if I'm totally whacked out on
that. It's best not to do this sort of thing. Why you had a requirement
for a single allocation, I can't fathom.
Brian
Ian Collins wrote:
Default User wrote:
friend.05 wrote:
So he did :)
Yes, there's some sort of key combination that sends posts. I
accidentally did whatever it is.
Brian
On May 8, 10:22 pm, "friend.05" <hirenshah...@g mail.comwrote:
typedef struct
{
int a;
int b;
int c;
} ABC;
typedef struct
{
int d;
int e;
int f;
ABC *abc;
} DEF;
Create function should use a single call to malloc to allocate
memoryfor structure DEF and its constituting structure ABC.
Assuming that this is homework, I would love to see the solution that
your teacher provides. The specification for the FreeDEF function
looks rather bizarre. I think the following should work:
DEF* CreateDEF (void) {
typedef struct { ABC abc; DEF def; } combined;
combined* p = malloc (sizeof (combined));
if (p) { p->def.abc = &p->abc; return &p->def; }
else return NULL;
}
void FreeDEF (ABC* pABC) { free (pABC); } This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dan |
last post by:
I'm trying to creat a data structure, that can be either a integer,
double, string, or linked list. So I created the following, but don't
know if it is the data structure itself causing problems, or something
I am doing in the rest of the program.
This is the data structure.
struct node
{
char type;
|
by: Aaron Walker |
last post by:
I am trying to write this piece of code that reads a config file, in the
format:
ENTRY value
and I want to read the config file, then read the "value" into the
appropriate member of the following structure:
struct conf_data {
char *root;
|
by: Eric Smith |
last post by:
Is a structure containing an incomplete array as its last element (per
paragraph 2 of section 6.7.2.1 of ISO/IEC 9899:1999 (E)) itself an
incomplete type? That appears to be indicated by paragraph 22 of
section 6.2.5. If so, that seems to make it difficult to allocate such
structures, because sizeof() is not allowed on incomplete types
(paragraph 1 of section 6.5.3.4).
For instance, I've routinely done things like this:
struct foo {...
|
by: Mannequin* |
last post by:
Hi all,
I'm working on a quick program to bring the Bible into memory from a
text file. Anyway, I have three questions to ask.
First, is my implementation of malloc () correct in the program to
follow?
Second, have I correctly passed the structure's pointer to the
functions in this program?
|
by: John |
last post by:
In the course of an assignment, I learned the hard way that I shouldn't try
to free a malloc'd member of a malloc'd structure after having freed that
structure (i.e., free( structure ); free( structure->bufferspace ) ).
My question is, if I free just the structure, will the (e.g.) bufferspace be
freed implicitly, or do I have to (as I currently am) free the members
first?
Thanks.
-cjl
| |
by: Excluded_Middle |
last post by:
Suppose I have a struct
typdef struct foo
{
int age;
char *name;
}foo;
now I made a list of foo using
|
by: Madhav |
last post by:
Hi all,
I was going through a piece of code which had a very
interesting format. There were two files: one was a .h file, and the
other was a .c file. The .c file had a structure defined in it which
was typedef'ed in .h file.
what I observered was even more interesting: I was allowed
to declare objects of structure that was typedefed in the header file,
but I could not use any of the members of the structure outside the .c
file which...
|
by: ulyses |
last post by:
I'm trying to put pointer to flexible array of structures in other
structure. I want to have pointer to array of pixels in screen
structure. Here is mine code, but I think it isn't quite all right:
struct pixel
{
int x;
int y;
int color;
};
|
by: SP |
last post by:
The following code crashes after I add the two nested FOR loops at the
end, I am starting to learn about pointers and would like to understand
what I'm doing wrong. I think the problem is the way I access the
array elements.
Thanks for your help.
#include <stdio.h>
|
by: jbholman |
last post by:
I am pretty new to C and doing my first project in C. I actually read
almost the entire FAQ, but can't seem to figure out this problem.
I have a structure. I have a list of these structures. Inside each
structure, I have two members: a list of strings, and a string.
I have made a sample program below that exhibits the error I am
having. I also read about Valgrind, and used it to tell me where I
was getting the segmentation fault,...
|
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...
| |
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,...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |