473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

user-defined class and a dynamically sized array

13
I am trying to figure out how to store and access an array, which is created inside a function, and needs to be accessed outside the function.

My user defined class has set and get functions for 2 values, "value" and "pseudoAddress".

"matrix" is of the user-defined class,
"*address" is a pointer to the user-defined class

I want *address to store the value of matrix, then I want *address to increment by the size of the UD-Class.

Expand|Select|Wrap|Line Numbers
  1. for (int i=0;i<rows;i++){
  2.         for(int j=0; j<columns; j++){
  3.             cin>>value;
  4.             matrix.setValue(value);
  5.  
  6.             matrix.setPseudoAddress(i*(columns)+j);
  7.  
  8.             *address.setValue(matrix.getValue);
  9.  
  10.             *address.setPseudoAddress( matrix.pseudoAddress);
  11.  
  12.             *address++;
  13.  
  14.         }
  15.     }
  16.  
In an earlier version of my code, I had *address= matrix, which the compiler accepted, but when running the code, it crashed when it reached that statement.

Is there a better way (or a way that works) in which I can store my "matrix" values in the "address" pointer, and then increment the pointer? (so the pointer acts as a dynamic array)
Jan 31 '08 #1
1 2775
oler1s
671 Expert 512MB
I am trying to figure out how to store and access an array, which is created inside a function, and needs to be accessed outside the function.
You need to dynamically create it, and then return the pointer. But that's not exactly good programming. Naked dynamic arrays being passed around are not exactly great programming in C++.

Your easiest solution is to use vectors in place of arrays. Boost also has an array wrapper. I wouldn't advise writing your own managed structure.

"*address" is a pointer to the user-defined class
Warning. To refer to the pointer itself, it's just address. *address dereferences the pointer.

Fundamentally, you do not understand pointers and their relationship to arrays. This is troublesome, as your code is fundamentally broken. As you are using C++, I urge you to immediately look into vectors and use them. I'll explain your fundamental misunderstanding as shown by the code. At your leisure, you may then read about the necessary topics in a C++ book.

A pointer is not an array. Never is. It's simply some virtual address of another variable. This means a couple things right off. When you dereference a pointer, you are trying to access the actual memory or whatever referred to by the pointer. When you say *address = matrix, you have just derefenced address. It better be pointing to a valid location. If not, potentially you'll crash the program, as that dereference accesses a bad memory location and the OS terminates your program.

What you would have to do is create a dynamic array using new, which returns a pointer you can then assign to address. You could then access each element of the array like address[0] and address[1], along the lines of

Expand|Select|Wrap|Line Numbers
  1. address[index]->setValue(matrix.getValue);
  2. address[index]->setPseudoAddress( matrix.pseudoAddress);
  3.  
Here, index is some value that indicates a position in the array. And -> is an operator which takes care of the whole (*a).b ugliness (it becomes a->b.
Feb 1 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

60
by: Fotios | last post by:
Hi guys, I have put together a flexible client-side user agent detector (written in js). I thought that some of you may find it useful. Code is here: http://fotios.cc/software/ua_detect.htm ...
3
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
2
by: Jesper Stocholm | last post by:
I have implemented role-based security within my ASP.Net application. However, it seems the role is not passed to the authentication ticket I create. I want to use it to display/hide some...
3
by: Jiho Han | last post by:
Should ASPNET user belong to the local Users group? I may have made some changes that affected my workstation setup and I am experiencing some unexpected behaviors. For example, I have my IIS...
6
by: Andrew Chalk | last post by:
My application attempts to connect to an SQL Server database as name ASPNET and Login Name SERVERNAME/ASPNET in response to these commands: SqlConnection myConnection = new SqlConnection("Data...
4
by: James | last post by:
I have a VB windows forms application that accesses a Microsoft Access database that has been secured using user-level security. The application is being deployed using No-Touch deployment. The...
1
by: Demetri | last post by:
I'm trying to determine if we want to use panels or user controls for our pages. Our primary concern is performance, page loading and posting speed. To illustrate my question, lets use the...
1
by: Carlettus | last post by:
Dear All, sorry but I'm not sure if this is the right place to post my problem. I was using the following asp code to create users in Active Directory. Suddenly, and I don't know the reason, users...
9
by: Gordon | last post by:
I want to add a feature to a project I'm working on where i have multiple users set up on my Postgres database with varying levels of access. At the bare minimum there will be a login user who...
3
by: shapper | last post by:
Hello, On my web site I have a property, Visitor, which is available for Anonymous users: public class Visitor { public CultureInfo Culture { get; set; } public List<GuidPolls { get; set;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.