Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > memory allocation for structure array

Reply
Thread Tools

memory allocation for structure array

 
 
jvax
Guest
Posts: n/a
 
      07-04-2003
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
 
Reply With Quote
 
 
 
 
Simon Biber
Guest
Posts: n/a
 
      07-04-2003
"jvax" <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
 
Zoran Cutura
Guest
Posts: n/a
 
      07-04-2003
jvax <(E-Mail Removed)> 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 ((E-Mail Removed))
"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
 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      07-05-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
siva2152 siva2152 is offline
Junior Member
Join Date: Nov 2010
Posts: 1
 
      11-24-2010
You cannot dereference the structure like
cities[i].x

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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
Memory allocation in Structure to Structure pra_ramli@rediffmail.com C++ 2 03-09-2006 05:51 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
Memory Allocation for Structure / Class akshay4friend@yahoo.com C++ 2 02-16-2005 04:08 PM
Structure memory allocation Trying_Harder C Programming 4 09-16-2003 02:07 PM



Advertisments