Allocated pointer set to NULL

Hello there. I've got a question. So here is what i have:

1
2
3
4
5
 // in .h file
static Class** vektor;

// in .cpp file
Class** Class::vektor = new Class*[256];


my question is, is there a way to allocate each pointer to null upon it's creation? (after the call of 'new')
I know c++ doesnt have static blocks like java, but i need it to be initialized before main starts, upon linking(if i'm not wrong), so having a function that does the initialization, which i call from main(), is not really an option.
Last edited on
C++11 did introduce brace initialization. See http://www.informit.com/articles/article.aspx?p=1852519

However, how do you expect the dynamic allocation to behave with global variables? More importantly, who does the deallocation?

(You definitely cannot initialize a memory location during linking, if it does not even exists before process is running.)
1
2
3
4
5
6
7
8
Class** allocate_and_null(std::size_t size)
{
    Class** result = new Class*[size];
    std::fill(result, result+size, nullptr);    // http://en.cppreference.com/w/cpp/algorithm/fill
    return result;
}

Class** Class::vektor = allocate_and_null(256);


All global objects are initialized before main begins.

[Edit: That's a general solution. However, it isn't needed here:
Class** Class::vektor = new Class*[256]();]
Last edited on
yeah, thanks guys it worked out, made a static function that return a **pointer, which done the job.

If you dont mind asking here 1 more question, i dont wanna open another topic.

I have that same vektor, and an object at 9th position. At the end of the main i'm calling function:

1
2
3
4
5
6
void Class::delete(){
for(int i=0; i<256; i++){
if(vektor[i]) delete vektor[i];
}
delete [] vektor;
}


the strangest this is happening, since all entries are NULL except for entry 9, after i comes to 9, it calls the destructor which does 2 instructions:

1
2
3
4
5
6
Class::~Class(){
if(entryNumber == 9){
setvect(entryNumber,oldInterrupt);  // restoring the interrupt routine 
vektor[entryNumber] = 0;
}
}


and as soon as it exits the destructor, and gets back to the for loop, all other entries are not NULL anymore, they get some kind of adress 0395:0054 and so on (concrete situation in my case)
Is there any way to avoid that? :/ or why is it happening?
Last edited on
Is there any way to avoid that? :/ or why is it happening?

Yes. Get rid of line 5 in Class::delete (which probably should not have as its name a reserved keyword.) Deleting vektor means it no longer contains a valid address. Don't delete stuff you're still using.
Last edited on
ok, delete() method was just an example, it's not named like that in my .cpp like that. But anyway, line 5 is called after the loop finishes, and the problem i'm having is manifesting while still inside for loop, it doesn't get to line 5 cus the program breaks.
I'm a little confused here. Your example code shows delete and the destructor belonging to the same "Class". Is this the case in your code? If it is, it's quite possible that the object being operated on in delete is destroyed in your for loop, which would lead to undefined behavior.
Topic archived. No new replies allowed.