memory allocation for structure array

Discussion in 'C Programming' started by jvax, Jul 4, 2003.

  1. jvax

    jvax Guest

    Hi,
    I'm new to C programming and am having the hardest time trying to
    figure out how to allocate memory for a structure array. Can somebody
    please see if I'm doing things correctly? My code is as follows.

    typedef struct s_city{
    double x, y;
    } CITY;

    ....

    CITY *cities;

    cities = (struct s_city *)malloc(numberOfCities * sizeof(struct
    s_city));

    ....

    later in the code, when I try to do

    for(i=0; i<numberOfCities; i++) {
    cities.x = someX;
    cities.y = someY;
    }

    I get a segmentation fault.

    I really don't know what I am doing wrong. Help me please.
    Thank you in advance.

    -- jvax
    jvax, Jul 4, 2003
    #1
    1. Advertising

  2. jvax

    Simon Biber Guest

    "jvax" <> wrote:
    > I'm new to C programming and am having the hardest time
    > trying to figure out how to allocate memory for a
    > structure array. Can somebody please see if I'm doing
    > things correctly? My code is as follows.


    No, it's little snippets. Reduce the problem to a small
    compilable program then we can actually help you, rather than
    trying to guess what you aren't showing us. Make sure it is
    indented consistently, using only spaces, no tab characters.

    #include <stdlib.h>

    > typedef struct s_city{
    > double x, y;
    > } CITY;
    > CITY *cities;
    > cities = (struct s_city *)malloc(numberOfCities *
    > sizeof(struct s_city));


    Make it
    cities = malloc(numberOfCities * sizeof *cities);
    this is shorter, easier to read and will work even if the type
    of cities is changed.

    Make sure you check the returned value, if malloc failed then
    it will be a null pointer.

    > for(i=0; i<numberOfCities; i++) {
    > cities.x = someX;
    > cities.y = someY;
    > }
    >
    > I get a segmentation fault.


    The only thing in this code that could cause that segmentation
    fault is if malloc failed and your code
    cities
    attempted to use that null pointer. I think the error is
    probably in something you didn't show.

    --
    Simon.
    Simon Biber, Jul 4, 2003
    #2
    1. Advertising

  3. jvax

    Zoran Cutura Guest

    jvax <> wrote:
    > Hi,
    > I'm new to C programming and am having the hardest time trying to
    > figure out how to allocate memory for a structure array. Can somebody
    > please see if I'm doing things correctly? My code is as follows.
    >
    > typedef struct s_city{
    > double x, y;
    > } CITY;
    >
    > ...
    >
    > CITY *cities;
    >
    > cities = (struct s_city *)malloc(numberOfCities * sizeof(struct
    > s_city));


    No need to cast mallocs return value.

    cities = malloc(numberOfCities * sizeof *cities);

    >
    > ...
    >
    > later in the code, when I try to do
    >
    > for(i=0; i<numberOfCities; i++) {
    > cities.x = someX;
    > cities.y = someY;
    > }
    >
    > I get a segmentation fault.
    >
    > I really don't know what I am doing wrong. Help me please.


    The problem doesn't look to stem from the presented code, there
    must be something wrong elsewhere in your program.

    --
    Z ()
    "LISP is worth learning for the profound enlightenment experience
    you will have when you finally get it; that experience will make you
    a better programmer for the rest of your days." -- Eric S. Raymond
    Zoran Cutura, Jul 4, 2003
    #3
  4. jvax

    Micah Cowan Guest

    (jvax) writes:

    > Hi,
    > I'm new to C programming and am having the hardest time trying to
    > figure out how to allocate memory for a structure array. Can somebody
    > please see if I'm doing things correctly? My code is as follows.
    >
    > typedef struct s_city{
    > double x, y;
    > } CITY;
    >
    > ...
    >
    > CITY *cities;
    >
    > cities = (struct s_city *)malloc(numberOfCities * sizeof(struct
    > s_city));


    The cast doesn't really add anything: your sizeof useof could lead to
    problems if you ever decide to change the type of cities. Better could
    be:

    cities = malloc(numberOfCities * (sizeof *cities));

    > later in the code, when I try to do
    >
    > for(i=0; i<numberOfCities; i++) {
    > cities.x = someX;
    > cities.y = someY;
    > }
    >
    > I get a segmentation fault.


    Did you remember to check the result of malloc() to make sure it
    allocated successfully? ...I can't think why you'd get a segfault
    here, but without seeing the rest of the code...

    Why don't you post the minimal amount of compilable code which
    demonstrates the problem you're having? (That is a standard
    expectation here).

    -Micah
    Micah Cowan, Jul 5, 2003
    #4
  5. jvax

    siva2152

    Joined:
    Nov 24, 2010
    Messages:
    1
    Dereferencing the structure

    You cannot dereference the structure like
    cities.x

    You should be using either this
    *(cities).x or cities->x
    siva2152, Nov 24, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    3,082
    Akshay
    Feb 16, 2005
  2. Trying_Harder

    Structure memory allocation

    Trying_Harder, Sep 16, 2003, in forum: C Programming
    Replies:
    4
    Views:
    7,790
    Irrwahn Grausewitz
    Sep 16, 2003
  3. Ken
    Replies:
    24
    Views:
    3,859
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    987
    chris
    Oct 28, 2005
  5. Replies:
    2
    Views:
    601
Loading...

Share This Page