LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 03-07-2010, 04:51 PM   #1
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Rep: Reputation: 15
C - How to malloc for a structure


First off, thanks for everyone who's helped me so far. I know I've been asking a lot of questions.

How do you malloc a struct in C? I have a structure of the form:
Code:
typedef struct child_req_to_parent
{
	req_type type;
	child_request req;
}child_req_to_parent;
I basically want to allocate the space using malloc so that I can use free at a later time multiple times. I've been trying to implement it by
Code:
struct child_req_to_parent Router_Buf;
Router_Buf = (Router_Buf*)malloc(sizeof(Router_Buf));
But this is clearly wrong. If someone could help me with using malloc on such a struct it would be great. Thanks!
 
Old 03-07-2010, 04:57 PM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 97
Code:
typedef struct child_req_to_parent
{
	req_type type;
	child_request req;
};

struct child_req_to_parent *Router_Buf;
Router_Buf = (struct child_req_to_parent*)malloc(sizeof(struct child_req_to_parent));

Last edited by troop; 03-07-2010 at 04:58 PM.
 
Old 03-07-2010, 05:00 PM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
You are very close:
Code:
typedef struct
{
	req_type type;
	child_request req;
}child_req_to_parent;
...
child_req_to_parent *pRouter_Buf;
pRouter_Buf = (child_req_to_parent *)malloc(sizeof(child_req_to_parent));
== edit ==
same as above!

Last edited by neonsignal; 03-07-2010 at 05:15 PM.
 
Old 03-07-2010, 05:13 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
You could also use calloc which does the computation "inside." See man calloc for details.
 
Old 03-08-2010, 05:58 AM   #5
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Hey,

you could also write:

Code:
typedef struct
{
	req_type type;
	child_request req;
}child_req_to_parent;
...
child_req_to_parent *pRouter_Buf;
pRouter_Buf = malloc(sizeof(*pRouter_Buf));
The advantage is that could change the type of pRouter_Buf without changing the (m)allocation statement. Though, this reduces the risk of errors caused by source code refactoring AND, you don't have to type too many characaters ... :-)

- Andi -
 
Old 03-08-2010, 12:16 PM   #6
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
Great! Thanks everyone!
 
  


Reply



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
Convert directory structure from long file names in Linux to DOS 8.3 structure? manorina Linux - Software 5 09-12-2009 09:18 AM
Home Jail Folder Structure like Gobolinux Directory Structure luispt Linux - General 3 07-26-2008 06:46 PM
malloc anoosh Programming 1 03-15-2006 04:41 PM
malloc eagle683 Programming 6 05-22-2005 02:40 PM
about malloc eshwar_ind Programming 11 02-18-2004 03:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:12 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration