473,756 Members | 7,817 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*)callo c(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 1517
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*)callo c(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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #2
Christopher Benson-Manica <at***@nospam.c yberspace.org> wrote:
Alfonso Morra <sw***********@ the-ring.com> wrote: Nothing wrong here...

data++ = (Element*)callo c(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)cybers pace.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*)callo c(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*)callo c(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4038
by: meyousikmann | last post by:
The following code just sets up and fills a dynamic array of integers. #include <cstdlib> int main() { int* intArray = NULL; int count; count = 20;
4
7697
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something simple, but its just not popping into my mind at the moment. My little snippet of code is below. Basically, the studentID array is dynamic so it will fit any length of a Student's Name. What I'm trying to do is place this chunk of code into a...
5
2246
by: FKothe | last post by:
Hello together, the program below shows a behavior i do not understand. When compiled with the HX-UX11 c-comiler ( version B.11.11.04 ) v2.p in function test_it0 points to an invalid adress and an attempt to write to this pointer causes the program to exit with a core dump. Output after compiling with HP c-compiler: 1. ffffff78 1. 7eff3358
3
2815
by: genc ymeri | last post by:
Hi, What can I use in C# for dynamic arrays ???? I have some records (struts in ..Net) and want to store them in a dynamic "arrays" or object list. I noticed the in C# arrays' length can't be extented in run time. So, what should I use ? Thank You in advance. PS:
4
2471
by: learnfpga | last post by:
Here is a little code I wrote to add the numbers input by the user.....I was wondering if its possible to have the same functionality without using dynamic arrays.....just curious..... //trying to get input from the user to add all the numbers that user inputs //tried to do it without dynamic memory usage but probably cannot achieve it //here is using "new" and "delete" operator....
2
7053
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs, input box for units of service, description of the service and each row has its own dropdown list of unit fees that apply. Each dynamically created row will return 3 values fee1_choice, fee1_unit and fee1_money. Note The above informaton is...
4
5069
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or deleted, however it must be initialized by a function during run-time to contain so many users which each contain so many directories of which each contain so many files. I've completed the program and have it running flawlessly without implementing...
4
4384
by: Bernd Gaertner | last post by:
Dear experts, according to my interpretation of the Standard, loop 1 in the following program is legal, while loop 2 is not (see explanation below). This looks a bit counterintuitive, though; do you know the truth? Thanks a lot in advance, Bernd Gaertner.
4
5427
by: Sunny | last post by:
Hi, Is there a way in javascript to create Dynamic arrays or arrays on fly. Something Like: var "ptsgN"+sd = new Array(); Here sd is incrementing by 1. I have lots of data that I am putting in arrays.
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10040
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3806
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.