Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Access violation writing location 0x0... setting int *

Tags:

c++

pointers

I've looked through various questions both here and other places, but I still cannot explain the access violation error I'm getting. "Access violation writing location 0x00000000" corresponds to a NULL pointer, correct? I've declared an int pointer, and I later try to set a value at that location. Shouldn't the memory space be allocated when I declare the pointer? Forgive me if this is noobish, but I'm much more of a Java/AS3 guy.

Here is a portion of my code...

int* input;
char* userInput[1];
int* output;

int _tmain(int argc, _TCHAR* argv[])
{
    while(1)
    {
        srand(time(0));
        *input = (int)(rand() % 10);

It breaks at the last line.

like image 791
TheBeege Avatar asked Dec 04 '22 08:12

TheBeege


1 Answers

Memory is allocated for the pointer, but the pointer itself still doesn't point anywhere. Use new to allocate memory for the pointer to point to, and free it with delete later.

like image 185
Ignacio Vazquez-Abrams Avatar answered Dec 26 '22 20:12

Ignacio Vazquez-Abrams