C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Noob Question

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language (Moderated)
View previous topic :: View next topic  
Author Message
Ricardo Gomes
Guest





PostPosted: Thu Mar 29, 2007 6:27 am    Post subject: Noob Question Reply with quote



Hello

I've got a question.

I want to create a array of strutures (i know i should do a linked
list, but i can't) using as litle resources as possible.

This is my idea:

#define MAXARRAY 100

typedef struct{

.... members ...

} DATASTRUCT;

insertMember(DATASTRUCT *p){

*p[1]->member1 = something;
*p[1]->member2 = something;
..
..
..
}


int main(void){

DATASTRUCT *struct_array[MAXARRAY];
int i;

for (i=0;i<MAXARRAY;i++){
struct_array = malloc(sizeof(struct_array) +
(1*sizeof(DATASTRUCT)));
insertMember(struct_array);
}

}

Of course de for is an example, what I want is to pass the array of
pointers to a function, but only reserving memory when I need it.

I don't know if I made myself clear.

Thanks in advanced for all your help.

--
Ricardo Gomes
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Jonas
Guest





PostPosted: Mon Apr 16, 2007 8:18 pm    Post subject: Re: Noob Question Reply with quote



"Ricardo Gomes" <rgcaldas (AT) gmail (DOT) com> wrote in message
news:clcm-20070328-0013 (AT) plethora (DOT) net...
Quote:
Hello

I've got a question.

I want to create a array of strutures (i know i should do a linked
list, but i can't) using as litle resources as possible.

Yes, you might want to use a linked list. Try a search:
http://www.google.com/search?q=c+tutorial+linked+list. A vector might also
be a good choice though, depending on how you want to access the elements.

Quote:

This is my idea:

#define MAXARRAY 100

typedef struct{

... members ...

} DATASTRUCT;

insertMember(DATASTRUCT *p){

*p[1]->member1 = something;
*p[1]->member2 = something;
.
.
.
}


int main(void){

DATASTRUCT *struct_array[MAXARRAY];
int i;

for (i=0;i<MAXARRAY;i++){
struct_array = malloc(sizeof(struct_array) +
(1*sizeof(DATASTRUCT)));
insertMember(struct_array);
}

}

Of course de for is an example, what I want is to pass the array of
pointers to a function, but only reserving memory when I need it.


If you want to use a static array of pointers, you need to keep track of
allocated elements:

typedef struct vector_st {
DATASTRUCT *array[MAXARRAY];
size_t allocated;
} array_t;

void add_element(array_t *array, DATASTRUCT *data)
{
assert (array->allocated < MAXARRAY);
array->array[array->allocated++] = data;
}

int main(void)
{
unsigned i;
array_t my_array;
my_array.allocated = 0;

for (i = 0; i < MAXARRAY; i++) {
DATASTRUCT *data;
/* allocate one element: */
data = malloc (sizeof(DATASTRUCT));

/* initialize data: */
/* data->member1 = ... */

/* add element to array: */
add_element (&my_array, data);
}

/* to access the member of an element: */
/* my_array.array[5]->member1 */
}

Starting with this, you'd probably want to define functions that let you
create and free an array, delete and find array elements etc. There are
plenty of books on data structures out there, I recommend you pick one up.

/Jonas



Quote:
I don't know if I made myself clear.

Thanks in advanced for all your help.

--
Ricardo Gomes
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line. Sorry.
--

comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Ricardo Gomes
Guest





PostPosted: Mon Apr 23, 2007 10:59 pm    Post subject: Re: Noob Question Reply with quote



On Apr 16, 4:16 pm, Barry Schwarz <schwa...@doezl.net> wrote:
Quote:
On 29 Mar 2007 01:27:06 GMT, "Ricardo Gomes" <rgcal...@gmail.com
wrote:

Hello

I've got a question.

I want to create a array of strutures (i know i should do a linked
list, but i can't) using as litle resources as possible.

Why can't you



This is my idea:

#define MAXARRAY 100

typedef struct{

... members ...

} DATASTRUCT;

insertMember(DATASTRUCT *p){

*p[1]->member1 = something;

p1 is a pointer to struct. *p is a struct. p[i] is a struct. *p[i]
is a syntax error. You can code
p[1].member1 ...

OK, got it, thanks

Quote:
*p[1]->member2 = something;
.
.
.
}

int main(void){

DATASTRUCT *struct_array[MAXARRAY];
int i;

for (i=0;i<MAXARRAY;i++){
struct_array = malloc(sizeof(struct_array) +
(1*sizeof(DATASTRUCT)));

struct_array is an array. It cannot appear in the left of an
assignment.

insertMember(struct_array);

In this context, struct_array evaluates to an address with type
DATASTRUCT**. insertMember expects an argument of type DATASTRUCT*.
The two are incompatible and there is no implicit conversion between
them.

}

}

Of course de for is an example, what I want is to pass the array of
pointers to a function, but only reserving memory when I need it.

If you want to pass an array of pointers to the function, then declare
the function as accepting a array of pointers. Don't try to get all
fancy with the notion that array parameters are really pointers. It's
true but there is no reason not to make your code mnemonically
meaningful.



I don't know if I made myself clear.

Do you really think that defining an array of pointers that will exist
for the life of your program takes less memory than allocating each
struct as you need it with each one pointing to the next. It is only
after allocating all 100 structures that you break even; you never get
ahead.

Yes I know, but this is a school assignment and for the moment I can't
use Linked Lists

Thanks for all you help

Quote:

Remove del for email
--
comp.lang.c.moderated - moderation address: c...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
--

comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Ricardo Gomes
Guest





PostPosted: Mon Apr 23, 2007 10:59 pm    Post subject: Re: Noob Question Reply with quote

On Apr 16, 4:16 pm, "bytebro" <keith.wil...@aah.co.uk> wrote:
Quote:
I want to create a array of strutures (i know i should do a
linked list, but i can't) using as litle resources as possible.

You're right; it should be a linked list. When you say "can't", is
that "don't know how to" or "not allowed to"?

Yes, I'm not allowed to use Linked List yet.

Quote:
If you must use your alternative then you allocated the array of
pointers once, up front, and initialise all elements to NULL. Then
allocate each member struct when needed and store its address in the
array of pointers in the next empty (NULL) space.

This seams a very good solution, I'm going to try this

Quote:
Don't forget to free the members first then the pointer array when
you're done.
--
comp.lang.c.moderated - moderation address: c...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
--

comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Ricardo Gomes
Guest





PostPosted: Mon Apr 23, 2007 10:59 pm    Post subject: Re: Noob Question Reply with quote

On Apr 16, 4:18 pm, "Jonas" <spamhereple...@gmail.com> wrote:
Quote:
"Ricardo Gomes" <rgcal...@gmail.com> wrote in message

news:clcm-20070328-0013 (AT) plethora (DOT) net...

Hello

I've got a question.

I want to create a array of strutures (i know i should do a linked
list, but i can't) using as litle resources as possible.

Yes, you might want to use a linked list. Try a search:http://www.google.com/search?q=c+tutorial+linked+list. A vector might also
be a good choice though, depending on how you want to access the elements.





This is my idea:

#define MAXARRAY 100

typedef struct{

... members ...

} DATASTRUCT;

insertMember(DATASTRUCT *p){

*p[1]->member1 = something;
*p[1]->member2 = something;
.
.
.
}

int main(void){

DATASTRUCT *struct_array[MAXARRAY];
int i;

for (i=0;i<MAXARRAY;i++){
struct_array = malloc(sizeof(struct_array) +
(1*sizeof(DATASTRUCT)));
insertMember(struct_array);
}

}

Of course de for is an example, what I want is to pass the array of
pointers to a function, but only reserving memory when I need it.

If you want to use a static array of pointers, you need to keep track of
allocated elements:

typedef struct vector_st {
DATASTRUCT *array[MAXARRAY];
size_t allocated;

} array_t;

void add_element(array_t *array, DATASTRUCT *data)
{
assert (array->allocated < MAXARRAY);
array->array[array->allocated++] = data;

}

int main(void)
{
unsigned i;
array_t my_array;
my_array.allocated = 0;

for (i = 0; i < MAXARRAY; i++) {
DATASTRUCT *data;
/* allocate one element: */
data = malloc (sizeof(DATASTRUCT));

/* initialize data: */
/* data->member1 = ... */

/* add element to array: */
add_element (&my_array, data);
}

/* to access the member of an element: */
/* my_array.array[5]->member1 */

}

Starting with this, you'd probably want to define functions that let you
create and free an array, delete and find array elements etc. There are
plenty of books on data structures out there, I recommend you pick one up.

/Jonas

First, thanks for your help.

I bought 2 books on data structures in C, the problem is that I have
to code wrongly since I can't use Linked Lists.

I will try you suggestions .

Again Thanks a lot

Quote:

I don't know if I made myself clear.

Thanks in advanced for all your help.

--
Ricardo Gomes
--
comp.lang.c.moderated - moderation address: c...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line. Sorry.

--
comp.lang.c.moderated - moderation address: c...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
--

comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Jonas
Guest





PostPosted: Sat Apr 28, 2007 1:58 am    Post subject: Re: Noob Question Reply with quote

"Ricardo Gomes" <rgcaldas (AT) gmail (DOT) com> wrote in message
news:clcm-20070423-0006 (AT) plethora (DOT) net...
Quote:
On Apr 16, 4:18 pm, "Jonas" <spamhereple...@gmail.com> wrote:
"Ricardo Gomes" <rgcal...@gmail.com> wrote in message

news:clcm-20070328-0013 (AT) plethora (DOT) net...

Hello

I've got a question.

I want to create a array of strutures (i know i should do a linked
list, but i can't) using as litle resources as possible.


<snip>

Quote:

First, thanks for your help.

I bought 2 books on data structures in C, the problem is that I have
to code wrongly since I can't use Linked Lists.

<OT>

Your question might be received better in e.g. alt.comp.lang.learn.c-c++.

Anyway, not using linked lists is not "to code wrongly". Linked lists are
good when you need to insert elements quickly, but don't care about their
order, or only care about which element is first and/or last. Thus, you use
linked lists to implement stacks, queues and dequeues.

If beign able to access *any* element in the list/array (random access) is
important, then a linked list is a bad choice, as you have to step through
the list to find your element. A dynamically allocated vector that grows and
shrinks to fit the amount of data is then a much better choice.

</OT>

--
Jonas
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language (Moderated) All times are GMT
Page 1 of 1

 
 


Powered by phpBB © 2001, 2006 phpBB Group