1

Why the first options does not work? It's there a difference between "pointer->malloc" and "type []" a part of dynamic reservation of memory?

int a[10];
int b[10];
int *aux;
aux=a;
a=b;
b=aux;
int *a=(int*)malloc(10);
int *b=(int*)malloc(10);
int *aux;
aux=a;
a=b;
b=aux;
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
nickname23
  • 31
  • 5
  • 1
    The first option doesn't work because `a` and `b` aren't pointers, they just decay to them sometimes. You can't assign a new address to an array like that. – Dmitri Dec 08 '20 at 21:07
  • 1
    `a` and `b` are not pointers, they are arrays. Arrays are not pointers, they are arrays. Arrays and arrays and pointers are pointers. Arrays can be converted to pointers, but that does not mean that they, themselves, are pointers (and thus cannot be swapped/treated like them) – Human-Compiler Dec 08 '20 at 21:07
  • `a=b` does not compile in C++ in the first case. – Daniel Langr Dec 08 '20 at 21:09
  • Arrays are not pointers; they can be implicitly converted to pointers to their first elements. A pointer resulting from such a conversion is generated on the fly, and isn't stored anywhere in memory otherwise, so assigning to such pointer doesn't make sense and is not permitted. – HolyBlackCat Dec 08 '20 at 21:09
  • I have tried aux=&a[0] and ir works, but i can't do a=&b[0] – nickname23 Dec 08 '20 at 21:11
  • I believe this is a duplicate of _some_ question, but I'm not sure what the best one to link is. There are quite a few questions already detailing the difference between a pointer and array (and these all contribute to a long link of other duplicates answering the same problem in one way or another) – Human-Compiler Dec 08 '20 at 21:11
  • 1
    ***but i can't do a=&b[0]*** That is correct you can't do that. – drescherjm Dec 08 '20 at 21:12
  • i don't understand why not, where i can read more about that, i really want to to know it @drescherjm – nickname23 Dec 08 '20 at 21:14
  • Also realize that an array is like a const pointer in C++, i.e. `int * const a` is closer to `int a[10]` than `int *a`. Does that help you understand why your code "does not work"? – franji1 Dec 08 '20 at 21:14
  • @franji1 yeah, that's better – nickname23 Dec 08 '20 at 21:15
  • To really understand why and what you can and can't do with arrays, you need to delve into the 1970s and the early days of the C programing language. See if you can find a copy of the book *The C Programming Language*. – user4581301 Dec 08 '20 at 21:16
  • It's not stupid, and I'm sure there's someone on the site a lot more familiar with compilers and the history of C than I am who could explain why these choices were made, but the question as asked is a simple, "Because the language says you can't." – user4581301 Dec 08 '20 at 21:21
  • 3
    A lot of programmers trip over this because the education they receive mixes things up badly and teaches them that arrays are pointers. Then they graduate, get jobs, and find out better than half of what they learned about any specific language is wrong, if not hilariously and dangerously wrong. – user4581301 Dec 08 '20 at 21:27

1 Answers1

2

In this code:

int a[10];
int b[10];
int *aux;
aux=a;
a=b;
b=aux;

The statement aux=a; works because a decays into a pointer to its 1st element. So this is effectively just the same as aux=&a[0];

The statement a=b; does not work because a and b are both arrays, and arrays are simply not copyable in this manner. You are trying to do &a[0]=&b[0]; and that simply will not work. You can't reassign a variable's address in memory.

Had you used std::array instead, then the statement a=b; would work, as std::array has an operator= implemented, which simply copies each element from one array to another. But if you are not using std::array, then you have to copy the elements manually, using memcpy() or better std::copy().

The statement b=aux; does not work because b is an array and aux is a pointer, and you can't assign a pointer to an array.

For what you are attempting to do, you will have to change aux into an array, and actually copy the elements from one array to another, eg:

#include <algorithm>

int a[10];
int b[10];
int aux[10];
std::copy(a, a+10, aux);
std::copy(b, b+10, a);
std::copy(aux, aux+10, b);

Or, using std::array, which can do the copy for you:

#include <array>

std::array<int, 10> a;
std::array<int, 10> b;
std::array<int, 10> aux;
aux = a;
a = b;
b = aux;

Whereas in the other code:

int *a=(int*)malloc(10);
int *b=(int*)malloc(10);
int *aux;
aux=a;
a=b;
b=aux;

You are just swapping around pointers of the same type (int*), it doesn't matter what they are actually pointing at, you are not touching that data. And BTW, (int*)malloc(10) is wrong for an int array, it would need to be (int*)malloc(sizeof(int)*10) instead.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • but i dont want to copy, i want to swap thei pointes if they have pointer and if it is possible. – nickname23 Dec 08 '20 at 21:23
  • 2
    Arrays ARE NOT pointers, so you CAN'T swap them as mere pointers, you can ONLY swap their element data. – Remy Lebeau Dec 08 '20 at 21:24
  • Yes, is for that i say "if", because maybe there is a way to do it, i don't know. Because i have read "Arrays are not pointers" 10 times in this page and i o¡understand it, but i know that to acces to the data you need the memory adreess so, that array refers to a memorya adress, and i can know it, what i want is change it – nickname23 Dec 08 '20 at 21:29
  • @mouadnaciri what it sounds like you're really looking for is `std::vector`. `vector` is a wrapper around a dynamically allocated array and you can swap its pointer around to other `vector`s. – user4581301 Dec 08 '20 at 21:29
  • 1
    @mouadnaciri There is NO WAY to do what you want with arrays. Either you move around the individual element values from one array to another, or you swap pointers to the arrays, but you CAN'T swap the arrays themselves. It just doesn't work that way. You can't change a variable's memory address – Remy Lebeau Dec 08 '20 at 21:30
  • @user4581301 okey, i will look for it, thanks – nickname23 Dec 08 '20 at 21:31
  • 1
    _"array refers to a memorya adress"_ @mouadnaciri It sounds like you're still confusing arrays with pointers a little bit. An array _has_ a memory address, but it does not _refer_ to one. I think if you're trying to "swap" the pointers to an array what you probably want to do is create 2 pointers that point to the elements in the array and swap those. This doesn't swap the arrays themselves, just what the pointers refer to. Like `int* pa = a, pb = b;` and then swap `pa` and `pb`. – Human-Compiler Dec 08 '20 at 21:33
  • @Human-Compiler Yes, that's a good idea. Tank you – nickname23 Dec 08 '20 at 21:40
  • Note about pointers: take care in how you use them. Pointers aren't bad, but not knowing who owns what the pointer points at, who is responsible for maintenance and ultimately releasing the object, leads to major problems in programs. You want to dynamically allocate (`malloc`, etc) as little as possible. – user4581301 Dec 08 '20 at 21:42