Here's the code:
int EdgeCount = 0;
int numOfEdges = 0;
void addEdge() {
// some code
numOfEdges++;
}
int EdgeWeightArray[numOfEdges]; // error
I want that global array with variable parameters to use it later but I couldn't do that because without #define we can't define globally arrays parameters; and #define is not a variable thing. In my code numOfEdges is variable and I couldn't make it constant.
EdgeWeightArray has global scobe, so it must be a fixed size. But numOfEdges is of course not a constant expression.
What size do you expect EdgeWeightArray to be? Do you expect it to grow when you increment numOfEdges? If so, you nwed to look into dynamic memory allocation; namely malloc and realloc.
Brief example with no error checking:
int numOfEdges = 0;
int *EdgeWeightArray;
void addEdge(some parameters) {
//SOME CODE
numOfEdges++;
EdgeWeightArray = realloc(EdgeWeightArray, numOfEdges * sizeof(EdgeWeightArray[0]));
}
Why not use a global int pointer and allocate memory with malloc() with desired number of elements?
What you are trying to do is not possible because memory for global variables are calculated at compile time and the value of numOfEdges is updated at runtime.
In my eyes, you want to create a not well designed code, where the size has global scope and the vector has to have local scope in order to be stored in the stack (according to your starting point). Anyway, you might do something like that:
void defineSize() {
numOfEdges++;
}
void useIt()
{
int EdgeWeightArray[numOfEdges];
/* Use EdgeWeightArray, once the function has been executed, \
EdgeWeightArray will dissapear */
}
This is an expansion to Jonathons answer.
If you already know how many Elements there will be at maximum for your array you could use a fixed size for your array. This could be faster in the moment, because there is no reallocation happening.
But you have to make sure to do appropriate error handling with this approach. It can be quite error prone.
You would then need a counter to keep track of your Edges, but you appear to already have one of those.
Your code would then look like:
#define MAX_EDGE_COUNT 1000
int EdgeCount = 0;
void addEdge() {
// some code
EdgeWeightArray[EdgeCount] = newEdge;
EdgeCount++;
}
int EdgeWeightArray[MAX_EDGE_COUNT];
Related
There is a line of code inside a method similar to:
static char data[] = "123456789";
I want to fill the above data array with a million characters not just nine.
But since it is tedious to type it, I want to do that in for loop.
Is that possible to do it keeping it as "static char data[]"?
edit:
static char data[1000000];
for(int i=0; i<1000000; i++)
{
data[i] = 1;
}
There are multiple ways to achieve this in C:
you can declare the global static array as uninitialized, write an initialization function and call this function at the beginning of the program. Unlike C++, C does not have a standard way to invoke such an initialisation function at program startup time, yet some compilers might provide an extension for this.
static char data[1000000];
void init_data(void) {
//the loop below will generate the same code as
//memset(data, 1, sizeof data);
for (int i = 0; i < 1000000; i++) {
data[i] = 1;
}
}
int main() {
init_data();
...
}
you can change your program logic so the array can be initialized to 0 instead of 1. This will remove the need for an initialization function and might simplify the code and reduce the executable size.
you can create the initializer for the array using an external program and include its output:
static char data[1000000] = {
#include "init_data.def"
};
you can initialize the array using macros
#define X10(s) s,s,s,s,s,s,s,s,s,s
#define X100(s) X10(s),X10(s),X10(s),X10(s),X10(s),X10(s),X10(s),X10(s),X10(s),X10(s)
#define X1000(s) X100(s),X100(s),X100(s),X100(s),X100(s),X100(s),X100(s),X100(s),X100(s),X100(s)
#define X10000(s) X1000(s),X1000(s),X1000(s),X1000(s),X1000(s),X1000(s),X1000(s),X1000(s),X1000(s),X1000(s)
#define X100000(s) X10000(s),X10000(s),X10000(s),X10000(s),X10000(s),X10000(s),X10000(s),X10000(s),X10000(s),X10000(s)
static char data[1000000] = {
X100000(1), X100000(1), X100000(1), X100000(1), X100000(1),
X100000(1), X100000(1), X100000(1), X100000(1), X100000(1),
};
Note however that this approach will be a stress test for both your compiler and readers of your code. Here are some timings:
clang: 1.867s
gcc: 5.575s
tcc: 0.690s
The last 2 solutions allow for data to be defined as a constant object.
Is that possible to do it keeping it as "static char data[]"?
No, you have to specify the size explicitly. If you wish to compile-time initialize the array rather than assigning to it in run-time with a for loop or memset, you can use tricks such as this.
Another option might be to use dynamic allocation with malloc instead, but then you have to assign everything in run-time.
You can define statically allocated arrays in various ways, incidentally, this has nothing to do with the static keyword, see this if you need more information about static variables. The following discussion won't have anything to do with that, hence I will be omitting your static keyword for simplicity.
An array declared as:
char data[] = "123456789";
is allocated in the stack in the compile time. Compiler can do that since the size of the array is implicitly given with the string "123456789" to be 10 characters, 9 for the data and +1 for the terminating null character.
char data[];
On the other hand, will not compile, and your compiler will complain about missing array sizes. As I said, since this declaration allocates the array in the compile time, your compiler wants to know how much to allocate.
char data[1000000];
This on the other hand will compile just fine. Since now the compiler knows how much to allocate. And you can assign elements as you did in a for loop:
for(int i=0; i<1000000; i++)
{
data[i] = 1;
}
Note:
An array of million chars has quite a respectable size, typically 1Mb, and may overflow your stack. Whether or not that it actually will depends on pretty much everything that it can depend on, but it certainly will rise some eyebrows even if your code works fine. And eventually, if you keep increasing the size you will end up overflowing your buffer.
If you have truly large arrays you need to work with, you can allocate them on the heap, i.e., in the wast empty oceans of your ram.
The part above hopefully should have answered your question. Below is simply an alternative way to assign a fixed value, such as your (1), to a char array, instead of using for loops. This is nothing but a more convenient way (and perhaps a better practice), you are free to ignore it if it causes confusion.
#include <string.h>
#define SIZE 100000
// Create the array, at this point filled with garbage.
static char data[SIZE];
int main( void )
{
// Initialise the array: assigns *integer 1* to each element.
memset( data, 1, sizeof data )
//^___ This single line is equivalent of:
// for ( int i = 0; i < SIZE; i++ )
// {
// data[i] = 1;
// }
.
.
.
return 0;
}
Not using C that often, I came across a possibly simple problem. I have several functions, that need access to a global array-variable g. But the actual size of this variable has to be defined in an init()-function. The size depends on some other stuff, so g has to be declared somehow with a dynamic size. I read about malloc and other functions, but I am not sure how to use them properly.
Example:
double g[dynamic]; // size is not known yet
int n;
void init()
{
// calculate "n" for array size
n = ...
// declare and initialze g with a size "n"
}
void dostuff()
{
for (int i = 0; i < n; i++)
work(g[i]);
}
How should I solve this?
You cannot use an array. You must use a pointer.
double *global_array; // size is not known yet
size_t nglobal_array; // may be helpful to have the size
void init(void)
{
// calculate "nglobal_array" for array size
nglobal_array = 42;
// declare and initialze global_array with a size "nglobal_array"
global_array = malloc(nglobal_array * sizeof *global_array);
if (global_array == NULL) {
fprintf(stderr, "Error allocating resources.\nProgram aborted.\n");
exit(EXIT_FAILURE);
}
}
void dostuff()
{
for (int i = 0; i < nglobal_array; i++)
work(global_array[i]);
}
Don't forget to free(global_array) when you no longer need it.
Complete usage would then be something like this
#include <stdlib.h>
// includes
// declarations & definitions as above
int main(void) {
init();
dostuff();
free(global_array);
}
What you want to achieve is not possible in C.
A global array must have a fixed size at compile, or at least at link time.
You can declare the array without a specified size:
extern double g[];
But it must be defined somewhere with an actual size, computed from a constant expression at the definition place, and the size cannot be determined from the above declaration, so it must be passed some other way to the functions that will use the array: either implicitly, with a special value signifying the end of the array (like '\0' for char strings) or explicitly via a separate variable as you posted. Note however that n and g are very poor name choices for global variables as they are likely to clash with local variable names and convey no meaning to the reader.
If the size is not known until run time, you should define a pointer instead of an array and also define a separate variable with the length of the array that will be allocated by the initialization function.
double *g;
size_t g_length;
No. C doesn't do that. Arrays declared in global scope have fixed space allocated for them in your binary(.EXE files on Windows and ELF executables on Linux). If you want an array of dynamic size, you need to dynamically allocate it.
Example is here:
#include <stdlib.h>
#define ARRAY_SIZE 100
typedef char T; //your type here
T* array;
void init() {
array = malloc(sizeof(T) * ARRAY_SIZE); //array filled with garbage values
//array = calloc(ARRAY_SIZE, sizeof(T)); //array filled with 0x00
}
void finish() {
free(array); // DO NOT ACCESS ARRAY AFTER THIS CALL!
}
int main() {
init();
array[6] = 63; //access array as normal
finish();
//array[41] = 23; //will most likely crash due to a segmentation fault, also called an access violation on Windoez
}
There's different coding style with different programmers.
Colleagues and I are working on image data processing and we have 3 different ways.
Colleague1:
int IMAGE_WIDTH=1280;
int IMAGE_HEIGHT=800;
char* colleague1_way() //just allocate when he wants
{
char* mem = malloc(IMAGE_WIDTH*IMAGE_HEIGHT*2);
return mem;
}
int main(void)
{
char* data;
data=colleague1_way();
function1(data); //pass by pointer
function2(data); //pass by pointer
function3(data); //pass by pointer
free(data);
}
Colleague2:
int IMAGE_WIDTH=1280;
int IMAGE_HEIGHT=800;
char* data; //set it as global memory
void colleague2_way()
{
data = malloc(IMAGE_WIDTH*IMAGE_HEIGHT*2);
}
int main(void)
{
colleague2_way();
function1(); //void input, proceed data inside function
function2(); //void input, proceed data inside function
function3(); //void input, proceed data inside function
free(data);
}
Me:
int IMAGE_WIDTH=1280;
int IMAGE_HEIGHT=800;
int main(void)
{
char* data = malloc(IMAGE_WIDTH*IMAGE_HEIGHT*2);
function1(data); //pass by reference
function2(data); //pass by reference
function3(data); //pass by reference
free(data);
}
My idea is
I could see the allocated memory clearly, and free it at the end of main().
function1~3 might be in another cpp file, so it's easy to handle.
Anyone could give me a comment and is there any better way?
Also, if it's in C++, any good ways?
I dont know the specifics of why you want heap memory but stack is faster. Your sample code makes your program seem small so i would do something like:
#define IMAGE_WIDTH 1280
#define IMAGE_HEIGHT 800
#define PIXEL_DEPTH 2
int main(void)
{
char data[IMAGE_WIDTH][IMAGE_HEIGHT][PIXEL_DEPTH] = {0};
function1(data); //pass by reference
function2(data); //pass by reference
function3(data); //pass by reference
}
This is like your solution but you dont have to worry about freeing memory and you get faster writes. Like you said different programmers have different solutions, thats just my two cents.
First lets talk about "Colleague2" way: char pointer data declared globally
You should avoid declaring variables globally until it is really needed. Here, I don't see any reason of declaring data pointer globally. Since it is visible to other functions it's scope is increased and can be accidentally modified by other part of code which is not desirable.
"Colleague1" and "Me" way:
In these both ways, one good thing is the scope of data pointer is limited. But in the "Colleague1" way the memory allocation operation is part of another function, which is actually good because you can do the memory allocation error handling in one place (which is missing in all the 3 ways). Consider a case where you need to allocate same size of memory in some other part of the code then you just need to call this function instead of again doing malloc. If you are sure that you will not need to allocate the same size of memory again then in that case "Me" way is also okay. So, out of these two ways follow whichever way is appropriate but make sure to do the required error handling.
I wish to create the following:
int amount[i];
As a global variable (to practice using threads and mutexes) but the variable i is defined at launch of the program:
./a.out 10
How may I take the value through the main (argv[1]) and create the global accordingly?
You can use global pointer variable and then allocate memory based on argv[1].
int *amount;
int main(int argc, char *argv[])
{
int count = atoi(argv[1]);
amount = malloc(count * sizeof(int));
...
free(amount);
return 0;
}
You're trying to use a variable length array at global scope. This won't work (globals need to have a constant, known size, otherwise compilation would be difficult).
IMHO, you shouldn't be using a global in the first place. Better use a local variable, and pass it via argument to the functions / parts of your program that need access to it.
IMHO, you shouldn't be using VLA in the first place.
I'd go with something like this:
int main(int argc, char ** argv) {
// check arguments, not done here!
int value = atoi(argv[1]);
// Check that it's actually usable as a size!
size_t count;
if (value >= 0) {
count = value;
}
else {
// Fires of hell here
exit(1);
}
int * amount = malloc(sizeof(int) * count); // add error check, please!
// use at will
free(amount);
return 0;
}
If you insist on using a global, then there's the possibility to make the (constant sized) pointer amount a global variable.
Also: Using heap allocated data instead of stack allocated if you'd use a VLA is to be preferred when accessing the data from a detached thread, because the VLA could already be out of scope when the thread tries to access it!
Use constexpr keyword to make any non-const variable as constexpr. It will avoid the compiler errors, but be careful about the variable.
For example:
#include<iostream.h>
constexpr int afun()
{
return(3);
}
enum
{
TOTAL_NO_OF_PACKETS = afun() // You can use for Enum also
};
unsigned packets[afun()]; // Using in Global array
void main()
{
// **
}
It's not possible to create global variables using the user input. See basically you can use global variables by defining them in the program code.
Is there a way to reset variables declared as static within a function? The goal is to make sure that the function is not called with lingering values from an unrelated call. For example, I have a function opearting on columns of a matrix.
int foo(matrix *A, int colnum, int rownum){
static int whichColumn;
static int *v; //vector of length A->nrows
if (column != whichColumn){
memset(v,0,size);
whichColumn = which;
}
//do other things
}
The function is called n times, once for each column. Is this a proper way of "re-setting" the static variable? Are there other general fool-proof ways of resetting static variables? For example, I want to make sure that if the call is made with a new matrix with possibly different dimensions then the vector v is resized and zeroed etc. It seems the easiest way may be to call the function with a NULL pointer:
int foo(matrix *A, int colnum, int rownum){
static int whichColumn;
static int *v; //vector of length A->nrows
if (A == NULL){
FREE(v);
whichColumn = 0;
}
//do other things
}
Use an idempotent initializer function and global variables instead.
For example:
int foo;
int *m = NULL;
static void InitVars() {
foo = 0;
if (m != NULL) {
free(m);
}
m = malloc(sizeof(int)*5);
memset(m, 0, sizeof(int)*5);
}
If your initializer is really idempotent, you can call it again to reset the variables.
If you need this to be called automagically, use __attribute__((constructor)) (for GCC) like so:
static void InitVars __attribute__((constructor)) ();
However, you should note that if you need to do this, you should reconsider using in-function static variables and instead use passed-in fresh ones that are returned/written and passed to subsequent related calls.
I'd recommend turning it into a struct and writing a small helper function for managing the semantics of what you're trying to do. It could return the buffer if the request is appropriate for its size, or create a new one on demand (and free the old one) if necessary.
One approach I've seen used when a C module was imported to C++ was to surround the whole module with a class wrapper, and replace all static variables inside functions with uniquely-named "global" varaibles outside the functions. I don't know any good way to achieve a similar effect for projects involving multiple source files, though I'd like to know if one exists. I have some embedded system code in C, which I simulate by adding some C++ wrappers in VS2005. For example, I have I/O registers defined so that something like TX1CON = 0x5C; would translate into something like IOMAP(0x251).P = 0x5C; IOMAP is a property which would send "write 0x5C to address 0x251" to a hardware-simulation program. This approach works well, but I can't do a clean reset. Any ideas?
An approach which can sometimes be helpful if one needs a "reset" method which can hit an unknown number of functions or modules is to have a global counter for how many times that reset method has been called, and then have each function or module include code like:
extern unsigned long global_reset_count;
void do_something(int whatever)
{
static ... this, that, the other, etc. ...;
static unsigned long my_reset_count;
if (my_reset_count != global_reset_count)
{
my_reset_count = global_reset_count;
... initialize this, that, the other, etc ...
}
}
In some multi-threading contexts, if the initialization of the static variables may depend upon some global variables, one may wish to replace the "if" with a "while"; in such a case; memory barriers may also be needed in such a case, though the exact requirements would vary depending upon the operating environment.
Also, an alternative pattern that may be useful within embedded systems would be to have a modules_initialized global variable which gets set to 0 by the global reset method, and then have each module start with something like:
if (!atomic_bit_test_and_set32(&modules_initialized, FOOBOZZ_MODULE_ID))
{
... Initialize module FOOBOZZ ...
}
This would require that there be no more than 32 module ID's, and would require that they be uniquely allocated somehow, but some systems can handle that pretty nicely. For example, a linker may allow one to define a "data section" from address 0-31 of an address space independent from any other; if each module declares a single-byte variable within that address space, the linker could generate the appropriate addresses for those variables.
you could build your function in such a way that if you call it with zero parameters, then it will reset its internal static variables
here is an example :
int foo(matrix *A = NULL, int colnum = 0, int rownum = 0)
{
static int whichColumn;
static int *v; //vector of length A->nrows
if (A == NULL){
FREE(v);
whichColumn = 0;
}
//do other things
}
You actually just have to call function to reset like this:
foo(); // internal values would then be reset
Make sure that all your parameters to the function has default values, if for example you pass an optional, then make sure it has = boost::none as default value