Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 05-19-2005, 12:29 PM   #1
Registered: May 2005
Posts: 38

Rep: Reputation: 15

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

Old 05-19-2005, 01:41 PM   #2
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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:
struct name *ptr = malloc(sizeof(struct name));
Old 05-19-2005, 05:01 PM   #3
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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:
#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;


  //  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("Do something with them... ");
  //  set info into the pieces, print out their info, etc, blah blah
  //  ...
  //  ...

  //  clean up our malloc'd memory
  printf("Free up malloc'd ram... ");

  return 1;
Old 05-20-2005, 02:24 PM   #4
Registered: Sep 2004
Location: western massachusetts
Distribution: fedora core 3, Suse 10
Posts: 877

Rep: Reputation: 35
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 02:26 PM.
Old 05-20-2005, 02:37 PM   #5
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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.
Old 05-20-2005, 02:40 PM   #6
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
Old 05-22-2005, 03:40 PM   #7
Registered: May 2005
Posts: 378

Rep: Reputation: 33
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
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
problem using malloc in C huble Programming 10 03-27-2005 01:45 AM
malloc in c programming saiz66 Programming 18 10-04-2004 03:00 PM
malloc() vijeesh_ep Programming 4 08-25-2004 04:50 PM
malloc debugger legolas_t Programming 3 07-04-2004 02:32 PM
about malloc eshwar_ind Programming 11 02-18-2004 04:41 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:18 AM.

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