Click here to Skip to main content
15,891,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need a C++ global array.
Here is the snippet of its declaration
C++
// has to be global
int fork_socket_fd[2]; //, child;
extern char fork_buf[1024];   // common message buffer
int main() {


I am defining it as local and trying to assign the results to global...

Cannot make the complier happy.
Either multiple definitions or no definitions...
Maybe I need to split the socketpair from fork and have two separate function.
The fork processes will run in "loop". But I need to resolve the global issue first.

Could use some help , beside RTFM.

What I have tried:

C++
//extern int fork_socket_fd[2];
void socketfork() {
	int local_fork_socket_fd[2];
	static const int parentsocket = 0;
	static const int childsocket = 1;
	pid_t pid;

	/* 1. call socketpair ... */
	socketpair(PF_LOCAL, SOCK_STREAM, 0, local_fork_socket_fd);

is this correct way to assign local definition to global ???

	fork_socket_fd[0] = local_fork_socket_fd[0];


will this code use the global array after it is defined correctly?


	/* 2. call fork ... */
	pid = fork();
	if (pid == 0) { /* 2.1 if fork returned zero, you are the child */
		close(fork_socket_fd[parentsocket]); /* Close the parent file descriptor */
		child(fork_socket_fd[childsocket]);
	} else { /* 2.2 ... you are the parent */
		close(fork_socket_fd[childsocket]); /* Close the child file descriptor */
		parent(fork_socket_fd[parentsocket]);
	}
	exit(0); /* do everything in the parent and child functions */
}
Posted
Updated 17-Mar-20 7:12am

Quote:
I need a C++ global array.
There is no difference between an array in C and one in C++. But your code is pure C, not C++.

But, to the point, you have the following:
C++
// has to be global
int fork_socket_fd[2]; //, child;
extern char fork_buf[1024];   // common message buffer

When you declare something as extern, you are telling the compiler that it is defined elsewhere. But you have declared it as defined in some external module, but added a definition, so the compiler is confused as to what you mean. Just declare it (without the extern keyword) as
C++
char fork_buf[1024];   // common message buffer
 
Share this answer
 
You have to declare an instance in one and only one place. Then everywhere else the variable is accessed you use the extern modifier.

Another way is to put this definition into a header file. I have it a file called Globals.h :
C++
// the following needs to be defined ONCE to instatiate the globals
// it should be done prior to including this file.
//#define DEFINE_GLOBALS

#ifdef DEFINE_GLOBALS
#define	Global
#else
#define Global	extern
#endif
Then you include the Globals.h file in all of your modules, prior to declaring in variables. You can also put the global variables in one file. I sometimes do this and call it GlobalData.h. In your case, let's say you want the buffer and the integers to be global variables. I would make a GlobalData.h file and it would look like this :
C++
// GlobalData.h - declare data global to the application

#include "Globals.h"

Global int fork_socket_fd[2]; // child
Global char fork_buf[1024];   // common message buffer
Then, in the file where your main function is you do this :
C++
#define DEFINE_GLOBALS
#include "GlobalData.h"

int main()
...
and all other files that access the global data will have ONLY this :
C++
#include "GlobalData.h"
I do this whenever I need to have global data. I often wrap the global data in a namespace to denote their nature. You can extend the definitions to support initialization if you want to. Here is another definition you can add to Globals.h to do that :
C++
// these macros facilitate declaring and initializing a global variable

#ifdef DEFINE_GLOBALS
#define GlobalVar(Type,Variable,Value)		Type Variable=Value
#else
#define GlobalVar(Type,Variable,Value)		extern Type Variable
#endif
As an example, say you had a variable for the color of all text in your app. You can declare it in your GlobalData.h file like this :
C++
GlobalVar( COLORREF, TextColor, RGB(255,255,255) );
and it will be initialized to white and accessible to all modules that include GlobalData.h

-edit- I DID NOT SEE RICHARD'S ANSWER. Not this time or any other time.
 
Share this answer
 
v2
Comments
Richard MacCutchan 17-Mar-20 13:44pm    
"I DID NOT SEE RICHARD'S ANSWER"
No worries, it's a common issue when more than one response is being worked on around the same time.
Vaclav_ 17-Mar-20 19:18pm    
You have to declare an instance in one and only one place. Then everywhere else the variable is accessed you use the extern modifier.

Perfect.
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900