Thread: allocating memory?

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    3

    allocating memory?

    I have a structure which holds 3 float numbers, I'd like to create an array of this structure of size "iNumberVertices" (an integer).
    I'm doing it like this at the moment, but it's not working, could anybody help me out? Here's my code:

    Code:
    struct vect
          { 
          	float x, y, z; 
          };
    
    struct vect pVerts = vect[iNumberVertices];

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    use malloc() to allocate the space. and pVerts must be a pointer.
    Code:
    struct vect* pVerts = malloc(iNumberVertices * sizeof(struct vect));

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You should also include <stdlib.h> for any memory allocation functions (malloc, calloc, realloc, and free).

    And free your malloc()ed memory when you're done with it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  4. allocating memory in constructor
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-25-2004, 07:45 AM
  5. Replies: 5
    Last Post: 11-24-2002, 11:33 PM