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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
05-19-2005, 11:29 AM
|
#1
|
|
Member
Registered: May 2005
Posts: 38
Rep:
|
malloc
how would I use malloc with a struct declared like this
struct name
{
int (*vbeg) (void);
...
...
..
}
or would I even have to use malloc, a segmentation fault is occurring, and I think this might be the problem, butI am not using malloc correctly?
any help would be appreciated
Eagle
|
|
|
|
05-19-2005, 12:41 PM
|
#2
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
You don't need to malloc() memory for the function pointer, but if you declare a pointer to an instance of that struct then you might need to malloc() memory for the struct itself. Of course, it's entirely possible to not need malloc() at all. What does your actual code look like?
To malloc() memory for the struct it would look like:
Code:
struct name *ptr = malloc(sizeof(struct name));
|
|
|
|
05-19-2005, 04:01 PM
|
#3
|
|
Member
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349
Rep:
|
Without seeing code, it's hard to say why the binary is segfaulting. In the interim, here's a sample of code for using malloc with a struct:
Code:
#include <stdio.h>
#include <string.h>
struct chess_piece {
unsigned long id;
unsigned char piecename[30];
unsigned char imgpath[80];
};
int main(void)
{
// a struct given compile-time memory allocation
// (does not need malloc)
//
struct chess_piece bishop;
// a struct given dynamic allocation space (needs malloc)
//
struct chess_piece *rook;
printf("Malloc space for struct... ");
// create memory space for rook, don't forget to cast!!
//
rook = (struct chess_piece *)malloc(sizeof(struct chess_piece));
if(rook == NULL) {
perror("Couldn't allocate memory for rook");
return 0;
}
printf("OK\n");
// IMHO, it's always a good idea to blank out a struct
// before use
//
printf("Zeroing out our struct space... ");
memset(&bishop, 0, sizeof(struct chess_piece));
memset(rook, 0, sizeof(struct chess_piece));
printf("OK\n");
printf("Do something with them... ");
// set info into the pieces, print out their info, etc, blah blah
// ...
// ...
printf("OK\n");
// clean up our malloc'd memory
//
printf("Free up malloc'd ram... ");
free(rook);
printf("OK\n");
return 1;
}
|
|
|
|
05-20-2005, 01:24 PM
|
#4
|
|
Member
Registered: Sep 2004
Location: western massachusetts
Distribution: fedora core 3, Suse 10
Posts: 877
Rep:
|
im new to C++ but have been reading extensively about it lately. Couldn't you accomplish the same thing using the new keyword to allocate memory from the heap/free store (whatever you like to call it) And i think that would make it more portabilty because new is in C++ and malloc() is only a unix function right?
edit: sorry just noticed above with the printf() that you are refering to C and not C++ but structs are in both C and C++ and the original poster didn't say which he was using - may want to do that in the future so you don't confuse idiots like me. lol.
Last edited by dr_zayus69; 05-20-2005 at 01:26 PM.
|
|
|
|
05-20-2005, 01:37 PM
|
#5
|
|
Member
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895
Rep:
|
Are you calling your function pointer there before assiging it to a function? That could cause a segfault.
Post some actual code that is causing the segfault. Without seeing that, all anyone can do is guess.
|
|
|
|
05-20-2005, 01:40 PM
|
#6
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
Quote:
|
malloc() is only a unix function right?
|
Certainly not. If your compiler doesn't support malloc() then it does not comply to the C (or C++) standard.
|
|
|
|
05-22-2005, 02:40 PM
|
#7
|
|
Member
Registered: May 2005
Posts: 378
Rep:
|
Pointers are the single most confusing area of C for newcomers. They see a definition like "struct *stat" as an argument for a system call, so they declare it as "struct *stat", start using it and wonder why their program SEGVs. They need to learn the difference between a pointer (which is storage that may point somewhere, and data storage which exists but is only implicitly pointed to (by its name). Once you get the hang of it, it's very straightforward, but there's often some pain (and core files!) getting there.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:45 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|