Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   memory allocation for structure array (http://www.velocityreviews.com/forums/t313999-memory-allocation-for-structure-array.html)

jvax 07-04-2003 03:35 AM

memory allocation for structure array
 
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

Simon Biber 07-04-2003 05:34 AM

Re: memory allocation for structure array
 
"jvax" <jvax@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.



Zoran Cutura 07-04-2003 07:39 AM

Re: memory allocation for structure array
 
jvax <jvax@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 (Zoran.Cutura@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

Micah Cowan 07-05-2003 12:35 AM

Re: memory allocation for structure array
 
jvax@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

siva2152 11-24-2010 11:43 PM

Dereferencing the structure
 
You cannot dereference the structure like
cities[i].x

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


All times are GMT. The time now is 03:58 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.