When to use an array over a pointer or vice versa

My understanding is that pointers can be used, among other things, so that you can dynamically allocate memory when you need it (and know how much you need), instead of statically setting arrays in advance.

I am struggling with the determination of when it is better to save the time of calculating the dynamic distribution and select a larger amount of memory, having more memory and using some calculation time to select only the memory I need.

Can someone shed some light on this topic? Are there any general rules that might help?

+4
source share
5 answers

You should use dynamically allocated memory if:

  • You do not know how much memory you will need at compile time.
  • the amount of memory changes at startup
  • you need large amounts of memory

You should use statically allocated memory if:

  • you know the size at compile time
  • low memory required

Using dynamically allocated memory requires the use of system calls, this is when you program to ask something in the operating system. You have a speed limit because the process is likely to lose the “processing time” that is transferred to another process. To make a call, you need to complete many tasks. Making a system call to query memory is a process that is much more difficult than just writing to an array stored on the process stack.

+1
source

Typically, you want to use an array when you know either the total size of the data you will be dealing with, or at least the maximum data size. This is especially applicable if you really do not expect huge changes in size - if (for example) the option varies from 10 to 20 elements, then it is probably easiest to select 20 independently and do with it (unless each element is really big )

If you have a much lesser idea of ​​the size of the data ahead of time or (and an important feature), it may be too difficult to schedule a stack installation, dynamic allocation becomes much more useful. The main weakness of dynamic allocation is that if you ever need to know the size, you need to monitor it yourself, and it is up to you to free the memory when you are done with it. Many (especially complicated, grumbling) problems in C come down to using memory after freeing it or forget to free memory when you are done with it.

+1
source

You use a dynamic container when your data is dynamic, or you need to transfer data from different areas of your program.

1 - Dynamic data Say you have a list of your neighbors. They are building a new house on your street, and you should add a guy to the list, but you have been allocated enough space for 15 neighbors. This dynamic memory will allow you to increase the size of this container. This is not how it works. In fact, it finds a new piece of memory of the required size and then copies the old container.

Or another example. Say you are writing a program that tracks an address book. One of your users has ten contacts. The other user is a corporation, and 50,000 employees must be stored in this address book. You do not want to highlight 50,000 spaces for a user who has ten contacts, so you specify exactly how much you need.

2 - Data Transfer When you allocate static data, it is pushed onto the stack and then inaccessible after exiting the scope. Therefore, if you call some function that generates your array and then passes the memory address of the array back to your caller, you will get a runtime error. This is due to the fact that after this function exits, the array goes out of scope, and therefore it is removed from the stack.

However, if you allocate it dynamically, it goes into a heap and is not freed until you release it or until the program exits. That way, you can simply save the pointer to the beginning of the array and use it throughout your program, without worrying that it goes out of scope until you want it.

0
source

Arrays are just continuous chunks of memory. When you declare an array, you have a pointer

int foo[5]; 

foo (no index) is a pointer to the first element in this array.

 foo[0] = 1; *foo = 1; 

They do what they do:

 foo[1] = 2; *(foo + 1) = 2; 

When you create an array using int foo[5]; which you create on the stack. This is local to the current function, and as soon as you return from the function, it ceases to be valid. If you malloc() memory, you create an array on the heap (and a pointer to it)

 int *foo = malloc(sizeof(int) * 5); foo[0] = 1; *foo = 1; 

You need to manage this memory yourself and free() when you are done with it:

 free(foo); 
0
source

Although this question has an accepted answer. Let me tell you a little different thing. In C, sometimes you can use an array with a size defined at runtime. For instance:

 void foo (int a[], int n) { int buf[n]; /* do something with buf[] */ } 

This type of array is allocated on the stack and has a variable size. The difference from malloc is that you do not need free() memory; which will be taken care of when there is a function call. Of course, this also means that you may not be able to return the address of the array.

0
source

All Articles