简体   繁体   中英

Dynamic sized array in C

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.)

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).

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)

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM