-3

I'm trying to create an array of 18 elements of BYTE whit calloc but don't know why calloc only give me back only 8 elements, I did it manually in the stack and the program works.

int arrsize;
BYTE copyrow[18];
arrsize = sizeof(copyrow);

When i compile here arrsize = to 18, so, evrething is fine. But when I use calloc:

int arrsize;
BYTE *copyrow;
copyrow = calloc(18, sizeof(BYTE));
arrsize = sizeof(copyrow);

Now the compiler say arrsize = to 8, so I don't know what's happening here. Need help.

ManKino
  • 47
  • 4
  • 8 is the pointer size. You should store the number of elements in the array in separate variable. Compiler can't get size of array where your pointer `copyrow` points. – Inline Mar 21 '18 at 20:03
  • @machine_1 I flagged it as well and SO added this comment :( –  Mar 21 '18 at 20:08

3 Answers3

3

When you define BYTE *copyrow;, copyrow is a pointer, and the size of a pointer is 8 on a 64 bit architecture, regardless how "big" the memory block to which it points is.

BYTE copyrow[18];, in constrast, is an array of 18 BYTE-elements, and the size of such an array is 18 * sizeof(BYTE).

You could do it this way:

int nrOfElements = 18;  // could be dynamically set as well.
BYTE *copyrow = calloc(nrOfElements, sizeof(BYTE));
int arrsize = sizeof(BYTE) * nrOfElements;
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
  • I just want to create an array wich number of elements dinamiclly, in the example i'm trying I know is 18 numbers of elements, but that will change for other examples. – ManKino Mar 21 '18 at 20:14
  • @ManKino the answer tells you exactly what you are asking. Instead of writing `int nrOfElements = 18;` you calculate the number by any means necessary to your algorithm. – Pablo Mar 21 '18 at 20:42
  • Ok so if BYTE copyrow[18]; is the same as BYTE *copyrow = calloc(18, sizeof(BYTE)); and I know that, why my program doesn't work with the dinamic one. This array is not storing the 18 bytes of data in need, or as I can't see what is storing each element of the dinamic array, I don't know what really is storing each element. – ManKino Mar 21 '18 at 22:14
  • I didn't find a way to see in the debugger what are storing each element of the dinamic array – ManKino Mar 21 '18 at 22:20
1

Array and pointer are different things.

In many cases array will decay to a pointer, but they are essentially different.

BYTE copyrow[18];
arrsize = sizeof(copyrow);

Here sizeof shows the size of the whole array, which is 18 * sizeof(BYTE).

BYTE *copyrow;
arrsize = sizeof(copyrow);

Here sizeof shows the size of a pointer, which has nothing to do with what it points to.

llllllllll
  • 16,169
  • 4
  • 31
  • 54
  • so if the size of une element is 8, half of the problem is going good, but anyway I need to allocate dinamic memory for an array of element, that, in this example I know is 18 but that can change, the thin is, when I declare the array in the stack, the program works, but I need to do it dinamic. – ManKino Mar 21 '18 at 20:18
0

To me, this was also a confusing topic when I started of with C. It seemed like sizeof() were implemented differently depending on what the argument was.

sizeof() is in most cases evaluated at compile time and the only reason int a[n]; sizeof(a); works is because all the information needed to figure out its size is present at compile time.

BYTE copyrow[18];
arrsize = sizeof(copyrow);

Is (as pointed out by others) interpreted as...

BYTE copyrow[18];
arrsize = sizeof(BYTE)*18;

...by the compiler. Hence the dynamic case would look the same:

BYTE *copyrow;
copyrow = (BYTE*) calloc(18, sizeof(BYTE));
arrsize = sizeof(BYTE)*18;
Witnessthis
  • 136
  • 4