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

structure and malloc

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)
{
}

May 8 '07 #1
22 2883
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
May 8 '07 #2
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",strDEF.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 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 -

May 8 '07 #3
In article <11**********************@y5g2000hsa.googlegroups. com>,
friend.05 <hi**********@gmail.comwrote:
>DEF *CreateDEF (void)
{
DEF strDEF;

strDEF.abc=(ABC *)malloc(sizeof(ABC));
strDEF.abc->a = 10;
printf("%d\n",strDEF.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)
May 8 '07 #4
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",strDEF.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.
May 8 '07 #5
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",strDEF.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
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>
May 8 '07 #6
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",strDEF.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
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 -



--
Ian Collins.
May 8 '07 #7
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",strDEF.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(DEF) + sizeof(ABC));
p = (char*)strDEF;
p += sizeof(strDEF);
strDEF->abc = (void*)p;
strDEF->abc->a = 10;
printf("%d\n",strDEF->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
May 8 '07 #8
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
May 8 '07 #9
On May 8, 10:22 pm, "friend.05" <hirenshah...@gmail.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); }

May 9 '07 #10
On 8 May 2007 23:19:49 GMT, "Default User" <de***********@yahoo.com>
wrote:
>friend.05 wrote:
snip
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:
snip
>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(DEF) + sizeof(ABC));
p = (char*)strDEF;
p += sizeof(strDEF);
strDEF->abc = (void*)p;
strDEF->abc->a = 10;
printf("%d\n",strDEF->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.
The alignment issue can be resolved by declaring a local struct type
which contains both "real" structures, as in
struct mystruct{
DEF x;
ABC y;
};

We know the offset of x is 0 and we can find the offset of y using the
offsetof macro. If you allocate enough space for a struct mystruct
and store the address in DEF* p. You can then safely use
p->abc = (ABC*)((char*)p + offsetof(struct mystruct, y));
to insure that abc points to a valid ABC. Then
p->abc->a = 10;
is safe.
Remove del for email
May 9 '07 #11
Default User wrote:
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",strDEF.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(DEF) + sizeof(ABC));
p = (char*)strDEF;
p += sizeof(strDEF);
strDEF->abc = (void*)p;
I would prefer a second malloc here, just to be sure of the alignment.
--
Ian Collins.
May 9 '07 #12
Ian Collins wrote:
Default User wrote:
>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(DEF) + sizeof(ABC));
p = (char*)strDEF;
p += sizeof(strDEF);
strDEF->abc = (void*)p;

I would prefer a second malloc here, just to be sure of the alignment.

Sorry for not trimming before.

The alternative local struct solution may be better than 2 mallocs,
assuming the lifetime of abc is the same as def.

--
Ian Collins.
May 9 '07 #13
Ian Collins wrote:
Default User wrote:
DEF *CreateDEF (void)
{
char *p;
DEF *strDEF;

strDEF = malloc(sizeof(DEF) + sizeof(ABC));
p = (char*)strDEF;
p += sizeof(strDEF);
strDEF->abc = (void*)p;

I would prefer a second malloc here, just to be sure of the alignment.

Absolutely, but the original requirements were:
>>Create function should use a single call to malloc to allocate
memoryfor structure DEF and its constituting structure ABC.


Brian

May 9 '07 #14
Barry Schwarz wrote:
On 8 May 2007 23:19:49 GMT, "Default User" <de***********@yahoo.com>
wrote:
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(DEF) + sizeof(ABC));
p = (char*)strDEF;
p += sizeof(strDEF);
strDEF->abc = (void*)p;
strDEF->abc->a = 10;
printf("%d\n",strDEF->abc->a);
return strDEF;
}
The alignment issue can be resolved by declaring a local struct type
which contains both "real" structures, as in
struct mystruct{
DEF x;
ABC y;
};
Oh, that's a pretty slick idea. I didn't think of that, but yeah it
should take care of the situation.

Brian
May 9 '07 #15
On 8 May 2007 23:19:49 GMT, "Default User" <de***********@yahoo.com>
wrote in comp.lang.c:
friend.05 wrote:
[snip]
Returning a pointer to a local variable is not allowed.
Absolutely untrue. Of course it can be returned, and in fact it was.

Undefined behavior occurs if one uses the value of that pointer, or
any other pointer that points to memory whose storage duration has
expired.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
May 9 '07 #16
Jack Klein wrote:
On 8 May 2007 23:19:49 GMT, "Default User" <de***********@yahoo.com>
wrote in comp.lang.c:
friend.05 wrote:

[snip]
Returning a pointer to a local variable is not allowed.

Absolutely untrue. Of course it can be returned, and in fact it was.
Yeah, yeah, after I posted that I knew I should have been more specific.

Brian
May 9 '07 #17
"Default User" <de***********@yahoo.comwrites:
Barry Schwarz wrote:
>On 8 May 2007 23:19:49 GMT, "Default User" <de***********@yahoo.com>
wrote:
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(DEF) + sizeof(ABC));
p = (char*)strDEF;
p += sizeof(strDEF);
strDEF->abc = (void*)p;
strDEF->abc->a = 10;
printf("%d\n",strDEF->abc->a);
return strDEF;
}
>The alignment issue can be resolved by declaring a local struct type
which contains both "real" structures, as in
struct mystruct{
DEF x;
ABC y;
};

Oh, that's a pretty slick idea. I didn't think of that, but yeah it
should take care of the situation.
Yeah, pretty slick. And isn't it nice that the OP didn't have to do
his own homework?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 9 '07 #18
Default User wrote:
Ian Collins wrote:
>Default User wrote:
>>DEF *CreateDEF (void)
{
char *p;
DEF *strDEF;

strDEF = malloc(sizeof(DEF) + sizeof(ABC));
p = (char*)strDEF;
p += sizeof(strDEF);
strDEF->abc = (void*)p;
I would prefer a second malloc here, just to be sure of the alignment.


Absolutely, but the original requirements were:
>>>>>Create function should use a single call to malloc to allocate
>memoryfor structure DEF and its constituting structure ABC.
Now you can see why I used to get marked exam papers back with "real the
whole question" scrawled on them!
--
Ian Collins.
May 9 '07 #19
Keith Thompson wrote:
"Default User" <de***********@yahoo.comwrites:
Barry Schwarz wrote:
The alignment issue can be resolved by declaring a local struct
type >which contains both "real" structures, as in
struct mystruct{
DEF x;
ABC y;
};
Oh, that's a pretty slick idea. I didn't think of that, but yeah it
should take care of the situation.

Yeah, pretty slick. And isn't it nice that the OP didn't have to do
his own homework?
The OP made a stab after prodding, and frankly this was such a poor
assignment that I'm ok with things.

Brian
May 9 '07 #20
On 8 May 2007 14:22:42 -0700, "friend.05" <hi**********@gmail.com>
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)
{
}
Naming a function FreeDEF when it free's an object of type *ABC is not
a good idea.

Here is an example that is guaranteed not to cause a segmentation
violation. It is also guaranteed to give you 100% decision coverage,
and even 100% Modified Condition/Decision Coverage (MC/DC), if you can
somehow get malloc() to return NULL.

#include <assert.h>
#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 *pDEF;
pDEF = malloc(sizeof *pDEF);
assert(pDEF != NULL);
#ifdef NDEBUG
if ( !pDEF )
{
exit(EXIT_FAILURE);
}
#endif

pDEF->abc = malloc(sizeof *pDEF->abc);
assert(pDEF->abc != NULL);
#ifdef NDEBUG
if ( !pDEF->abc )
{
exit(EXIT_FAILURE);
}
#endif
return pDEF;
}

void FreeDEF(DEF *pDEF)
{
free(pDEF->abc);
free(pDEF);
}

int main(void)
{
DEF* pDEF = CreateDEF();
pDEF->abc->a = 100;
FreeDEF(pDEF);
return 0;
}

--
jay
May 9 '07 #21

"jaysome" <ja*****@hotmail.comwrote in message
news:fi********************************@4ax.com...
On 8 May 2007 14:22:42 -0700, "friend.05" <hi**********@gmail.com>
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)
{
}

Naming a function FreeDEF when it free's an object of type *ABC is not
a good idea.

Here is an example that is guaranteed not to cause a segmentation
violation. It is also guaranteed to give you 100% decision coverage,
and even 100% Modified Condition/Decision Coverage (MC/DC), if you can
somehow get malloc() to return NULL.

#include <assert.h>
#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 *pDEF;
pDEF = malloc(sizeof *pDEF);
assert(pDEF != NULL);
#ifdef NDEBUG
if ( !pDEF )
{
exit(EXIT_FAILURE);
}
#endif

pDEF->abc = malloc(sizeof *pDEF->abc);
assert(pDEF->abc != NULL);
#ifdef NDEBUG
if ( !pDEF->abc )
{
exit(EXIT_FAILURE);
}
#endif
return pDEF;
}

void FreeDEF(DEF *pDEF)
{
free(pDEF->abc);
free(pDEF);
}

int main(void)
{
DEF* pDEF = CreateDEF();
pDEF->abc->a = 100;
FreeDEF(pDEF);
return 0;
}
Your "solution" gets graded an F.
Read the requirements:
"Create function should use a single call to malloc"
Your solution has 2 calls to malloc.
--
Fred L. Kleinschmidt
May 9 '07 #22
"Fred Kleinschmidt" <fr******************@boeing.comwrote in message
news:JH********@news.boeing.com...
Your "solution" gets graded an F.
Read the requirements:
"Create function should use a single call to malloc"
Your solution has 2 calls to malloc.
I'd have already given the instructor an F for requiring the argument to
FreeDEF() to be a ABC* (a gross violation of the principle of least
astonishment) and then walked out of the class to go get a refund.

Still, if one modifies Barry's answer like this:

struct mystruct {
ABC x;
DEF y;
};

you can meet the requirements. CreateDEF() would malloc() a mystruct, point
y->abc to x, and return &y. FreeDEF() would simply call free() on its
argument.

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
--
Posted via a free Usenet account from http://www.teranews.com

May 9 '07 #23

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

Similar topics

4
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,...
4
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...
6
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...
11
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...
13
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(...
7
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
2
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...
8
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:...
8
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...
25
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. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.