struct forcePin {
char _name[512];
};
struct forcePin *_forcePin[500000];
_forcePin[i] = (struct forcePin *) malloc (sizeof (struct forcePin));
May I know what is the line as shown below doing?
_forcePin[i] = (struct forcePin *) malloc (sizeof (struct forcePin));
I am not familiar with c,if you can tell me how to make this line to be in C++ format as well.Thanks
Dynamic memory allocation is so important in C that you should really learn it properly.
What the line in question does is allocating memory from the heap, namely sizeof(struct forcePin) bytes. The malloc function returns a generic pointer to this allocated memory, and that pointer is assigned to the pointer _forcePin[i].
One thing about that line, you should not type-cast the return value of the malloc function.
In C++ you use the new statement to allocate pointers:
_forcePin[i] = new forcePin;
However, in C++ using pointers and dynamic heap allocations is discouraged. I would instead recommend you to use a std::vector of non-pointer structures:
struct forcePin {
std::string name;
};
std::vector<forcePin> forcePin;
forcePin.push_back(forcePin{});
It is calling the standard library function malloc(), to allocate sizeof (struct forcePin) bytes of dynamic ("heap") memory.
It is then pointlessly casting the returned pointer, and storing it in the variable _forcePin[i].
It's not the optimal way to write this code, in my opinion it should be:
_forcePin[i] = malloc(sizeof *_forcePin[i]);
Note that if the allocation is broken out of a loop (as the i implies), then that code looks like it's allocating 512 * 500,000 bytes, or around 244 MB of memory. Since it's done in half a million allocation calls, there will be considerable overhead, too.
If all the memory really is needed, it would be better to try for a single malloc() call and then split the allocated buffer into the 500,000 parts. Doing it that way would very likely be faster since malloc() can be expensive and half a million calls is a lot, but it would certainly save memory since there would be a one overhead cost rather than 500,000.
_forcePin[i] = (struct forcePin *) malloc (sizeof (struct forcePin));
Allocates a memory block of size forcePin, and casts the allocated memory from (void *) to the forcePin type. In C++ you would do:
_forcePin[i] = new forcePin();
or better yet, you can have:
std::vector<forcePin> vec;
vec.push_back(forcePin());
This line is your generic C-style memory allocation:
allocate memory (malloc) for exact number of bytes as is needed for structure forcePin (sizeof(struct forcePin)).
as the return value of malloc is a void pointer (void *) pointing to newly allocated memory, cast it to the pointer to forcePin structure ((struct forcePin *))
C++ version would be something like:
_forcePin[i] = new forcePin;
in C++, struct keyword is unnecessary when refering to struct type)
don't forget to free the memory when not needed by delete _forcePin[i]
Because it looks like you just may want to create all 500000 forcePins, you may do in in one step:
forcePin _forcePins = new forcePin[500000];
The line you ask about allocate a block of memory (see malloc()), and stores its address into the _forcePin array, at index i.
In C++, you would have used new, ie: _forcePin[i] = new forcePin
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 been working on a project that uses structs as storage for strings. I declared a struct consists of char type members:
struct datastore1
{
char name[50];
char address[50];
char email[50];
char number[50];
char idnum[50];
};
I'm aware that I can just do char *name, char *address... but let's say we specified it with max length of 50. Then on my function which uses the struct, I malloc'ed it with index size of 30:
struct datastore1 *dsdata = malloc(30 * sizeof(struct datastore1));
Supposedly I finished copying all strings into the struct by accessing each index, How should i free the allocated memory that was used after calling malloc? I tried doing free(dsdata) on the end of the program but I am not sure if it's the right way. Should i free each indexes individually? Please enlighten me. Thank you in advance for the feedback!
How should i free the allocated memory that was used after calling malloc?
Consider below example,
struct datastore1 *obj1 = malloc(sizeof(struct datastore1));
free(obj1);
Here obj1 is pointing to the block of memory of size same as size of datastore1 in order to free you need to send the address which is allocated by malloc.
likewise,
struct datastore1 *obj2 = malloc(3 * sizeof(struct datastore1));
free(obj2);
obj2 is pointing to a block of contiguous memory of size 3 * sizeof(datastore1) you need to pass the base address to free
Should i free each indexes individually?
NO, Since block of memory is allocated only once and you need to free exactly one time.
Let me extend it further,
struct datastore1 *obj3[3];
for(int i=0;i<3;i++)
obj3[i] = malloc(sizeof(struct datastore1));
for(int i=0;i<3;i++)
free(obj3[i]);
Here obj3 is array of pointer and each index is pointing to different part of memory and hence need to be freed individually.
Note: For simplicity I haven't considered return value from malloc. Null check has to be done on malloc return value.
1.
How should I free the allocated memory that was used after calling malloc?
I tried doing free(dsdata) on the end of the program but I am not sure if it's the right way.
free(dsdata) is fine, since you allocated the whole space by just one call to malloc with:
struct datastore1 *dsdata = malloc(30 * sizeof(struct datastore1));
To cite the standard (C18), 7.22.3.4 - "The malloc function" (emphasize mine):
7.22.3.4 The malloc function
Synopsis
1
#include <stdlib.h>
void* malloc(size_t size);
Description
2 The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate.
Returns
3 The malloc function returns either a null pointer or a pointer to the allocated space.
It is correct to use free(dsdata) because malloc allocated all of the required space at once and returned a pointer to the first structure variable of this array which is assigned to the pointer of dsdata.
The free() function "knows" that dsdata is a reference to the whole allocated space. You do not need to free each of the 30 structures of type struct datastore1 in memory individually.
2.
Should I free each indexes individually?
No, you do not need and even more important you should not do so; this would be Undefined Behavior:
Citation from the current standard (C18), 7.22.3.5/3 - "The free function" (emphasize mine):
Otherwise, if the argument does not match a pointer earlier returned by a memory management function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.
As far as I understand you only used malloc to allocate space for an array of datastore1 struct, so it is sufficient to just do free(dsdata).
If in the struct you would have pointers and you would use malloc to allocate each of them, only than you would need to free each of them first.
Both answers above are correct, but to fully understand how does it work I recommend you to learn how to use valgrind.
To check if program released memory correctly use
valgrind -v --leak-check=full --track-origins=yes ./your-program
This will execute your program on valgrind's virtual processor and give you full feedback about used resources.
Basically operator [] in C programming language in array definition context causes creation of (lets say to simplify) static array - this means that array is included in size of structure (if defined as part of structure) or is stored on the stack (if defined in function or globally).
The malloc function returns address of block of data you can use. Size of this block is at least as big as you requested. When you use free you release this block wich in this case means all data in block pointed by this address will be released.
I am developing a baremetal application. I am looking to adopt U-Boot source code for my application. I want to replace the following malloc calls found in U-Boot to statically allocate memory instead -
struct foo *ptr = malloc(sizeof(struct foo));
As per my understanding, this line only attempts to allocate enough space for one instance of the structure, and stores the address of that space in ptr.
Hence, I attempted to replace it with the following lines -
struct foo *ptr;
int struct_sz = sizeof(struct foo);
char buf[struct_sz];
ptr = &buf; //also tried ptr = buf;
However, this results in Stack Buffer Overflows with the return addresses of functions in the call stack being overwritten with junk.
How do I statically allocate memory instead of using malloc here?
What you are doing is allocating an array of characters and then having a pointer to a struct as pointing to the same memory.
While this is possible, you can run into memory alignment issues. The structure might have a requirement to start at a particular yte location. (e.g. a 32 bit int needs to start an address of a multiple of 4). A character array can start at any location.
An easier approach would be to declare a variable of struct foo and use the pointer to that variable. This variable needs to be global or static so that it will be present after the function scope is completed.
static struct foo var;
struct foo *ptr = &var;
To allocate many structs, you need a static array of structs
static struct arr[5];
struct foo *ptr = arr;
This approach will work if you allocate memory and do not free it very often. If you have many malloc and free you should consider writing your own allocator which will mimic the functionality of malloc
I have a structure:
struct generic_attribute{
int current_value;
int previous_value;
};
And a constructor which outputs a pointer to this structure:
struct generic_attribute* construct_generic_attribute(int current_value){
struct generic_attribute *ga_ptr;
ga_ptr = malloc (sizeof (struct generic_attribute));
ga_ptr->current_value = current_value;
ga_ptr->previous_value = 0;
return ga_ptr;
}
Now, in another function, I want to define a pointer and set it to point to the same address as the pointer that the above constructor outputs.
struct tagged_attribute* construct_tagged_attribute(int num_args, int *args){
...
struct generic_attribute* generic = malloc (sizeof(struct generic_attribute));
generic = construct_generic_attribute(args[0]);
...
}
It looks to me that what I am doing here is this:
1) I define a pointer "generic" and allocate a memory chunk to hold an instance of generic_attribute structure.
2) I call a function construct_generic_attribute within which, the program once again allocates a memory chunk of size of a generic_attribute structure. It outputs a pointer to this memory chunk.
3) In construct_tagged_attribute I set "generic" pointer equal to the pointer output by the construct_generic_attribute function, so now both of them point to the same memory slot.
However, it appears that I am allocating twice as much memory as I need to allocate.
Is there a way for me to allocate memory only once without getting a segmentation fault for failing to allocate space for "generic" pointer? Alternatively, am I misunderstanding what is happening in this code?
struct generic_attribute* generic = construct_generic_attribute(args[0]);
Should do the trick. Pointer variable is just that, a variable. You can trade pointer values around just like numbers.
Yes, you're misunderstanding, but I can't quite figure out what you think is happening to explain how it's wrong.
struct generic_attribute *generic = construct_generic_attribute(args[0]); a pointer is a kind of value. If you assign a pointer to another, you get two pointers to the same thing, without any allocation of memory. Since C doesn't manage memory for you, it's up to you to make sure that any object that's allocated is freed exactly once, and that you don't try to use pointers to an object after it's been freed.
Here
struct generic_attribute* generic = malloc (sizeof(struct generic_attribute));
you allocate a memory block, big enough to keep a generic_attribute structure, then store a pointer to that structure (technically: an address of the block) in the generic variable. Note: you do not initialize the structure members.
Then in
generic = construct_generic_attribute(args[0]);
you call a function, which internally allocates (another) block of memory and initializes it and returns a pointer to it (which was stored in a ga_ptr variable during the function execution). The pointer returned is then assigned to the generic variable, overwriting the value stored there by a previous instruction. Consequently you loose an access to the first allocated structure.
EDIT
I'm afraid I do not quite understand what you're trying to achieve. If you want two pointers to the same structure, just declarega1 and assign it a pointer to the created structure:
struct generic_attribute *ga1 = construct_generic_attribute(args[0]);
then make a copy of the pointer:
struct generic_attribute *ga2 = ga1;
I would like to know if there's a real difference between this:
c = (struct_t *) malloc(sizeof(struct_t));
and this
c = malloc(sizeof(struct_t *));
Besides avoid the cast, is the compiler takes any advantage in the second form respect the first? Or the two ways are completely the same and is just a "aesthetical" question ?
The first allocates sizeof(struct_t) bytes, the second sizeof(struct_t*) bytes.
Apart from that, there is no difference in what malloc does, whether you cast the result or not. Casting the result makes the code acceptable to C++ compilers, but it can hide the mistake of not including stdlib.h, therefore it is widely preferred to not cast the result in C.
The two are totally different. The first allocates an instance of the struct, whereas the second allocates a pointer to the struct.
In general, they won't even allocate the same number of bytes.
No, they are not the same. The latter allocates 4 or 8 bytes of space for a pointer to struct, the first allocates enough space for the struct it self.
When sizeof(struct_t) is small enough, and when the malloc allocates actually more than requested, the user may not see the difference.
Two forms are different. They both allocate memory, but with different amounts.
General rule is as follows:
when allocating type T, the result of malloc shall be casted to T*.
void sample1()
{
struct pollfd *pfd = (struct pollfd*)malloc(sizeof(struct pollfd));
// pfd is points to a memory with a size of struct pollfd
...
free(pfd);
}
void sample2()
{
struct pollfd *pfd = (struct pollfd*)malloc(sizeof(*pfd));
// same as above, but uses variable type instead
free(pfd);
}
If you specify incorrect type in malloc argument, generally that will lead to buffer overrun problems:
void sample3()
{
struct x *px= (struct x*)malloc(sizeof(struct x*));
x->field = 5; //<< error, as allocated only 4 or 8 bytes depending on pointer size
}
Both are different.
Usually malloc returns (void*). So you want to typecast void* to (struct_t*).