By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,900 Members | 1,275 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,900 IT Pros & Developers. It's quick & easy.

memory allocation for structure array

P: n/a
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[i].x = someX;
cities[i].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
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"jvax" <jv**@hotmail.com> 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[i].x = someX;
cities[i].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[i]
attempted to use that null pointer. I think the error is
probably in something you didn't show.

--
Simon.
Nov 13 '05 #2

P: n/a
jvax <jv**@hotmail.com> 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[i].x = someX;
cities[i].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 (Zo**********@daimlerchrysler.com)
"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
Nov 13 '05 #3

P: n/a
jv**@hotmail.com (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[i].x = someX;
cities[i].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
Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.