Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Structure memory allocation

Reply
Thread Tools

Structure memory allocation

 
 
Trying_Harder
Guest
Posts: n/a
 
      09-16-2003
Consider the following declaration,

#include <stdio.h>
#include <stdlib.h>

typedef struct foo {
char name[30];
int age;
}Foo;

typedef struct bar {
char * name;
int age;
}Bar;

typedef struct baz {
char name[30];
int *age;
}Baz;

int main()
{
Foo f1,f2;
Bar b1,b2;
Baz c1,c2;

/* Populating structure */
strcpy(f1.name,"JACK");
f1.age=10;

/* CASE 1 - Works fine */
f2=f1;
printf("\n F2 Members, Name %s , Age %d ", f2.name, f2.age);
printf("\n F1 Pointer %p , F2 Pointer %p ", f1, f2);


/* CASE 2 - Hmmmm.. I don't have an explanation for this */
b1.name = malloc(sizeof(char)*10);
strcpy(b1.name,"LACK");
b1.age=20;

b2=b1; /*Assignment??*/
printf("\n B2 Members, Name %s , Age %d ", b2.name, b2.age);
printf("\n B1 Pointer %p , B2 Pointer %p ", b1, b2);
/* This will print the same address out. */
printf("\n B1 Name Pointer %p , B2 Name Pointer %p ", b1.name, \
b2.name);


/* CASE 3 - As expected, does not work */
c1.age = malloc(sizeof(int));
strcpy(c1.name,"MACK");
*(c1.age)=30;

c2=c1; /*Assignment??*/
printf("\n C2 Members, Name %s , Age %d ", c2.name, c2.age);
printf("\n C1 Pointer %p , C2 Pointer %p ", c1, c2);

/* But this will print the same address out, how come? */
printf("\n C1 Name Pointer %p , C2 Name Pointer %p ", c1.age ,\
c2.age );

exit(EXIT_SUCCESS);
}


Let me begin with apologies for posting so much of code.

Clarification 1 : Is copying structures by simply assigning them, valid
(legal) portable, pedantic ?
<My Opinion> : No. If someone argues, please reason.

Clarification 2 : Why is case 2 working ?

Clarification 3 : In case 3, you will notice the address copied exactly.
Does copying address mean pointing to the same
location?
 
Reply With Quote
 
 
 
 
Morris Dovey
Guest
Posts: n/a
 
      09-16-2003
Trying_Harder wrote:

> Clarification 1 : Is copying structures by simply assigning them, valid
> (legal) portable, pedantic ?
> <My Opinion> : No. If someone argues, please reason.


It's legal and portable (see standard)
>
> Clarification 2 : Why is case 2 working ?


Why would you not expect it to work?

> Clarification 3 : In case 3, you will notice the address copied exactly.
> Does copying address mean pointing to the same
> location?


It does, but you'd do better with:

printf("\n C2 Members, Name %s , Age %d ", c2.name, *c2.age);

since c2.age is a pointer (as you remembered in your next statement.

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c

 
Reply With Quote
 
 
 
 
Jake Roersma
Guest
Posts: n/a
 
      09-16-2003
On Mon, 15 Sep 2003 22:30:44 -0700, Trying_Harder wrote:

> #include <stdio.h>
> #include <stdlib.h>


#include <string.h>

> typedef struct baz {
> char name[30];
> int *age;
> }Baz;
>
> int main()
> {
> Foo f1,f2;
> Bar b1,b2;
> Baz c1,c2;
>
> /* Populating structure */
> strcpy(f1.name,"JACK");
> f1.age=10;
>
> /* CASE 1 - Works fine */
> printf("\n F1 Pointer %p , F2 Pointer %p ", f1, f2);


This may work but is not right. Try:

printf("\n F1 Pointer %p , F2 Pointer %p ", &f1, &f2);

>
> printf("\n B1 Pointer %p , B2 Pointer %p ", b1, b2);


And again....

printf("\n B1 Pointer %p , B2 Pointer %p ", &b1, &b2);

>
> printf("\n C2 Members, Name %s , Age %d ", c2.name, c2.age);


You'll need to dereference c2.age to get its value:

printf("\n C2 Members, Name %s , Age %d ", c2.name, *c2.age);

> printf("\n C1 Pointer %p , C2 Pointer %p ", c1, c2);


I hate to be repetitive.. but...

printf("\n C1 Pointer %p , C2 Pointer %p ", &c1, &c2);

>
> /* But this will print the same address out, how come? */
> printf("\n C1 Name Pointer %p , C2 Name Pointer %p ", c1.age ,\
> c2.age );
>


free(b1.name);
free(c1.age);

> exit(EXIT_SUCCESS);
> }
>
>
> Let me begin with apologies for posting so much of code.
>
> Clarification 1 : Is copying structures by simply assigning them, valid
> (legal) portable, pedantic ?


legal and portable.

>
> Clarification 2 : Why is case 2 working ?


Other than the fact you are not freeing the memory you've allocated I
don't see any reason as to why it shouldn't work.

> Clarification 3 : In case 3, you will notice the address copied exactly.
> Does copying address mean pointing to the same
> location?


Yep.

- Jake


 
Reply With Quote
 
Al Bowers
Guest
Posts: n/a
 
      09-16-2003


Trying_Harder wrote:
> Consider the following declaration,
>
> #include <stdio.h>
> #include <stdlib.h>


#include <string.h> /* for function strcpy */

>
> typedef struct foo {
> char name[30];
> int age;
> }Foo;
>
> typedef struct bar {
> char * name;
> int age;
> }Bar;
>
> typedef struct baz {
> char name[30];
> int *age;
> }Baz;
>
> int main()
> {
> Foo f1,f2;
> Bar b1,b2;
> Baz c1,c2;
>
> /* Populating structure */
> strcpy(f1.name,"JACK");
> f1.age=10;
>
> /* CASE 1 - Works fine */
> f2=f1;
> printf("\n F2 Members, Name %s , Age %d ", f2.name, f2.age);
> printf("\n F1 Pointer %p , F2 Pointer %p ", f1, f2);
>


%p specifies a void* argument. f1 and f2 need to be pointers that
should be cast to void*.
printf("\n F1 Pointer %p, F2 Pointer %p ", (void*)&f1, (void*)&f2);
>
> /* CASE 2 - Hmmmm.. I don't have an explanation for this */
> b1.name = malloc(sizeof(char)*10);
> strcpy(b1.name,"LACK");
> b1.age=20;
>
> b2=b1; /*Assignment??*/
> printf("\n B2 Members, Name %s , Age %d ", b2.name, b2.age);
> printf("\n B1 Pointer %p , B2 Pointer %p ", b1, b2);
> /* This will print the same address out. */
> printf("\n B1 Name Pointer %p , B2 Name Pointer %p ", b1.name, \
> b2.name);
>


>
> /* CASE 3 - As expected, does not work */
> c1.age = malloc(sizeof(int));
> strcpy(c1.name,"MACK");
> *(c1.age)=30;
>
> c2=c1; /*Assignment??*/
> printf("\n C2 Members, Name %s , Age %d ", c2.name, c2.age);


*c2.age

> printf("\n C1 Pointer %p , C2 Pointer %p ", c1, c2);
>
> /* But this will print the same address out, how come? */
> printf("\n C1 Name Pointer %p , C2 Name Pointer %p ", c1.age ,\
> c2.age );


Perhaps you are being confused by the above printf statement. You
are printing "Name Pointer" but your arguments refer to the age members.
Shouldn't these be:

printf("C1 Name Pointer %p , C2 Name Pointer %p\n",
(void*) c1.name ,(void*)c2.name);
printf("C1 Age Pointer %p , C2 Age Pointer %p\n", (void*)c1.age ,
(void*)c2.age );
>
> exit(EXIT_SUCCESS);
> }
>
>


>
> Clarification 3 : In case 3, you will notice the address copied exactly.
> Does copying address mean pointing to the same
> location?


Try the corrected code and see if you still need clarification.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct foo {
char name[30];
int age;
}Foo;

typedef struct bar {
char * name;
int age;
}Bar;

typedef struct baz {
char name[30];
int *age;
}Baz;

int main(void)
{
Foo f1,f2;
Bar b1,b2;
Baz c1,c2;

strcpy(f1.name,"JACK");
f1.age=10;
f2=f1;
printf("F2 Members, Name %s , Age %d\n", f2.name, f2.age);
printf("F1 Pointer %p , F2 Pointer %p\n", (void*)&f1, (void*)&f2);
b1.name = malloc(sizeof(char)*10);
strcpy(b1.name,"LACK");
b1.age=20;
b2=b1; /*Assignment??*/
printf("B2 Members, Name %s , Age %d\n", b2.name, b2.age);
printf("B1 Pointer %p , B2 Pointer %p\n", (void*)&b1, (void*)&b2);
/* This will print the same address out. */
printf("B1 Name Pointer %p , B2 Name Pointer %p\n",
(void*)b1.name, (void*)b2.name);
c1.age = malloc(sizeof(int));
strcpy(c1.name,"MACK");
*(c1.age)=30;
c2=c1; /*Assignment??*/
printf("C2 Members, Name %s , Age %d\n", c2.name, *c2.age);
printf("C1 Pointer %p , C2 Pointer %p\n", (void*)&c1, (void*)&c2);
printf("C1 Name Pointer %p , C2 Name Pointer %p\n",
(void*) c1.name ,(void*)c2.name);
printf("C1 Age Pointer %p , C2 Age Pointer %p\n", (void*)c1.age ,
(void*)c2.age );
return 0;
}

--
Al Bowers
Tampa, Fl USA
mailto: (remove the x)
http://www.geocities.com/abowers822/

 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      09-16-2003
Jake Roersma <> wrote:

>On Mon, 15 Sep 2003 22:30:44 -0700, Trying_Harder wrote:

<SNIP>
>> printf("\n F1 Pointer %p , F2 Pointer %p ", f1, f2);

>
>This may work but is not right. Try:
>
>printf("\n F1 Pointer %p , F2 Pointer %p ", &f1, &f2);


The printf %p conversion specification requires a void pointer argument;
so make it:

printf("\n F1 Pointer %p , F2 Pointer %p ", (void *)&f1, (void *)&f2);
>>
>> printf("\n B1 Pointer %p , B2 Pointer %p ", b1, b2);

>
>And again....
>
>printf("\n B1 Pointer %p , B2 Pointer %p ", &b1, &b2);

.... and again ...

printf("\n B1 Pointer %p , B2 Pointer %p ", (void *)&b1, (void *)&b2);
>
>>
>> printf("\n C2 Members, Name %s , Age %d ", c2.name, c2.age);

>
>You'll need to dereference c2.age to get its value:
>
>printf("\n C2 Members, Name %s , Age %d ", c2.name, *c2.age);
>
>> printf("\n C1 Pointer %p , C2 Pointer %p ", c1, c2);

>
>I hate to be repetitive.. but...
>
>printf("\n C1 Pointer %p , C2 Pointer %p ", &c1, &c2);
>

.... me too, but ...

printf("\n C1 Pointer %p , C2 Pointer %p ", (void *)&c1, (void *)&c2);

<SNIP>
Note that the only useful application for printing out pointer values I
know of is for debugging purposes.

Regards

Irrwahn
--
What does this red button do?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
memory allocation for structure array jvax C Programming 4 11-24-2010 11:43 PM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
Memory allocation in Structure to Structure pra_ramli@rediffmail.com C++ 2 03-09-2006 05:51 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
Memory Allocation for Structure / Class akshay4friend@yahoo.com C++ 2 02-16-2005 04:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57