Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

array shorthand in c++ (how safe?)

29 views
Skip to first unread message

hdr...@freenet.de

unread,
Jul 21, 2016, 7:21:10 AM7/21/16
to
I inspect some code which works with Intel icpc, but seems to
be time-bomb if compiled with g++.
It swaps two arrays like thus:
tmp=array1;
array1=array2;
array2=tmp;

array1 and array2 were allocated this way:
array1=(int *)malloc (4*size);

tmp is not allocated, it is merely:
int *tmp;

Now I am curious how compiler would know to copy "size" words
then stop. Apparently Intel can, but g++ cannot, so corrupts
adjacent memory, and the free() for a later malloc has invalid pointer.


JiiPee

unread,
Jul 21, 2016, 7:37:54 AM7/21/16
to
On 21/07/2016 12:20, hdr...@freenet.de wrote:
> I inspect some code which works with Intel icpc, but seems to
> be time-bomb if compiled with g++.
> It swaps two arrays like thus:
> tmp=array1;
> array1=array2;
> array2=tmp;
>
> array1 and array2 were allocated this way:
> array1=(int *)malloc (4*size);
>
> tmp is not allocated, it is merely:
> int *tmp;
>
> Now I am curious how compiler would know to copy "size" words
> then stop.

"copy" - are you talking about this:

array1=array2;
which copy are you talking about?

Fred.Zwarts

unread,
Jul 21, 2016, 9:06:20 AM7/21/16
to
schreef in bericht
news:1c6f603b-7200-4527...@googlegroups.com...
You did not show a compilable program, nor the declarations of array1 and
array2.
If they are int * pointers, than not the arrays are swapped in this code,
but the pointers to the arrays are swapped.
This is not a problem for any compiler.
How do you know that adjacent memory is corrupted?
Because of swapping pointers, it could also be that one array is not freed
and the other one is freed twice.

D Laboratory

unread,
Jul 21, 2016, 9:58:23 AM7/21/16
to
On 2016/7/21 19:20, hdr...@freenet.de wrote:

> tmp=array1;
> array1=array2;
> array2=tmp;

> tmp is not allocated, it is merely:
> int *tmp;
>
> Now I am curious how compiler would know to copy "size" words
> then stop. Apparently Intel can, but g++ cannot, so corrupts
> adjacent memory, and the free() for a later malloc has invalid pointer.
>

You did not copy the array. You just swapped the pointers.

Jerry Stuckle

unread,
Jul 21, 2016, 10:28:22 AM7/21/16
to
You're not copying the array itself. What you are doing is swapping the
addresses of the two arrays. Try displaying the pointers before and
after the swap, and you'll see what I mean.

It's like switching the addresses on two apartments, so that apartment
1A is now apartment 1B, and vice versa. You haven't moved the families,
furniture, or anything except the numbers on the doors. But now mail
going to apartment 1A ends up in a different family.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstu...@attglobal.net
==================

JiiPee

unread,
Jul 21, 2016, 10:35:16 AM7/21/16
to
thats what i was also thinking

0 new messages