Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-06-2004, 09:32 PM
|
#1
|
|
Member
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420
Rep:
|
size of an array
hi
im having some trouble with finding the size of an array,
for example:
struct MyStruct{
int num;
};
MyStruct *ms = new MyStruct[1];
now what i want it do do is find out how many arrays ms has and adds 1 array to it
so it would look like this
ms[2];
//find the size + 1
ms[3];
//find the size + 1
ms[4];
// and so on....
any suggestions?
thanks
|
|
|
|
03-07-2004, 03:47 AM
|
#2
|
|
Member
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128
Rep:
|
I have two suggestions, but they both may involve building the array a different way. The first is to always create a NULL element at the end of an array, then you can just traverse the array until you hit a NULL element. This is very widely used throughout the system, and a lot of people use it in code. The other suggestion is to use a container structure. The structure has two elements: the array and a count of the array size ( if you use it inclusively then you can use the count looping comparisons and so forth ). As there is no guarantee as to what a given point in memory will contain on the heap ( ie, they may or may not be zeroed pages at the point of use ), I do not think you can rely on the actual array itself. There are other methods of approaching this, but I like these two.
|
|
|
|
03-07-2004, 12:10 PM
|
#3
|
|
Member
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420
Original Poster
Rep:
|
hrmmm ok
but i get an error or something:
here is my source:
typedef struct{
char buff;
short ID;
string Name;
string Number;
string Address;
}Fields;
//Create object of type Fields
//Fields Entry;
Fields* pEntry = new Fields[1];
pEntry[1].buff = NULL;
and i get an error on the line with
pEntry[1].buff = NULL;
it says i have an error before the '.' (period)
any suggestions?
|
|
|
|
03-07-2004, 12:15 PM
|
#4
|
|
LQ Newbie
Registered: Jul 2003
Distribution: Fedora Core 2
Posts: 12
Rep:
|
I believe you have to dereference pEntry first, so you would do
Code:
pEntry[1]->buff = NULL;
or
Code:
(*pEntry)[1].buff = NULL;
|
|
|
|
03-07-2004, 06:18 PM
|
#5
|
|
Member
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420
Original Poster
Rep:
|
bahh i still get the same errors except for the first one it says i have a syntax before the '->' token
hrrmmmm
any suggestions? haha
|
|
|
|
03-07-2004, 06:23 PM
|
#6
|
|
Member
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420
Original Poster
Rep:
|
the thing is that,
pEntry.[1].buff = NULL;
is in the global scope
but when i move that line into any of my functions or into main(), it works just fine O.o
the thing is, i need it in the global scope! hahah
yeah this is realy weird
|
|
|
|
03-07-2004, 08:52 PM
|
#7
|
|
Member
Registered: Jul 2003
Location: VA Tech
Distribution: Mandrake 9.1
Posts: 73
Rep:
|
I might be tripping out, its been a while since I coded in c or c++, but I believe that this:
Fields* pEntry = new Fields[1];
pEntry[1].buff = NULL;
is equivalent to
Fields* pEntry = new Fields;
pEntry[1].buff = NULL;
It looks to me like you are creating a array size 1 and then trying to dereference it at position 1 which is the second position in the array. This would cause a segmentation fault or illegal operation.
Also it looks like you want an array of pointer objects but you are creating an array of objects so your declaration should look more like the following:
Fields **array;
array = new *Fields[some number hopefully larger than 1];
array[i]= &someObject; or NULL;
then you access it like:
array[i]->dataMember= something;
Last edited by haobaba1; 03-07-2004 at 08:56 PM.
|
|
|
|
03-08-2004, 09:13 AM
|
#8
|
|
Member
Registered: Mar 2003
Location: UK
Distribution: gentoo
Posts: 67
Rep:
|
Quote:
Originally posted by Longinus
the thing is that,
pEntry.[1].buff = NULL;
is in the global scope
but when i move that line into any of my functions or into main(), it works just fine O.o
the thing is, i need it in the global scope! hahah
yeah this is realy weird
|
The thing is in C the global scope is only for declarations and not for program code. All program code must be in a function (like main). Hence it is legal to say
In the global space but not
Code:
Fields* pEntry = new Fields[1];;
because the new operator is executable code and not compiler declaration.
What you probably want is something like
Code:
typedef struct
{
char buff;
short ID;
string Name;
string Number;
string Address;
} Fields;
//Create object of type Fields
//Fields Entry;
Fields* pEntry;
int main(int argc, char**argv)
{
// Initialise the array structure
pEntry = new Fields[1]; // Start with 1 element
pEntry[0].buff = NULL; // Assign NULL to the first element (note index 0!!)
// ... use and extend array
// dispose
delete pEntry;
}
Or something like that
|
|
|
|
03-08-2004, 01:48 PM
|
#9
|
|
Member
Registered: May 2002
Posts: 964
Rep:
|
see www.boost.org and look up smart pointers. Smart pointers know how many elements are in an array.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:53 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|