0

I'm having issues with my program, and I'm trying to break it down and I'm seeing that I'm having issues with memory allocation at a minimum.

I have a struct:

typedef struct{

    int originX;
    int originY;
    char ticBoard[3][3];
    int result;
    int turn;

} simBoard;

I would expect the size of this struct (one single instance of it) to be (4 ints * 4 bytes) + (1 bye char * 3 * 3) or 25 bytes. When I run the sizeof() function on simBoard, I get a value of 28 bytes. I'm assuming that it's the 25 + 3 extra that I don't need to worry about.

The main issue is when I try to declare an array of this struct

simBoard* boardArray = (simBoard*)malloc(sizeof(simBoard)*size));

Assume size is some constant for this scenario. To my knowledge this should create an array of the simBoard struct, of size size. I should be able to go

boardArray[3]

And get the 4th item of boardArray correct? However I'm running into an issue with the memory allocation. When I run:

printf("%zu is the size of the array\n", sizeof(boardArray));

The return value is 8. I even tried to further sort out the issue:

simBoard* boardArray = (simBoard*)malloc(224);

When I ran the printf again, I'm still getting a value of 8 bytes for boardArray. If you guys could lead me in the right direction that'd be fantastic, as I'm absolutely stumped here.

Thank you!

Matthew Kerian
  • 812
  • 5
  • 18
  • 3
    boardArray is a pointer, not an array. Its size is whatever pointer size is on your platform, regardless of what it points to. – Mat Dec 12 '17 at 09:59
  • And you're not even printing the `sizeof`... you're just printing the value of the pointer. But then if you got `8`, I guess what you posted isn't really what you're running. :/ What is your question anyway? _"I'm running into an issue with the memory allocation."_ What is the issue? That you want to get the size and don't know how? That'd be a programmer issue, not any problem with the allocation. This is not a clearly written question. – underscore_d Dec 12 '17 at 10:00
  • @underscore_d Sorry that was a typo, on the printf I was doing `printf("%zu is the size of the array\n", sizeof(boardArray))l – Matthew Kerian Dec 12 '17 at 10:04
  • @Mat But isn't an array just a list of items stored in order in memory? I think I'm beginning to see what you mean I believe. So I should do something like `boardSim myArray = (boardSim)malloc(SIZE);` – Matthew Kerian Dec 12 '17 at 10:09
  • You simply need to read the duplicate already linked. C can't tell you the allocated size again later. You need to remember that yourself. – underscore_d Dec 12 '17 at 10:10
  • @underscore_d Alright thank you, I appreciate the clarification. – Matthew Kerian Dec 12 '17 at 10:11
  • 1
    @MatthewKerian: boardArray is not an array according to the C type system. Its type is "pointer to boardSim". When you ask for its size, you get the size of that type (pointer to data). What you posted in the previous comment does not work at all. The only sort of array with a size set at runtime are called VLAs in C (look it up), and probably not what you want here. – Mat Dec 12 '17 at 10:19
  • 1
    'Sorry that was a typo' copy/paste only the code that you actually tested, else you are wasting everyone's time:( – Martin James Dec 12 '17 at 10:48
  • @MartinJames In order to isolate the problem and take away any distractions I just typed out the needed stuff. Unfortunately I forgot one of the needed items :/ – Matthew Kerian Dec 12 '17 at 10:51

1 Answers1

0

You can indeed index the 4th struct in the array using the notation boardArray[3].

Bear in mind though that boardArray itself is a pointer type. That explains your printf output. Arrays with dynamic storage duration and pointer types are inextricably linked in C. For the avoidance of doubt, sizeof(boardArray) is sizeof(simBoard*).

C doesn't provide functionality to obtain the length of an dynamic array. You need to remember the length yourself.

Don't forget to call free(boardArray) when your done. The C runtime library will take appropriate measures to ensure that all the struct elements are freed.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Hi. So in this case when I'm trying to find the size of the entire array, what would I do? I was under the impression that to find the amount of numbers in an int array for example, I could do something like `int size = (sizeof(myArray) / sizeof(int));` So wouldn't `sizeof(simBoard);` give me the total size of the array? – Matthew Kerian Dec 12 '17 at 10:07
  • 1
    @MatthewKerian: Added that. No, you need to remember the length yourself. The sizeof(myArray) / sizeof(myArray[0]) idiom only applies to arrays with automatic storage duration. – Bathsheba Dec 12 '17 at 10:09
  • Thank you, I'm working on the program and I noticed something else. It seems like `sizeof(myArray)` is giving me the total items in myArray? Is this how it works or is it a coincidence – Matthew Kerian Dec 12 '17 at 10:16
  • What's the point of asking that with no indication of what "`myArray`" is? I'd expect that if it were a stack-allocated array of `char`, for example. – underscore_d Dec 12 '17 at 10:17