LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-24-2006, 03:21 PM   #1
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Rep: Reputation: 30
c linked list wiht struct


im trying to create a linked list of strings, using a structure like this...

Code:
struct llitem{
    struct llitem *prev;
    char item[200];
    struct llitem *next;
};
when dynamically creating a new item, i have to allocate the memory, if i do

Code:
malloc(sizeof(struct llitem));
will that allocate the whole item array? or will i have to manually allocate the size i want it? for example, im trying to create a function called ll_add(struct llitem list, char *item), when adding an item how should i allocate the memory needed for the new entry? thanks in advance
 
Old 04-24-2006, 04:13 PM   #2
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
item is a member of the struct, as well as prev and next are, so the size of item is included in sizeof( llitem ).
 
Old 04-24-2006, 04:13 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
It will allocate memory for two pointers plus 200*char. On a 32 bit machine this would be 208 bytes. You can confirm that by printing out teh value of your sizeof.
 
Old 04-24-2006, 04:46 PM   #4
Vagrant
Member
 
Registered: Nov 2001
Posts: 75

Rep: Reputation: 15
I don't think there is a big, if any, difference between what convention you choose. Personally, for this situation I think it makes more sense to use the "new" convention. Remember to deallocate your memory when you no longer need it. So, if you have a delete method, then if you use malloc, use demalloc, if you use the "new" convention, delete your pointer as well.
 
Old 04-24-2006, 05:24 PM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by Vagrant
I don't think there is a big, if any, difference between what convention you choose. Personally, for this situation I think it makes more sense to use the "new" convention. Remember to deallocate your memory when you no longer need it. So, if you have a delete method, then if you use malloc, use demalloc, if you use the "new" convention, delete your pointer as well.
The title is "C..." not C++ so there is no new and delete; also what is this demalloc you talk of? don't you mean "free"?
 
Old 04-24-2006, 05:37 PM   #6
Vagrant
Member
 
Registered: Nov 2001
Posts: 75

Rep: Reputation: 15
Woops. =) Sorry, been in Java land all week.
 
Old 04-24-2006, 05:41 PM   #7
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by Vagrant
Woops. =) Sorry, been in Java land all week.
was it a nice trip lol
 
Old 04-24-2006, 06:21 PM   #8
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Original Poster
Rep: Reputation: 30
i wish it was c++... moving from object based to this really sucks...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Linked List question lowpro2k3 Programming 3 06-16-2005 10:15 AM
linked list + c dilberim82 Programming 5 05-04-2005 11:48 PM
cirular linked list pantera Programming 8 04-21-2005 06:59 AM
C++ linked list fun chens_83 Programming 2 08-04-2003 07:40 AM
book linked list in C jetfreggel Programming 14 03-16-2003 10:52 AM


All times are GMT -5. The time now is 05:41 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration