LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   malloc (https://www.linuxquestions.org/questions/programming-9/malloc-325060/)

eagle683 05-19-2005 12:29 PM

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

itsme86 05-19-2005 01:41 PM

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));

TheLinuxDuck 05-19-2005 05:01 PM

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;
}


dr_zayus69 05-20-2005 02:24 PM

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.

deiussum 05-20-2005 02:37 PM

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.

itsme86 05-20-2005 02:40 PM

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.

eddiebaby1023 05-22-2005 03:40 PM

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.


All times are GMT -5. The time now is 06:15 PM.