I'm trying to create a file system and I need to calloc the file descriptors.
Let's say I have two structures with the following definitions:
#define DESCRIPTOR_MAX (256)
#define TABLE_MAX (32)
typedef struct S16FS S16FS_t;
typedef struct {
bitmap_t *fd_status;
size_t fd_pos[DESCRIPTOR_MAX];
inode_ptr_t fd_inode[DESCRIPTOR_MAX];
} fd_table_t;
struct FS {
back_store_t *bs;
fd_table_t fd_table;
};
I'm callocing a new file system with no problem:
FS_t *fs = (FS_t *)calloc(8, sizeof(FS_t));
but my problem arrises when I want to calloc the fd_table within the FS struct. Here's what I'm trying.
This one produces no errors:
fs->fd_table = *(fd_table_t*)calloc(TABLE_MAX, sizeof(fd_table_t));
I'm getting an error with the following:
fs->fd_table.fd_pos = (size_t *)calloc(TABLE_MAX, sizeof(size_t));
error: array type 'size_t [256]' is not assignable
Can someone explain to me what i'm doing wrong or if I'm just completely wrong in my process? Do I even need to calloc the fd_pos?
It looks like fs->fd_table.fd_pos is an array, not a pointer. If you want a dynamic array, then change the type to size_t*.
Otherwise, it's perfectly fine to leave it as an array and not use dynamic allocation. In that case, if you want to zero the memory (as calloc does), you can just use memset:
memset( fs->fd_table.fd_pos, 0, sizeof(fs->fd_table.fd_pos) );
Oh yes, and also WhozCraig points out that your first example is a leak (allocating memory, then dereferencing and copying, followed by losing the pointer). In fact, you don't need to memset as I suggested above because the entire structure was zeroed with the first calloc.
Related
I tried to find the proper way to dynamically allocate memory for a structure that looks like this:
typedef struct myThread {
unsigned int threadId;
char threadPriority;
unsigned int timeSlice;
sem_t threadSem;
} myThread;
I remember, but I'm not sure, that, in some school paper, I saw that the proper way to allocate memory for this case is this one:
myThread *node = (myThread *)malloc(sizeof(myThread *));
I tried that and it worked, but I didn't understand why. Sizeof pointer for my architecture is 8 bytes, so by writing the instruction above, I'm allocating 8 bytes of continuous memory, not enough to hold the information needed in my structure. So I tried to allocate 1 byte of memory, like this:
myThread *node = (myThread *)malloc(1);
And it's still working.
I tried to find the answer for this behavior but I didn't succeed. Why is this working? Besides that, I have few more questions:
Which is the right way to dynamically allocate memory for a structure?
Is that cast necessary?
How is the structure stored in memory? I know that (*node).threadId is equivalent to node->threadId and this confuses me a bit because by dereferencing the pointer to the structure, I get the whole structure, and then I have to access a specific field. I was expecting to access fields knowing the address of the structure in this way: *(node) it's the value for the first element, *(node + sizeof(firstElement)) it's the value for the second and so on. I thought that accessing structure fields it's similar to accessing array values.
Thank you
Later Edit: Thank you for your answers, but I realized that I didn't explained myself properly. By saying that it works, I mean that it worked to store values in those specific fields of the structure and use them later. I tested that by filling up the fields and printing them afterwards. I wonder why is this working, why I can fill and work with fields of the structure for which I allocated just one byte of memory.
The below works in that they allocate memory - yet the wrong size.
myThread *node = (myThread *)malloc(sizeof(myThread *));// wrong size,s/b sizeof(myThread)
myThread *node = (myThread *)malloc(1); // wrong size
Why is this working?
When code attempts to save data to that address, the wrong size may or may not become apparent. It is undefined behavior (UB).
C is coding without training wheels. When code has UB like not allocating enough memory and using it, it does not have to fail, it might fail, now or later or next Tuesday.
myThread *node = (myThread *)malloc(1); // too small
node->timeSlice = 42; // undefined behavior
Which is the right way to dynamically allocate memory for a structure? #M.M
The below is easy to code right, review and maintain.
p = malloc(sizeof *p); //no cast, no type involved.
// or
number_of_elements = 1;
p = malloc(sizeof *p * number_of_elements);
// Robust code does error checking looking for out-of-memory
if (p == NULL) {
Handle_error();
}
Is that cast necessary?
No. Do I cast the result of malloc?
How is the structure stored in memory?
Each member followed by potential padding. It is implementation dependent.
unsigned int
maybe some padding
char
maybe some padding
unsigned int
maybe some padding
sem_t
maybe some padding
I wonder why is this working, why I can fill and work with fields of the structure for which I allocated just one byte of memory.
OP is looking for a reason why it works.
Perhaps memory allocation is done in chunks of 64-bytes or something exceeding sizeof *p so allocating 1 had same effect as sizeof *p.
Perhaps the later memory area now corrupted by code's use of scant allocation will manifest itself later.
Perhaps the allocater is a malevolent beast toying with OP, only to wipe out the hard drive next April 1. (Nefarious code often takes advantage of UB to infect systems - this is not so far-fetched)
Its all UB. Anything may happen.
Since memory allocation in C is quite error prone I always define macro functions NEW and NEW_ARRAY as in the example below. This makes memory allocation more safe and succinct.
#include <semaphore.h> /*POSIX*/
#include <stdio.h>
#include <stdlib.h>
#define NEW_ARRAY(ptr, n) \
{ \
(ptr) = malloc((sizeof (ptr)[0]) * (n)); \
if ((ptr) == NULL) { \
fprintf(stderr, "error: Memory exhausted\n"); \
exit(EXIT_FAILURE); \
} \
}
#define NEW(ptr) NEW_ARRAY((ptr), 1)
typedef struct myThread {
unsigned int threadId;
char threadPriority;
unsigned int timeSlice;
sem_t threadSem;
} myThread;
int main(void)
{
myThread *node;
myThread **nodes;
int nodesLen = 100;
NEW(node);
NEW_ARRAY(nodes, nodesLen);
/*...*/
free(nodes);
free(node);
return 0;
}
malloc reserves memory for you to use.
When you attempt to use more memory than you requested, several results are possible, including:
Your program accesses memory it should not, but nothing breaks.
Your program accesses memory it should not, and this damages other data that your program needs, so your program fails.
Your program attempts to access memory that is not mapped in its virtual address space, and a trap is caused.
Optimization by the compiler transforms your program in an unexpected way, and strange errors occur.
Thus, it would not be surprising either that your program appears to work when you fail to allocate enough memory or that your program breaks when you fail to allocate enough memory.
Which is the right way to dynamically allocate memory for a structure?
Good code is myThread *node = malloc(sizeof *node);.
Is that cast necessary?
No, not in C.
How is the structure stored in memory? I know that (*node).threadId is equivalent to node->threadId and this confuses me a bit because by dereferencing the pointer to the structure, I get the whole structure, and then I have to access a specific field. I was expecting to access fields knowing the address of the structure in this way: *(node) it's the value for the first element, *(node + sizeof(firstElement)) it's the value for the second and so on. I thought that accessing structure fields it's similar to accessing array values.
The structure is stored in memory as a sequence of bytes, as all objects in C are. You do not need to do any byte or pointer calculations because the compiler does it for you. When you write node->timeSlice, for example, the compiler takes the pointer node, adds the offset to the member timeSlice, and uses the result to access the memory where the member timeSlice is stored.
you do not allocate the right size doing
myThread *node = (myThread *)malloc(sizeof(myThread *));
the right way can be for instance
myThread *node = (myThread *)malloc(sizeof(myThread));
and the cast is useless so finally
myThread *node = malloc(sizeof(myThread));
or as said in remarks to your question
myThread *node = malloc(sizeof(*node));
The reason is you allocate a myThread not a pointer to, so the size to allocate is the size of myThread
If you allocate sizeof(myThread *) that means you want a myThread ** rather than a myThread *
I know that (*node).threadId is equivalent to node->threadI
yes, -> dereference while . does not
Having myThread node; to access the field threadId you do node.threadId, but having a pointer to you need to deference whatever the way
Later Edit: ...
Not allocating enough when you access out of the allocated block the behavior is undefined, that means anything can happen, including nothing bad visible immediately
I've got a theoretical question on allocating memory for structs. Consider the following code IN THE MAIN FUNCTION:
I have the following struct:
typedef struct {
char *descr = NULL;
DWORD id = 0x00FFFF00;
int start_byte = 0;
int end_byte = 0;
double conversion_factor = 0.0;
} CAN_ID_ENTRY;
I want an array of this structs, so I'm allocating a pointer to the first struct:
can_id_list = (CAN_ID_ENTRY **)malloc(sizeof(CAN_ID_ENTRY));
And then I'm allocating memory for the first struct can_id_list[0]:
can_id_list[0] = (CAN_ID_ENTRY *)malloc(sizeof(CAN_ID_ENTRY));
Now the problem is, that I don't know HOW MANY of these structs I need (because I'm reading a CSV-File and I don't know the amount of lines/entries). So I need to enlarge the struct-pointer can_id_list for a second one:
can_id_list = (CAN_ID_ENTRY **)malloc(sizeof(CAN_ID_ENTRY));
And then I'm allocating the second struct can_id_list[1]:
can_id_list[1] = (CAN_ID_ENTRY *)malloc(sizeof(CAN_ID_ENTRY));
can_id_list[1]->id = 6;
Obviously, this works. But why? My point is the following: Normally, malloc allocates memory in one block in the memory (without gaps). But if another malloc is done BEFORE I'm allocating memory for the next struct, there is a gap between the first and the second struct. So, why can I access the second struct via can_id_list[1]? Does the index [1] store the actual address of the struct, or does it just calculate the size of the struct and jumps to this address beginning on the offset of the struct-pointer can_id_list (-> can_id_list+<2*sizeof(CAN_ID_ENTRY))?
Well, my real problem is, that I need to do this inside a function and therefore I need to pass the pointer of the struct to the function. But I don't know how to do this, because can_id_list is already a pointer ... and the changes must also be visible in the main method (that's the reason i need to use pointers).
The mentioned function is this one:
int load_can_id_list(char *filename, CAN_ID_ENTRY **can_id_list);
But is the parameter CAN_ID_ENTRY **can_id_list correct? And how do i pass the struct-array into this function? And how can i modify it inside??
Any help would be great!
EDIT: Casting malloc returns - Visual Studio forces me to do that! (Because it's a C++ project i think)
As the comments already said, the source of your confusion is can_id_list = (CAN_ID_ENTRY **)malloc(sizeof(CAN_ID_ENTRY)); allocating the wrong amount of memory. It probably gave you space for a few pointers to be stored, not just one. Should be can_id_list = (CAN_ID_ENTRY **)malloc(sizeof(CAN_ID_ENTRY*));.
To answer the question at the end,
But is the parameter CAN_ID_ENTRY **can_id_list correct? And how do i
pass the struct-array into this function? And how can i modify it
inside??
If you want to enlarge the size of the array within another function, you need to pass CAN_ID_ENTRY*** pr so you can set *ptr = realloc(...) inside as needed. Realloc may give you the new chunk of memory at a different address, so you can't simply pass in a CAN_ID_ENTRY** ptr then do realloc(ptr). See https://www.tutorialspoint.com/c_standard_library/c_function_realloc.htm
I have a simple struct containing an array of ints and an index to be used for that array.
#define BUFF_SIZE 100
typedef struct _buffer Buffer;
struct _buffer {
int buff[BUFF_SIZE];
int index;
};
I am trying to write a function to create a pointer to an instance of this struct, and initialise its values to zero. However when I try to initialise the array using the arrow operator I get an error that the expression is invalid.
...
Buffer* newBuff;
newBuff->buff = {0}; //Error occurs here
...
I know that in c an array is a pointer to the base of the memory block in which the array resides, but was under the impression that dereferencing the struct pointer would allow me to access the array. Am I wrong about this, or am I missing something here? I have a feeling that it is something very simple, but for the life of me I just can't spot it.
Thanks!
EDIT: Thanks for the quick answers guys, they've helped me understand some of what is occurring. I neglected to mention that this is intended to be run on an embedded system (sorry about that), so I would like to avoid using includes or malloc if at all possible.
There are several types of confusion here. A pointer points to memory (duh), but you need to get that memory from somewhere. How you initialize that memory is separate.
You can allocate your structure on the stack by declaring a local variable inside your function:
// Initialize all buff elements and index to 0
// Note: Buffer, not Buffer*
Buffer newBuf = { {0}, 0 };
Or you can allocate it on the heap:
Buffer *newBuf = malloc(sizeof(Buffer));
memset(newBuf, 0, sizeof(Buffer));
When allocating objects on the stack, they are only valid while the current function is executing. You can't return a pointer to an object on the stack. Further, stack space is typically limited, so you can't put megabyte-sized objects there. When allocating memory on the heap with malloc(), you need to take care to free() when it is not used any more, otherwise you leak memory.
You see that when allocating objects on the stack, you are able to use an initializer-list { {0}, 0 }. In the heap case, you can not do that and you have to zero the memory manually using memset.
The problem is not with the arrow -> operator, it's with the right side of the assignment: {0} is allowed in initializers, but you cannot assign an array like that. You need to use memset to zero out the elements of the array:
memset(newBuff->buff, 0, sizeof(newBuff->buff));
Note: your code does not set the newBuf to a valid location in memory. You need to allocate memory first, like this:
newBuff->buff = malloc(sizeof(*newBuff));
I have to disagree with #dasblinkenlight, but I think the problem is that newBuff is a pointer, but it hasn't been given a value. Hence, when you dereference it, you'll probably get a crash or unpredictable results.
You'd have to have a statement that's something like:
newBuff = malloc(sizeof (Buffer));
to allocate space of the appropriate size and assign it to newBuff.
Unless I'm missing something terribly obvious...
From your question I was able to understand- you want to initialize all array elements of structure variable buff[ ] to zero.
memset() function from <string.h> file will help you to solve this problem.
As you said you are writing code for embedded system so there is no need to include whole <string.h> file in your program
I tried to replicate your program and found the following solution-
void *memset(void * , int , size_t ) __attribute__((__nonnull__(1)));
#define BUFF_SIZE 100
typedef struct _buffer Buffer;
struct _buffer {
int buff[BUFF_SIZE];
int index;
};
int main (void)
{
Buffer* newBuff;
memset(newBuff->buff, 0, BUFF_SIZE);
}
Please let me know if it was helpful.
I am trying to dynamically allocate an array of structures in c so that I can refer to them the same as if I had done a static declaration. I understand that calloc() does the additional step of initializing all the allocated memory to 0. But, other than that, are the 2 completely interchangeable for the following code? If I am using fread() and fwrite() to get these data structures in and out of a file, does calloc() help or hinder this?
#define MAGIC 13
struct s_myStruct {
int a[6000][400];
int b[6000][400];
int c[6000][400];
};
struct s_myStruct stuff[MAGIC];
vs
struct s_myStruct *stuff = calloc(MAGIC, sizeof(s_myStruct);
Thank you.
They're not the same. Declaring the data like this:
struct s_myStruct stuff[MAGIC];
will leave the memory uninitialized if you're declaring it in function scope (which you must be, given the second choice). Adding = {0} before the semicolon rectifies this.
The second choice, of using calloc, allocates the memory on the heap.
There's always a difference though: sizeof(stuff) will be 13 * sizeof(struct s_myStruct) in the first case, and the size of a pointer in the second case.
You really don't want to do the first one, as you'd be putting 13 * 3 * 6000 * 400 * 4 = 370MB on the stack.
But this has nothing to do with using fread and fwrite.
In one function I have written:
char *ab;
ab=malloc(10);
Then in another function I want to know the size of memory pointed by the ab pointer.
Is there any way that I can know that ab is pointing to 10 chars of memory?
No, you don't have a standard way to do this. You have to pass the size of the pointed-to memory along with the pointer, it's a common solution.
I.e. instead of
void f(char* x)
{
//...
}
use
void f(char *x, size_t length)
{
//....
}
and in your code
char *ab = malloc( 10 );
f( ab, 10 );
It's a deep secret that only free() knows for sure. It's likely in your system, but in a totally implementation dependent manner.
A bit awkward, but if you want to keep everything together:
typedef struct
{ // size of data followed by data (C only trick! NOT for C++)
int dimension; // number of data elements
int data[1]; // variable number of data elements
} malloc_int_t;
malloc_int_t *ab;
int dimension = 10;
ab = malloc( sizeof(*ab) + (dimension-1)*sizeof(int) );
ab->dimension = dimension;
ab->data[n] // data access
I've changed the data type to int to make the code a more generic template.
You can't (portably anyway). You have to keep track of the size yourself.
Some implementations of malloc could give you an API to access that information, but there is no provisions in the standard for this.
The size is what you passed into malloc, you can use a global variable or macro to remember it.
There is no way, you have to store the size of the allocated memory in another variable.
No, unfortunately.
You need to pass the size of the block along with the pointer.
No.
Now, that being said, there are non-portable hacks to do this, but it is not safe to rely upon them.
If you know with 100% certainty that the memory was allocated via malloc(), you may be able to rewind the pointer a few bytes and inspect the 'malloc node' that is used to track which parts of memory have been allocated and which have not. However, I can not stress this enough--do not ever depend upon this.
There is no way to deduce the size of allocated memory from the pointer itself. Since ab is a char *, sizeof(ab) is the same as sizeof(char *), which obviously is not the same as the size of the allocated chunk of memory.
Since you called malloc with the required size, you know what the size is. Pass this number along with the pointer to the function that needs to know the size.
I had a structure and a char pointer pointing to its memory address. So relating it to your question, I wanted to find the size of the memory location it was pointing to i.e. the size of the structure. So logically what you do is, find the size of the object the pointer creates to. This worked for me:
unsigned char * buffer= Library1Structure;
int x=sizeof(Library1Structure);
So the value of x tells me the size of the memory location the pointer buffer points to.