Thread: Creating a pointer to an array of structs

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    1

    Creating a pointer to an array of structs

    Hello, I was wondering if someone could please point out the incorrect use of syntax below, I want to create a pointer to an array of structs and then malloc that amount of memory....I currently have:


    Particle *pParticle[MAX_NUMBER_PARTICLES];

    pParticle = (Particle*)(malloc(MAX_NUMBER_PARTICLES * sizeof(Particle)));


    Should I use a type of loop to malloc each array element in turn instead?

    Thanks again.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    Two options

    First one

    Particle *pParticle[MAX_NUMBER_PARTICLES];

    for(i=0;i<MAX_NUMBER_PARTICLES;i++)
    pParticle[i] = (Particle*)(malloc(sizeof(Particle)));

    second one

    Particle *pParticle;

    pParticle = (Particle*)(malloc(MAX_NUMBER_PARTICLES * sizeof(Particle)));

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    But an array is a pointer... so it would just be:

    Code:
    Particle **pParticle;
    ( *pParticle ) = malloc( sizeof( Particle * MAX_NUM_PARTICLES ) );
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >But an array is a pointer

    The penalty for repeating such heresy is to review this.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    King of the Internet Fahrenheit's Avatar
    Join Date
    Oct 2001
    Posts
    128
    Originally posted by Dave_Sinkula
    >But an array is a pointer

    The penalty for repeating such heresy is to review this.
    Code:
    char abc[5] ;
    abc POINTS to statically allocated memory, correct?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >( *pParticle ) = malloc( sizeof( Particle * MAX_NUM_PARTICLES ) );
    This is incorrect. The multiplication should not be an expression for the sizeof operator. This is what you want:
    Code:
    malloc( sizeof( Particle ) * MAX_NUM_PARTICLES );
    Let's move to the other side of the assignment operator for a moment:
    Code:
    ( *pParticle )
    What does pParticle point to? If you said "Nothing predictable, and certainly nothing this program owns" you get a cookie.

    >But an array is a pointer...
    ...when used as an expression.
    My best code is written with the delete key.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Sorry 'bout that. I missed the closing parentheses on the sizeof( ) statement.

    So how would you do it? My way was just a guess.

    Would you have to do pParticle = malloc( sizeof( Particle * ) ) first?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Would you have to do pParticle = malloc( sizeof( Particle * ) ) first?
    It depends on what you want. If you want a dynamic array of pointers to Particle (as seems likely) then you would do this:
    Code:
    pParticle = malloc ( N * sizeof *pParticle );
    Where N is the number of items you want in the array. You can use sizeof ( Particle * ), but that tends to confuse people, so taking the size of *p where p is the pointer being assigned to is better. It also improves maintainablility.
    My best code is written with the delete key.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > abc POINTS to statically allocated memory, correct?
    No it doesn't point to the memory, it is the memory allocated to the array, and abc is the name by which those 5 chars are known.

    In some contexts, the name "abc" gets converted into "a pointer to the first element", but that doesn't affect the array itself - for example
    strcpy( abc, "yes" );
    But then you could also write
    strcpy( &abc[0], "yes" );

    int foo = 2;
    foo is the memory location containing 2, not a pointer to a memory location containing 2.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. Replies: 19
    Last Post: 07-20-2007, 01:46 AM
  3. pointer to multidimensional array
    By Bigbio2002 in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2006, 10:29 PM
  4. Replies: 41
    Last Post: 07-04-2004, 03:23 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM