ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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;
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;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.