Thread: Putting a set of structs in memory...how?

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    3

    Putting a set of structs in memory...how?

    How can I malloc a set of memory and then put a set of structs into that memory?

    Here is what I mean...let's say I want to put five integers into memory and then retrieve them (of course, I could use an array, linked list, etc.):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void) {
      int * dataset;
      int i;
    
      dataset = malloc ( sizeof (int) * 5 );
    
      *(dataset)   = 9000;
      *(dataset+1) = 10000;
      *(dataset+2) = 11000;
      *(dataset+3) = 12000;
      *(dataset+4) = 13000;
    
      for (i=0; i<5;i++)
        printf ("position %i value %i\n",i,*(dataset+i));
    }
    That works fine. But now let's say instead of integers, I want to put structs in memory. And in fact I have several million of these structs, and I don't know the number ahead of time, so an array is not possible.

    This won't work:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct person_struct {
      char name[20];
      int age;
    };
    
    int main (void) {
      int * dataset;
      int i;
      struct person_struct this_person;
    
      dataset = malloc ( sizeof (struct person_struct) * 5 );
    
      strcpy(this_person.name,"Bob");
      this_person.age = 20;
    
      *(dataset) = this_person; // error: incompatible types in assignment
    }
    Of course, I could malloc each struct and put a pointer to it in dataset, but then I might as well use a linked list or other tree.

    So how can I say "here is a struct, and here is a big pile of memory I've malloc'd. Store that struct at position 10. Now store the next one at position 11. Etc." Then once I have them, I can access them through pointer arithmetic and sort them.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need:
    Code:
    struct person_struct *dataset;

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Access it like an array (equivalent to pointer arithmetic, but clearer):
    Code:
    dataset = malloc(sizeof(*dataset) * 5); // this always makes sure you allocate the right amount of memory if dataset changes types
    dataset[10] = this_person;

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    (forehead slap)

    That was easy. Guess I need to take a break ;-)

    Thanks all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Problem with custom dynamic memory allocation routines
    By BLauritson in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2010, 07:26 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  4. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  5. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM