memory allocation for structure array

J

jvax

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
 
S

Simon Biber

jvax said:
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 said:
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.
 
Z

Zoran Cutura

jvax said:
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.
 
M

Micah Cowan

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
 
Joined
Nov 24, 2010
Messages
1
Reaction score
0
Dereferencing the structure

You cannot dereference the structure like
cities.x

You should be using either this
*(cities).x or cities->x
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top