Reputation: 14269
Here is a code snippet I was working with:
int *a;
int p = 10;
*(a+0) = 10;
*(a+1) = 11;
printf("%d\n", a[0]);
printf("%d\n", a[1]);
Now, I expect it to print
10
11
However, a window appears that says program.exe has stopped working.
The if I comment out the second line of code int p = 10;
and then tun the code again it works.
Why is this happening? (What I wanted to do was create an array of dynamic size.)
Upvotes: 0
Views: 270
Reputation: 11
You will need to use malloc to assign memory that array.
If you want the size to by dynamic you will need to use realloc every time you wish to increase the size of the array without destroying the data that is already there
Upvotes: 1
Reputation: 143122
No memory is being allocated for a
, it's just an uninitialized pointer to an int (so there are two problems).
Therefore when data is stored in that location, the behavior is undefined. That means you may sometimes not even get a segmentation fault/program crash, or you may -> undefined. (Since C doesn't do any bounds checking, it won't alert you to these sort of problems. Unfortunately, one of the strength of C is also one of its major weaknesses, it will happily do what you ask of it)
Upvotes: 3
Reputation: 490623
There are probably at least 50 duplicates of this, but finding them may be non-trivial.
Anyway, you're defining a pointer, but no memory for it to point at. You're writing to whatever random address the pointer happened to contain at startup, producing undefined behavior.
Also, your code won't compile, because int *a, int p = 10;
isn't syntactically correct -- the comma needs to become a semicolon (or you can get rid of the second int
, but I wouldn't really recommend that).
In C, you probably want to use an array instead of a pointer, unless you need to allocate the space dynamically (oops, rereading, you apparently do want to -- so you need to use malloc
to allocate the space, like a = malloc(2);
-- but you also want to check the return value to before you use it -- at least in theory, malloc
can return a null pointer). In C++, you probably want to use a std::vector
instead of an array or pointer (it'll manage dynamic allocation for you).
Upvotes: 9
Reputation: 21
First a has not been initialized. What does it point to? Nothing, hopefully zero, but you do not know.
Then you are adding 1 to it and accessing that byte. IF a were 0, a+1 would be 1. What is in memory location 1?
Also you are bumping the address by one addressable memory unit. This may or may not be the size of an integer on that machine.
Upvotes: 0
Reputation: 4251
You're not even allocating memory so you're accessing invalid memory...
Use malloc to allocate enough memory for your array:
int* a = (int*) malloc(sizeof(int)*arraySize);
//Now you can change the contents of the array
Upvotes: 2
Reputation: 9432
You have to allocate storage for the array. Use malloc
if you're in C, new
if it's C++, or use a C++ std::vector<int>
if you really need the array size to be dynamic.
Upvotes: 0