This question already has answers here:
How do I correctly set up, access, and free a multidimensional array in C?
(5 answers)
Closed 6 years ago.
I already have an idea on how to malloc a matrix if it were an int**. But using a typedef, I think have an idea but I'm not so sure.
typedef int LabeledAdjMatrix[SIZE][SIZE];
Do I do it like this?
APSP = (APSPMatrix*)malloc(sizeof(APSPMatrix));
But when I access it I'm gonna have to use *APSP[0][0] and I have no idea how to use this in memset/memcpy.
Is there a proper way of doing this? Both in dynamically allocating and in accessing.
My advice would be to not use array typedefs, they make the code harder to read as it is less apparent when array-pointer decay is or isn't happening.
If you want to allocate a contiguous array you can write:
int (*APSP)[SIZE] = malloc( sizeof(int[SIZE][SIZE]) );
and then access it as APSP[0][0].
Your post talks about "malloc as if it were int **", by which I assume you mean you want separate allocations for each rows... but then you would write int **APSP and write a loop to allocate each row, it is really nothing to do with [SIZE][SIZE].
Related
This question already has answers here:
Can we have a struct element of type Variable length array? [duplicate]
(5 answers)
Closed 2 years ago.
I would like to define array size inside a structure by using a parameter of this structure. Does C permit to do something like this ?
struct queue {
int head;
int top;
int size;
struct action action[size];
};
No you can't. Since action is not a dynamic variable, the compiler needs to know at compile time how much space it needs for action. size was not even initialized. Anyway, you could see this just by trying to compile.
The size is not known at the time of defining the struct. Therefore it is impossible for the compiler to understand how large the result will be. Typically, you would first allocate memory for the struct, and have a struct action *action; member. After initializing the struct, you use instance->action = calloc(instance->size, sizeof *instance->action) to allocate memory for the array.
This question already has an answer here:
Pointer to a specific fixed address
(1 answer)
Closed 3 years ago.
I am sorry I know that's super basic question but I have to ask.
I have an array in integer type and I want to Assign it to specific memory address. How can I do that with C language?
For Example ;
int inputs[10] ={4,10,89};
So I want to Assign this inputs to 0x20000001.
Thank you so much.
Unless you are managing your memory allocation regions, say with linker scripts or other means, you should not do it. Compiler takes care of all memory allocation for you and does not provide any means for pre-defined memory addresses.
However, if you know what you are doing, you can use a pointer to handle it:
int *array = (int*)0x2000000;
now you can initialize it element by element, or by memcpy.
memcpy(array, inputs, sizeof(inputs));
You are likely to get segfault due to memory access violation if you are accessing restricted area of the memory, but here's a quick code to test it out if you know what address you are writing to:
int address = 0x20000001;
int *ptr;
ptr = (int*) address;
*ptr = inputs;
This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 5 years ago.
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!
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.
This question already has answers here:
Determine size of dynamically allocated memory in C
(15 answers)
How to get the size of dynamically (using malloc or calloc )allocated memory? [duplicate]
(2 answers)
Closed 9 years ago.
Is there a way to know from a pointer the size with which malloc() was called?
For example, if I have:
typedef struct entry entry_t;
struct entry
{
int val;
};
entry_t *entryt_p = (entry_t *)malloc(10 * sizeof(entry_t));
Is there a way I can extract from entryt_p with which size malloc() was called?
There isn't a portable way specified by the language. Some versions of malloc may offer an extension to do it. In general, it's up to your program to keep track.
There is no way for the user to access this information. Malloc returns a pointer to the address of the memory that was allocated.
It is up to the program to keep track of how much was allocated.
If you really don't want to keep track of the size consider using a sentinel value in the last position.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I allocate a 2D array using double pointers?
I used VB 2012 Express to make a maze program.
It works really well even when I set ROW*COLUMN to 499*499, (the maze is an array: unsigned char maze[ROW][COLUMN]).
But one time I tried to make a super-giant maze of999*999, and the compiler gave me a "stack overflow" error.
I do know what it means, but is there any way to assign extra memory or even use some disk space to run my program?
You are allocating maze on the stack, and stack size is typically limited to between 1 and 8 megabytes. To overcome this limitation, allocate maze on the heap.
For suggestions on how to do this, see How can I allocate a 2D array using double pointers? and Heap allocate a 2D array (not array of pointers)
You can either dynamically allocate your array (e.g maze = new char[ROW*COLUMN]) or allocate it globally (outside function scope), like
#define ROW 999
#define COLUMN 999
unsigned char maze[ROW][COLUMN];
int main(void)
{
}