LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 03-06-2004, 09:32 PM   #1
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Rep: Reputation: 30
Question 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
 
Old 03-07-2004, 03:47 AM   #2
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
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.
 
Old 03-07-2004, 12:10 PM   #3
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Original Poster
Rep: Reputation: 30
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?
 
Old 03-07-2004, 12:15 PM   #4
raxxor
LQ Newbie
 
Registered: Jul 2003
Distribution: Fedora Core 2
Posts: 12

Rep: Reputation: 0
I believe you have to dereference pEntry first, so you would do

Code:
pEntry[1]->buff = NULL;
or

Code:
(*pEntry)[1].buff = NULL;
 
Old 03-07-2004, 06:18 PM   #5
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Original Poster
Rep: Reputation: 30
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
 
Old 03-07-2004, 06:23 PM   #6
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Original Poster
Rep: Reputation: 30
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
 
Old 03-07-2004, 08:52 PM   #7
haobaba1
Member
 
Registered: Jul 2003
Location: VA Tech
Distribution: Mandrake 9.1
Posts: 73

Rep: Reputation: 15
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.
 
Old 03-08-2004, 09:13 AM   #8
Galik
Member
 
Registered: Mar 2003
Location: UK
Distribution: gentoo
Posts: 67

Rep: Reputation: 15
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

Code:
Fields pEntry[1];
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
 
Old 03-08-2004, 01:48 PM   #9
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
see www.boost.org and look up smart pointers. Smart pointers know how many elements are in an array.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c++: array size limit? bigfatdud Programming 14 01-27-2005 11:19 AM
how to find out an array size on c++ poeta_boy Programming 9 06-22-2004 01:28 PM
Size of an array in C Hady Programming 6 04-06-2004 05:33 AM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM
getting size of an array in c++ qanopus Programming 8 11-06-2003 05:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration