OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Apr 24, 2024 8:12 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: c question
PostPosted: Thu Jan 23, 2003 4:15 pm 
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?


Top
  
 
 Post subject: Re:c question
PostPosted: Thu Jan 23, 2003 4:33 pm 
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


Top
  
 
 Post subject: Re:c question
PostPosted: Thu Jan 23, 2003 4:34 pm 
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


Top
  
 
 Post subject: Re:c question
PostPosted: Thu Jan 23, 2003 4:46 pm 
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.


Top
  
 
 Post subject: Re:c question
PostPosted: Thu Jan 23, 2003 4:51 pm 
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.


Top
  
 
 Post subject: Re:c question
PostPosted: Thu Jan 23, 2003 4:55 pm 
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


Top
  
 
 Post subject: Re:c question
PostPosted: Thu Jan 23, 2003 8:13 pm 
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:
int* intptrarr[1000];

#define NULL 0

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

void Module2();


HeaderTest.cpp
Code:
#include "HeaderTest.h"

int main()
{

    fn();

    return 0;
}


Module2.cpp
Code:
#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)


Top
  
 
 Post subject: Re:c question
PostPosted: Thu Jan 23, 2003 8:18 pm 
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?


Top
  
 
 Post subject: Re:c question
PostPosted: Thu Jan 23, 2003 8:29 pm 
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;
}


Top
  
 
 Post subject: Re:c question
PostPosted: Fri Jan 24, 2003 12:58 am 
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?


Top
  
 
 Post subject: Re:c question
PostPosted: Fri Jan 24, 2003 1:45 pm 
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


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 67 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group