Allocating Memory for Structs Item by Item 
Author Message
 Allocating Memory for Structs Item by Item

Hi, there!  I'm new to this group, and was hoping some kind soul out there
could help me with this gnarly question.

I am an IBM mainframe computer programmer, but also program on the
Macintosh in HyperCard.  To rev up the power, I'm learning C with the book
"Learn C on the Macintosh" by Dave Mark.

In it, he has sample exercises, but only every other one is answered in
the back.  That's OK - I got all those OK.

What's really stumping me, is the second to last major assignment in the
book, the one where you're supposed to modify the CDFiler program's memory
allocation.

Right now, it calls malloc to allocate the memory for the CDInfo struct.
The CDInfo struct contains two arrays of 50 characters each, to contain
the artist and title name.

I'm supposed to modify the CDInfo struct so it contains POINTERS instead
of arrays, and call malloc for each string individually as I need it.  I'm
in it way deep, here!  I took a stab at it, but it's not easy, it appears.

I've exhausted the reference manuals, Internet searches, everything.  I've
even broke down and bought the 2nd edition of this book, because he says
he supplies answers to ALL questions in that book, plus all the sample
exercises, as well.  But, the questions are supposedly different in the
second book - those questions won't help me with this particular problem
(at least, I presume not - the book hasn't come in the mail yet.)

Thanks in advance for any assistance you can lend me!  Don't flame me
because I'm writing from AOL - AOL has its place in the scheme of things
(like now - when my Internet provider has gone down, and I desperately
want to talk to people!)

Any suggestions on where to get the best Macintosh C programming help in
general?  If this is not the best place, feel free to tell me.




Fri, 27 Nov 1998 03:00:00 GMT  
 Allocating Memory for Structs Item by Item

: Hi, there!  I'm new to this group, and was hoping some kind soul out there
: could help me with this gnarly question.

: I am an IBM mainframe computer programmer, but also program on the
: Macintosh in HyperCard.  To rev up the power, I'm learning C with the book
: "Learn C on the Macintosh" by Dave Mark.

: In it, he has sample exercises, but only every other one is answered in
: the back.  That's OK - I got all those OK.

: What's really stumping me, is the second to last major assignment in the
: book, the one where you're supposed to modify the CDFiler program's memory
: allocation.

: Right now, it calls malloc to allocate the memory for the CDInfo struct.
: The CDInfo struct contains two arrays of 50 characters each, to contain
: the artist and title name.

: I'm supposed to modify the CDInfo struct so it contains POINTERS instead
: of arrays, and call malloc for each string individually as I need it.  I'm
: in it way deep, here!  I took a stab at it, but it's not easy, it appears.

: I've exhausted the reference manuals, Internet searches, everything.  I've
: even broke down and bought the 2nd edition of this book, because he says
: he supplies answers to ALL questions in that book, plus all the sample
: exercises, as well.  But, the questions are supposedly different in the
: second book - those questions won't help me with this particular problem
: (at least, I presume not - the book hasn't come in the mail yet.)

: Thanks in advance for any assistance you can lend me!  Don't flame me
: because I'm writing from AOL - AOL has its place in the scheme of things
: (like now - when my Internet provider has gone down, and I desperately
: want to talk to people!)

: Any suggestions on where to get the best Macintosh C programming help in
: general?  If this is not the best place, feel free to tell me.


try this

struct info {
    char *name;
    char *address;
    struct info *next;
    struct info *prev;

Quote:
}; /* this is a linked structure */

struct info *new_info = 0; /* optionally you can use NULL which is defined in
                              <stdio.h> to be zero anyway */

if(0 == (new_info = (struct info*)malloc(sizeof(struct info)))
    problem();
new_info->next = 0;
new_info->prev = 0;
if(0 == (name = (char*)malloc(sizeof(char)*SIZE)))
    problem();
if(0 == (address = (char*)malloc(sizeof(char)*SIZE)))
    problem();
you would have to #define SIZE to be something

gabor.
--
---------------------------------------------------------------------
No beast so fierce but knows some touch of pity
But I know none, And therefore am no beast
              Richard III., William Shakespeare

C is the language, vim is the editor
---------------------------------------------------------------------



Sat, 28 Nov 1998 03:00:00 GMT  
 Allocating Memory for Structs Item by Item

: Right now, it calls malloc to allocate the memory for the CDInfo struct.
: The CDInfo struct contains two arrays of 50 characters each, to contain
: the artist and title name.

Probably what you want is something like

        struct CDInfo *cdInfo = malloc(sizeof(structCDInfo));

        cdInfo->author = malloc(strlen_of_author_string+1);
                                Allocate at least as much space as the
                                largest string you wish to assign (or
                                allocate just before assigning a string)
                                + space for the zero byte terminator.
                strcpy(cdInfo->author,authour_string);
                                Assign a string to cdInfo->authour.
        cdInfo->title  = malloc(strlen_of_title_string+1);
                strcpy(cdInfo->title,title_string);
--

to claim his strength with claw and tooth. |          Cupertino, California
She wants to rise, to wend from lies,      | (xxx)xxx-xxxx            95015
and meet again the morning's truth.        |         I don't use no smileys



Sat, 28 Nov 1998 03:00:00 GMT  
 Allocating Memory for Structs Item by Item

Quote:

> ...
>What's really stumping me, is the second to last major assignment in the
>book, the one where you're supposed to modify the CDFiler program's memory
>allocation.

>Right now, it calls malloc to allocate the memory for the CDInfo struct.
>The CDInfo struct contains two arrays of 50 characters each, to contain
>the artist and title name.

>I'm supposed to modify the CDInfo struct so it contains POINTERS instead
>of arrays, and call malloc for each string individually as I need it.  I'm
>in it way deep, here!  I took a stab at it, but it's not easy, it appears.
> ...


It would help out a bit if you had included the exact structure of the
CDInfo, but if we improvise a bit we may be able to come up with
something...  assuming you have something along the lines of...

struct CDInfo {
         char artist[50];
         char title[50];

Quote:
};

we could simply allocate the structure via:

   struct CDInfo *myCDInfoPtr = malloc(sizeof(struct CDInfo));

the question is asking you to make the arrays pointers, then allocate
them separately... thus:

struct CDInfo {
         char *artist;
         char *title;

Quote:
};

   then, we have to:

      struct CDInfo *myCDInfoPtr = malloc(sizeof(struct CDInfo));

and then allocate the 50 bytes (or however many you need) for the
title and artist...

      if(myCDInfoPtr != NULL)
      {
        myCDInfoPtr->artist = malloc(50);
        myCDInfoPtr->title = malloc(50);
      }

Dont forget to free them separatly before freeing the structure
itself!

Hope this helps.

-Travis J. Galoppo



Tue, 01 Dec 1998 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How To: Selected Item or Items in Web Browser (in designMode)

2. Disable CTreeCtrl item/items

3. CListCtrl item and sub-item confusion

4. Trying to disable Menu Item and ToolbarCtrl item..

5. Quickest way to dynamically allocate STRUCT memory?

6. Counting items in a struct array

7. Counting items in a struct array

8. how do I add up all item values in a struct

9. deleting an item from an array of struct

10. CListCtrl - is there a way to add extra memory to each item

11. Getting crazy with ListBox Data Items memory leaks

12. allocating space for a struct including struct

 

 
Powered by phpBB® Forum Software