469,946 Members | 1,901 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,946 developers. It's quick & easy.

Adress arithmetic and dynamic arrays

Hi,

I'm trying to write a simple generic container. I have the ff:

typedef union {
long int_val ;
char* string_val ;
double dbl_val ;
void* vptr_val ;
}Element ;

I want to be able to store vars of type Element in a dynamic (i.e.
resizable) array . I tried the ff:
#define MAX_NUM 3

int main(int argc, char* argv[]) {

Element* data ;
int i ;

for (i=0;i< MAX_NUM; i++) {
data++ = (Element*)calloc(1, sizeof(int)) ;
*(data) = i ;
}

for (i=0;i< MAX_NUM; i++)
printf("Element %d has value : %d", i,*(data[i])) ;

for (i=0;i< MAX_NUM; i++)
free(data[i]) ;
}

It is obviously wrong (illegal indirection etc). This is schoolboy stuff
and embarassingly, I've forgotten how to fix it! (using C++ and STL
libraries for too long!).

Any help or pointers (pun intended) much appreciated

Nov 15 '05 #1
4 1376
Alfonso Morra <sw***********@the-ring.com> wrote:
typedef union {
long int_val ;
char* string_val ;
double dbl_val ;
void* vptr_val ;
}Element ;
Nothing wrong here...
#define MAX_NUM 3 int main(int argc, char* argv[]) { Element* data ;
int i ; for (i=0;i< MAX_NUM; i++) {
data++ = (Element*)calloc(1, sizeof(int)) ;
It's neither necessary nor advisable to cast the return value of
*alloc in this language (forgivable since you've become used to doing
it in C++, from the sound of it).
*(data) = i ;
}
What you really wanted was something like

/* Allocate enough space for MAX_NUM Elements */

data=malloc( MAX_NUM * sizeof *data ); /* check for NULL */
for( i=0; i < MAX_NUM; i++ ) {
data[i].int_val=i;
}

sizeof Element is by no means guaranteed to be sizeof int; an Element
is at least as large as its largest member, which in this case is
most likely not the int_val. Notice also that you must assign to the
int_val member of the union.
for (i=0;i< MAX_NUM; i++)
printf("Element %d has value : %d", i,*(data[i])) ;
printf( "Element %d has value: %ld\n", i, data[i].int_val );
for (i=0;i< MAX_NUM; i++)
free(data[i]) ;
free( data );
}


I hope that helped.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #2
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Alfonso Morra <sw***********@the-ring.com> wrote: Nothing wrong here...

data++ = (Element*)calloc(1, sizeof(int)) ; printf("Element %d has value : %d", i,*(data[i])) ;


I meant to add that you also neglected to include (perhaps not in your
real code) <stdio.h> and <stdlib.h>, which is as big a problem as it
would have been in C++.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #3


Christopher Benson-Manica wrote:
Alfonso Morra <sw***********@the-ring.com> wrote:

typedef union {
long int_val ;
char* string_val ;
double dbl_val ;
void* vptr_val ;
}Element ;

Nothing wrong here...

#define MAX_NUM 3


int main(int argc, char* argv[]) {


Element* data ;
int i ;


for (i=0;i< MAX_NUM; i++) {
data++ = (Element*)calloc(1, sizeof(int)) ;

It's neither necessary nor advisable to cast the return value of
*alloc in this language (forgivable since you've become used to doing
it in C++, from the sound of it).

*(data) = i ;
}

What you really wanted was something like

/* Allocate enough space for MAX_NUM Elements */

data=malloc( MAX_NUM * sizeof *data ); /* check for NULL */
for( i=0; i < MAX_NUM; i++ ) {
data[i].int_val=i;
}

sizeof Element is by no means guaranteed to be sizeof int; an Element
is at least as large as its largest member, which in this case is
most likely not the int_val. Notice also that you must assign to the
int_val member of the union.

for (i=0;i< MAX_NUM; i++)
printf("Element %d has value : %d", i,*(data[i])) ;

printf( "Element %d has value: %ld\n", i, data[i].int_val );

for (i=0;i< MAX_NUM; i++)
free(data[i]) ;

free( data );

}

I hope that helped.

Thanks Christopher

Nov 15 '05 #4
On Fri, 16 Sep 2005 16:17:13 +0000 (UTC), Alfonso Morra
<sw***********@the-ring.com> wrote:
Hi,

I'm trying to write a simple generic container. I have the ff:

typedef union {
long int_val ;
char* string_val ;
double dbl_val ;
void* vptr_val ;
}Element ;

I want to be able to store vars of type Element in a dynamic (i.e.
resizable) array . I tried the ff:
#define MAX_NUM 3

int main(int argc, char* argv[]) {

Element* data ;
At this point, data is uninitialized.
int i ;

for (i=0;i< MAX_NUM; i++) {
data++ = (Element*)calloc(1, sizeof(int)) ;
First a syntax error. data++ is not a modifiable lvalue and so may
not appear on the left of an assignment.

If it could, you would be invoking undefined behavior. Since data is
uninitialized, you cannot increment it.

And finally a "style" issue. Casting the return from calloc rarely
helps but it does cause the compiler to suppress some important
diagnostics if you forget to place a prototype in scope.
*(data) = i ;
Another syntax error. data is a pointer to union. *data is an actual
union. i is an int. You cannot assign an int to a union. You must
assign the value to one of the members of the union, in this case
either int_val or dbl_val.
}

for (i=0;i< MAX_NUM; i++)
printf("Element %d has value : %d", i,*(data[i])) ;
data is a pointer to union. data[i] is the i-th union pointed to. The
dereference operator is defined to work only on pointers. data[i] is
not a pointer but an actual union.
for (i=0;i< MAX_NUM; i++)
free(data[i]) ;
}

It is obviously wrong (illegal indirection etc). This is schoolboy stuff
and embarassingly, I've forgotten how to fix it! (using C++ and STL
libraries for too long!).

Any help or pointers (pun intended) much appreciated


I think you want data to be a pointer to some number of pointers to
union. Then you want to allocate space for data to point to. Then
you want to allocate space for each pointer in the space to point to.
This last allocation will create space for each of the unions.
<<Remove the del for email>>
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by meyousikmann | last post: by
4 posts views Thread by Scott Lyons | last post: by
5 posts views Thread by FKothe | last post: by
3 posts views Thread by genc ymeri | last post: by
4 posts views Thread by learnfpga | last post: by
4 posts views Thread by Bernd Gaertner | last post: by
4 posts views Thread by Sunny | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.