Can you explain the size difference between arrays and pointers in C?

  • Thread starter Thread starter ladesidude
  • Start date Start date
  • Tags Tags
    Arrays Pointers
Click For Summary

Discussion Overview

The discussion centers around the distinction between arrays and pointers in C, particularly focusing on whether dynamically allocated memory (using malloc) can be considered arrays and how their sizes are interpreted. The scope includes technical explanations and conceptual clarifications regarding memory allocation and pointer behavior.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants assert that a1, a2, and a3 are pointers that point to the first element of dynamically allocated memory, not true arrays.
  • Others argue that if considered as arrays, a1, a2, and a3 are not of the same size due to differing allocations.
  • It is noted that using sizeof on these pointers yields the size of a pointer (e.g., sizeof(int*)) rather than the size of the allocated memory.
  • One participant mentions that the name of an array is a constant pointer to its first element, while a1, a2, and a3 are not constant pointers.
  • Another participant highlights the practical need to free the allocated memory, distinguishing it from stack-allocated arrays.
  • Some participants provide examples to illustrate the differences between pointers and arrays, including syntax errors related to array assignment.

Areas of Agreement / Disagreement

Participants express differing views on whether a1, a2, and a3 should be classified as arrays or pointers, indicating a lack of consensus. The discussion remains unresolved regarding the implications of their classification and the interpretation of their sizes.

Contextual Notes

Limitations include the dependence on definitions of arrays and pointers, as well as the context of memory allocation (heap vs. stack). The discussion also touches on historical differences in C language behavior.

ladesidude
Messages
4
Reaction score
0
Code:
int* a1 = malloc(4 * sizeof(int));
int* a2 = malloc(3 * sizeof(int));
int* a3 = malloc(5 * sizeof(int));
a1[0] = 1; a1[1] = 2; a1[2] = 3; a1[3] = 4;
a2[0] = 9; a2[1] = 8; a2[3] = 7;
a3[0] = -1; a3[1] = -2; a3[2] = -3; a3[3] = -4; a3[4] = -5;

My question:

Are a1, a2 and a3 arrays? I think they are pointers that point to an array right?

Secondly, If a1, a2, and a3 are considered to be arrays, are they of the same size?
 
Technology news on Phys.org
>Are a1, a2 and a3 arrays? I think they are pointers that point to an array right?

Well, pointers that point to the first element in an array. You can do pointer arithmetic to accomplish the same thing the subscript operator does; e.g.:
Code:
*(a1+1) = 2;

>Secondly, If a1, a2, and a3 are considered to be arrays, are they of the same size?

If you consider them as arrays, then I would say no; you obviously shouldn't access a1[4] but you can access a3[4]. On the other hand, if you use sizeof on them, you'll get the same thing (sizeof(int*)).
 
JaWiB said:
>Are a1, a2 and a3 arrays? I think they are pointers that point to an array right?

Well, pointers that point to the first element in an array. You can do pointer arithmetic to accomplish the same thing the subscript operator does; e.g.:
Code:
*(a1+1) = 2;

>Secondly, If a1, a2, and a3 are considered to be arrays, are they of the same size?

If you consider them as arrays, then I would say no; you obviously shouldn't access a1[4] but you can access a3[4]. On the other hand, if you use sizeof on them, you'll get the same thing (sizeof(int*)).

i did use sizeof() and they all came back as 4, since the size of an int is 4 bytes and since its pointing to the first element in the array, I think. So its safe to say that they are not of the same size?
 
ladesidude said:
i did use sizeof() and they all came back as 4, since the size of an int is 4 bytes and since its pointing to the first element in the array, I think.
Try it with other types of pointers...
 
The name of an array is a constant pointer to the first element of the array. In your example a1, a2, and a3 are not constant, so they are not exactly the same as the name of an array. If you had declared them constant then, for all practical purposes, they would be the names of arrays.
 
DaleSpam said:
If you had declared them constant then, for all practical purposes, they would be the names of arrays.
Except, of course, for the eminently practical fact that they will need to be freed. That they won't be allocated on the stack could be relevant. And, of course, the fact that sizeof a1 will return the size in bytes of a pointer rather than the array that was allocated.
 
Hurkyl said:
Except, of course, for the eminently practical fact that they will need to be freed.
Yeah, there is that :redface:
 
I tell you my answer. a1,a2,a3 are all arrays. But they are allocated memory in heap not stack.
 
They are pointers, not arrays.

Here's an example of the difference:

Code:
int *p1, *p2;
int a1[5], a2[5];

    p1 = p2;    // copies a pointer
    a1 = a2;    // copies an entire array
 
  • #10
Jeff Reid said:
Code:
    a1 = a2;    // copies an entire array
Actually, that one is just a syntax error.
 
  • #11
Hurkyl said:
Actually, that one is just a syntax error.
That used to work with older C (not C++) compilers. An alternate example using structure instead of array (this works with Visual Studio Express):

Code:
typedef struct {
    int i[5];
}SI5, *PSI5;

static SI5 si0;
static PSI5 psi0;

int main(int argc, char **argv)
{
SI5 si1;
PSI5 psi1;

    si1  =  si0;    // copy a structure
    psi1 = psi0;    // copy a pointer
    return(0);
}

Getting back to the array example, it can be treated similar to pointer, when passed to a function. As Hurkly pointed out, an array name can't be used for assignment. The sizeof an array is different than the size of a pointer to an array.

Code:
#include <stdio.h>
void x(int *, int *);

int main(int argc, char **argv)
{
int ai0[5];
int *pai0 = ai0;
    printf ("%d %d\n", sizeof(ai0), sizeof(pai0));
    x(ai0, pai0);
    return(0);
}

void x(int *pi0, int*pi1)
{
    printf ("%x %x\n", pi0, pi1);
}

// on a 32 bit system, first line output is 20 4
// 2nd line output will show the same address for both values
 
Last edited:

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
20
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K