Programming Puzzle

J

Jerry Coffin

How can you _not_ remove an element from an array?

The ways are manifold and manifest... said:
Here is a trivial case:

size_t count = 2;
int * array = (int *)malloc(sizeof(int) * count);

What malloc returns is the address of some storage, but that storage
is NOT an array. While the differences are often negligible, there are
also situations where they become major. Just for an obvious example,
using sizeof on a real array gives you the size of the whole array.
There is no similar capability for storage returned by malloc.
 
J

Julie

Jerry said:
What malloc returns is the address of some storage, but that storage
is NOT an array. While the differences are often negligible, there are
also situations where they become major. Just for an obvious example,
using sizeof on a real array gives you the size of the whole array.
There is no similar capability for storage returned by malloc.

Conceptually, with the exception that you point out regarding intrinsic ability
to determine size, they are one and the same.

This all comes down to your definition of array. I was using the looser term
of a block of memory that can be accessed w/ an AMF, not the strict specific
term. I'm not alone in this looser term, either -- most of the time when
referring to an allocated block that is accessed through an AMF, it is called
an array, regardless of the specific allocation scheme, rather than calling it
a 'block of continuous allocated memory for same-structure instances' or some
such wording.
 
I

Ioannis Vranos

Julie said:
Conceptually, with the exception that you point out regarding intrinsic ability
to determine size, they are one and the same.

This all comes down to your definition of array. I was using the looser term
of a block of memory that can be accessed w/ an AMF, not the strict specific
term. I'm not alone in this looser term, either -- most of the time when
referring to an allocated block that is accessed through an AMF, it is called
an array, regardless of the specific allocation scheme, rather than calling it
a 'block of continuous allocated memory for same-structure instances' or some
such wording.



You are right, those sequences created on the free store, are usually
called "arrays on the free store / heap", while those in automatic
storage are called "built in arrays".

So both types are called arrays. However when the stand alone term
"array" is used, its meaning depends on context.

So for example, if you are asked to make a function to perform something
"on the contents of an array", both types are included.

In this case the stand-alone array term was used for something
impossible to do with built in arrays, so it would be better if the
question was about "arrays on the free store" explicitly.






Regards,

Ioannis Vranos
 
J

Jerry Coffin

[ ... ]
Conceptually, with the exception that you point out regarding intrinsic
ability to determine size, they are one and the same.

Somehow I'm reminded of an old joke:

Comedian: If you call a horse's tail a leg, how many legs does a horse
have?

Straight Man: five!

Comedian: Nope -- you can call its tail what you want, but it still
only has four legs.

Likewise here: there are certainly similarities, and if you want to
call them the same thing, you're welcome to do so, but they're not the
same thing, and your concept of them won't change that.
 
J

Julie

Jerry said:
[ ... ]
Conceptually, with the exception that you point out regarding intrinsic
ability to determine size, they are one and the same.

Somehow I'm reminded of an old joke:

Comedian: If you call a horse's tail a leg, how many legs does a horse
have?

Straight Man: five!

Comedian: Nope -- you can call its tail what you want, but it still
only has four legs.

Likewise here: there are certainly similarities, and if you want to
call them the same thing, you're welcome to do so, but they're not the
same thing, and your concept of them won't change that.

Then what do you call them?

int a1[] = { 0, 0, 0 };

int * a2 = (int *)calloc(3, sizeof(int));

int * a3 = new int[3];
 
R

Richard Herring

[QUOTE="Julie said:
[ ... ]
Conceptually, with the exception that you point out regarding intrinsic
ability to determine size, they are one and the same.

Somehow I'm reminded of an old joke:

Comedian: If you call a horse's tail a leg, how many legs does a horse
have?

Straight Man: five!

Comedian: Nope -- you can call its tail what you want, but it still
only has four legs.

Likewise here: there are certainly similarities, and if you want to
call them the same thing, you're welcome to do so, but they're not the
same thing, and your concept of them won't change that.

Then what do you call them?

int a1[] = { 0, 0, 0 };[/QUOTE]

Array of int.
int * a2 = (int *)calloc(3, sizeof(int));

int * a3 = new int[3];

Inferior substitutes for std::vector<int>

;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top