Page 1 of 1

c question

Posted: Thu Jan 23, 2003 4:15 pm
by elias
if i have the following line in a header file im going to include:

account* total_accounts[1000]; (account is a data strucuture i created)

then will space be allocated for 1000 pointers to account structures at runtime? or does it just declare a variable?

Re:c question

Posted: Thu Jan 23, 2003 4:33 pm
by creichen
Hi,

this will create a global array of "statically allocated" memory in each object file generated from a C file including this header. in practice, this means that you'll get conflicts when trying to link these, since each will have its on variant of these and the linker can't decide which one to use.

In order to resolve this, label the structure as 'extern'; this will introduce it as a valid identifier (and specify its type), but won't allocate space for it. If you compile it this way, the linker will complain because you're now lacking a concrete definition of it, but this can be resolved by picking one C file and adding an additional, non-extern definition of it.

(There are certain preprocessor tricks to work around the neccessity to "double-define" these things, but I won't get into that here).

-- Christoph

Re:c question

Posted: Thu Jan 23, 2003 4:34 pm
by ark
that declaration will allocate space for 1000 pointers to account structures; however, doing that in a header file might give you redefinition problems; you might want to look into the extern keyword

Re:c question

Posted: Thu Jan 23, 2003 4:46 pm
by elias
only functions in the header file are going to be using this array. when someone creates an account, space will be allocated with malloc, and this will return a pointer to the structure. then this pointer will be put into the array. but if the array dosnt exist, then i cant do this. thats why i wanted to know if that statement itself will allocate space for 1000 continguous points to stuct account. i havnt really done much in C, and nothign succesful. the first i learned was OOP Java, so im probably confusing some concepts.

Re:c question

Posted: Thu Jan 23, 2003 4:51 pm
by ark
if only the functions in the header file will be using the array, then declare the array inside the .c file where the functions are implemented instead of in the header file. You can do what you are trying to do, and you have the right idea, if I understand you correctly.

Re:c question

Posted: Thu Jan 23, 2003 4:55 pm
by elias
well does it really matter it its .h or .c? i thgouth that was jsut a convenience for programmers. the functions are in the header file, and not just prototypes, but the whole thing

Re:c question

Posted: Thu Jan 23, 2003 8:13 pm
by ark
as a general convention, interfaces go in header files and implementations go in the actual .c files.

If you have these three files:

HeaderTest.h

Code: Select all

int* intptrarr[1000];

#define NULL 0

void fn()
{
    intptrarr[0] = NULL;
}

void Module2();
HeaderTest.cpp

Code: Select all

#include "HeaderTest.h"

int main()
{

    fn();

    return 0;
}
Module2.cpp

Code: Select all

#include "HeaderTest.h"

void Module2()
{
    fn();
}
When attempting to compile this program with Visual C++, I get the following error messages:

--------------------Configuration: HeaderTest - Win32 Debug--------------------
Compiling...
HeaderTest.cpp
Module2.cpp
Linking...
Module2.obj : error LNK2005: "void __cdecl fn(void)" (?fn@@YAXXZ) already defined in HeaderTest.obj
Module2.obj : error LNK2005: "int * * intptrarr" (?intptrarr@@3PAPAHA) already defined in HeaderTest.obj
Debug/HeaderTest.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

HeaderTest.exe - 3 error(s), 0 warning(s)

Re:c question

Posted: Thu Jan 23, 2003 8:18 pm
by elias
but thats different.if it wont work, im willign to throw everything into one big .C file if i have too. but does the declaration account* total_accounts[1000]; actually alloacate memory for the 1000 pointers? or is it jsut a declaration?

Re:c question

Posted: Thu Jan 23, 2003 8:29 pm
by ark
that declaration will actually allocate space for 1000 pointers. The structures themselves must be created individually. that is, the following code will compile and run:

account* total_accounts[1000];

void fn()
{
total_accounts[999] = (account*) malloc(sizeof(account));
}

void fn2(account* paccount)
{
total_accounts[1] = paccount;
}

Re:c question

Posted: Fri Jan 24, 2003 12:58 am
by Schol-R-LEA
I hope you don't take this the wrong way, but I was wondering if you'd considered using a linked list instead of an array of pointers; the size limit of 1000 seems somewhat peculiar, as if you chose it on the basis of it being larger than you anticipate the data size ever getting. A linked list would be more efficient for smaller data sets, and more flexible for larger ones. Is there a reason why you couldn't or wouldn't want to use a list for this purpose?

Re:c question

Posted: Fri Jan 24, 2003 1:45 pm
by elias
im using an array because its actually a hash table. i need to look for accounts quickly using a hash key. and its for an online game similar to utopia, if you know what that is. at first i dont expect more than 1000 players, but after the preliminaries ill expand it much bigger