-
global variable
i need to make a dynamic global 2d array....
will this work....
Code:
//inside of header file, but outside of function
int p**
//function to create dynamic array
dynamicArray(int num_rows, int num_cols)
{
p = new int*[num_rows];
for (int i = 0 ; i < num_rows; i++ )
p[i] = new int[num_cols];
}
-
Don't declare your pointer-pointer in the header, do it in the actual cpp file instead.
Make sure the memory is deleted afterwards too, to prevent memory leaks. delete the pointers in the array first before deleteing the pointer-pointer (is there another name for this?).
Also, test if your memory allocations succeeds before attempting to access them.
Other than that, it should work fine.
-
what if i have functions in several different cpp files that all need the global array? Is this possible?
-
sorry, one more question. I know how to delete, but how do i test to make sure the memory is allocated.
-
You mean when linking your files? It is possible to share one global variable, if you declare it in every cpp file that uses it. You must declare all of them but one as extern.
To check if a memory allocation was successful, check if the pointer differs from NULL. If it equals to NULL, it failed.
Code:
int* MyVar;
MyVar = new int;
if(MyVar != NULL)
{
//Do stuff
delete MyVar;
}
else
{
cout << "Failed!";
}
int* MyArray;
MyArray = new int[1024];
if(MyArray != NULL)
{
//Do stuff
delete[] MyArray;
}
else
{
cout << "Failed!";
}
(notice the [] on delete when deleting an array)
-
Magos is correct, and this is something that I see too often. The main reason you don't want to do this is when mulitple files use the header with the pointer. What you should do is this.
Code:
//inside header
extern int **ptr;
//inside ONE of your files
int **ptr;
//in one of your files
void dynamicArray(int num_rows, int num_cols)
{
p = new int*[num_rows];
for (int i = 0 ; i < num_rows; i++ )
p[i] = new int[num_cols];
}
Okay, there are a couple of ways to make sure that memory is allocated.
Code:
void dynamicArray(int num_rows, int num_cols)
{
using namespace std;
try {
p = new int*[num_rows];
} catch(bad_alloc e) {
cerr << e.what();
return;
}
for (int i = 0 ; i < num_rows; i++ ) {
try {
p[i] = new int[num_cols];
} catch(bad_alloc e) {
cerr << e.what();
return;
}
}
}
//alternatively
void dynamicArray(int num_rows, int num_cols)
{
using namespace std;
p = new int*[num_rows];
if(!p) {
cerr << "not enough memory" << endl;
return;
}
for (int i = 0 ; i < num_rows; i++ ) {
p[i] = new int[num_cols];
if(!p) {
cerr << "not enough memory" << endl;
return;
}
}
}
-
thanks for the replies, i understand every thing except how to declare the global variable. What i really have is an array of structs.
Code:
typedef struct tile {
bool furniture;
bool cat;
bool vacuum;
int dirt;
} Tile;
Tile p**;
So as I understand, I have two choices:
choice 1: declare the struct and the pointer in all of the cpp files and not in any header files. Also making one of the declarations an extern.
choice 2: declare the struct and the pointer in a header as an extern and declare the struct and pointer in only one of the cpp files.
are both ways correct, or have i misunderstood?
-
I've made several tests on extern not long ago. Though it may be informative for other readers to declare the variable as extern in the header file, this is not neccessary (but doable).
It works fine without it.
Just declare the variable in all of your cpp files. One normally, the rest as extern. Which file you place the non-extern variable in doesn't matter.
Code:
OtherFile.cpp
extern Tile** p;
Code:
AnotherFile.cpp
extern Tile** p;
Code:
YourHeader.h
//Notice, no declaration is needed
-
thanks for the good answer. I still have another question though. :)
All i need to do with the struct defintion then is to put it in the header file that is included in every cpp file?
Code:
Main.cpp
#include "header.h"
Tile** p;
Code:
OtherFile.cpp
#include "header.h"
extern Tile** p;
Code:
AnotherFile.cpp
#include "header.h"
extern Tile** p;
Code:
YourHeader.h
typedef struct tile {
bool furniture;
bool cat;
bool vacuum;
int dirt;
} Tile;
//Notice, no declaration is needed
[/B][/QUOTE]
-
-