How to make malloc to a struct inside of another struct? - c

In the first .h file, i have this struct:
typedef struct system
{
char* name;
DArray Info;
} *System;
And in the .c file I have this function:
System createSystem(char *name){
if (!name){
return NULL;
}
System newSystem=malloc(sizeof(*newSystem));
if (!newSystem){
return NULL;
}
newSystem->name=malloc(sizeof(strlen(name)+1));
if (!newSystem->name){
free(newSystem);
return NULL;
}
strcpy(newSystem->name,name);
newSystem->Info=malloc(sizeof *(newSystem->Info));
if (!newSystem->Info){
free(newSystem->name);
free(newSystem);
return NULL;
}
newSystem->Info->x=0;
newSystem->Info->elements=NULL;
return newSystem;
}
In another .h file I have the struct dArray:
typedef struct dArray
{
int x;
Element *elements;
} *DArray;
where Element could be of any type.
However, the function always stops working in Eclipse and I get the error
hw stopped working
I know that the problem is in this line:
newSystem->Info=malloc(sizeof(*newSystem->Info));
but I don't understand why this is a problem as I am trying to malloc to the struct DArray in a regular way!
I have been using this test in the main file:
int main() {
sys=createSystem("ss1");
if (sys) {
printf ("ok");
return 0;
}
Any help would be appreciated.

System createSystem(char *name){
if (!name){
return NULL;
}
System newSystem=malloc(sizeof *newSystem);
if (!newSystem){
return NULL;
}
newSystem->name=malloc(sizeof *newSystem->name * (strlen(name)+1)); // <-- change here.
if (!newSystem->name){
free(newSystem);
return NULL;
}
strcpy(newSystem->name,name);
newSystem->Info=malloc(sizeof *newSystem->Info);
if (!newSystem->Info){
free(newSystem->name);
free(newSystem);
return NULL;
}
newSystem->Info->x=0;
newSystem->Info->elements=NULL;
return newSystem;
}
In case you are copying the string to dynamically allocated memory, it(malloc) basically allocate sizeof (strlen(str)+1) bytes of memory. And that will be nothing but sizeof operator applied to size_t which is unlikely to hold a characters string. (5 characters in my machine).
(why size_t? Because the function strlen has a signature size_t strlen(const char *s);)
Also in my system sizeof size_t is 4 bytes. So basically you are allocating 5 bytes. That means the string will consists of 5 characters including the nul terminating character.
Anything more than length more comes like "ABCDED", you are writing out of the memory you allocated causing an illegal memory access - and this has undefined behavior. In your case it simply stops.
To add more clarification, in your case I guess when you are entering the string you are passing something more than length 4. But if you pass the string "ss1" then this will work.
newSystem->name=malloc(sizeof *newSystem->name * (strlen(name)+1)); can be more clearly written as newSystem->name=malloc(strlen(name)+1);. As sizeof char is 1 byte we have avoided it.
You may later try to look up strdup function which is not part of ISO standard.

Also, the following is not at all what you are after:
newSystem->name=malloc(sizeof(strlen(name)+1));
That is going to malloc() a buffer of (at minimum) sizeof(size_t) not a buffer capable of holding strlen(name)+1 bytes -- size_t is the return type of strlen and that is what the sizeof is being applied to.

below statement are not correct:-
System newSystem=malloc(sizeof(*newSystem)); it should be System newSystem=malloc(sizeof(struct system));
and newSystem->Info=malloc(sizeof *(newSystem->Info)); is newSystem->Info is a pointer? if pointer it should be newSystem->Info=malloc(sizeof(DArray));

Related

Assign characters to arrow pointer in c

struct room
{
char venue[15]; //variable
}
*stream;
.
.
.
void idealounge(){
int i,idealounge[10];
char room1[10]="MMLC";
*** ** stream->venue = room1;*****
}
This is a room booking system.
The name of the room called MMLC.
I expect it can store "MMLC" to stream->venue but it shows errors like "incompatible values" etc.
The statement:
char venue[15];
declares venue to be an array of size 15 of char. Then,
char room1[10]="MMLC";
initialises the first 4 bytes of room1 with the string literal "MMLC", setting the rest to \0.
Then,
stream->venue = room1;
is invalid, because venue is an array, not a pointer. Arrays are not modifiable l-values. You can't assign arrays like this.
If you want to copy the contents of room1 to the contents of venue, use standard strcpy().
That being said,
struct room
{
char venue[15]; //variable
}
*stream;
only allocates space for the pointer, which is uninitialized and isn't pointing to anything meaningful. So first allocate memory and initialise the pointer:¹
stream = malloc (sizeof (struct room));
Then check if it succeeded:²
if (!stream) {
perror ("malloc()");
/* handle error here...*/
}
Now perform the string copy.
Alternatively, you can allocate the struct with automatic storage duration, which is simpler and less error-prone:
struct room bedroom;
and then assign it's address to the pointer:
stream = &bedroom;
This avoids the need for dynamic memory allocation, which you might fail to free() later on.
[1] — NB that I do not cast the result of malloc(). malloc() returns a generic void *, or void pointer, which is automatically promoted to the correct type. So there's no need to cast, and doing so might hide a potential bug.
See also: Do I cast the result of malloc?
[2] — POSIX-compliant systems set errno on malloc() failure, the above code snippet assumes it does. But you may not wish to take it for granted, and handle the error in another way.
struct room
{
char venue[15]; //variable
}
*stream = NULL;
void idealounge() {
int i;
int idealounge[10];
char room1[10]="MMLC";
stream = (struct room *)malloc(sizeof(struct room));
strcpy(stream->venue, room1);
}
You should write this way.

program crashes at repetitive calloc() call

Edit: solved by kaylums little comment. Thank you!
good morning,
I am relatively new to C still and I'm trying to make a doubly linked list.
I got my program to run properly with all the functions with this kind of element:
the program crashes after either 2 or 3 inserted elements in the list in the calloc() call of my insertElement() function. I don't get any SIGSEGV or anything, the program just stops with a random negative return.
I'll try to give a minimum code example of the function and the function call:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct Element {
char name[30];
}Element;
typedef struct List {
int size;
Element* first;
Element* last;
}List;
Element* insertElement(List* List, char name[30]) {
Element* element;
element = (Element*)calloc(0, sizeof(Element));
strncpy_s(element->name, name, 30);
return element;
}
List globalList;
char name[30];
int main() {
while (true) {
printf("insert the name >>");
if (fgets(name, 30, stdin) != NULL)
name[strcspn(name, "\n")] = 0;
insertElement(&globalList, name);
}
}
is there already something obvious wrong with that basic stuff?
Thank you very much in advance! Any advice would be very much appreciated, have a good day!
element = (Element*)calloc(0, sizeof(Element));
what is 0 in first argument?
actually you ask for 0 number of your type from memory!
here is some explanation about dynamic memory allocation:
Dynamic memory allocation is a process of allocating memory at run time. There are four library routines, calloc(), free(), realloc(), and malloc() which can be used to allocate memory and free it up during the program execution. These routines are defined in the header file called stdlib.h.
What is malloc() ?
It is a function which is used to allocate a block of memory dynamically. It reserves memory space of specified size and returns the null pointer pointing to the memory location.
The pointer returned is usually of type void. It means that we can assign malloc function to any pointer. The full form of malloc is memory allocation.
What is calloc() ?
Calloc() function is used to allocate multiple blocks of memory. It is a dynamic memory allocation function which is used to allocate the memory to complex data structures such as arrays and structures. If this function fails to allocate enough space as specified, it returns will null pointer. The full form of calloc function is contiguous allocation.
Why use malloc() ?
Here are the reasons of using malloc()
You should use malloc() when you have to allocate memory at runtime.
You should use malloc when you have to allocate objects which must exist beyond the execution of the current memory block.
Go for malloc() if you need to allocate memory greater than the size of that stack.
It returns the pointer to the first byte of allocated space.
It enables developers to allocate memory as it is needed in the exact amount.
This function allocates a memory block size of bytes from the heap.
Why use calloc() ?
Here are the reasons of using calloc()
When you have to set allocated memory to zero.
You can use calloc that returns a pointer to get access to memory heap.
Used when you need to initialize the elements to zero to returns a pointer to the memory.
To prevent overflow that is possible with malloc()
Use calloc() to request a page that is known to already be zeroed.
Syntax of malloc()
Here is a Syntax of malloc()
ptr = (cast_type *) malloc (byte_size);
n above syntax, ptr is a pointer of cast_type. The malloc function returns a pointer to the allocated memory of byte_size.
Example of malloc() in C
In the bellow code, sizeof(*ptr) is used to allocate a memory block of 15 integers. In the printf statement, we are finding the value of the 6th integer.
#include<stdlib.h>
#include<stdio.h>
int main(){
int *ptr;
ptr = malloc(15 * sizeof(*ptr));
if (ptr != NULL) {
*(ptr + 5) = 480;
printf("Value of the 6th integer is %d",*(ptr + 5));
}
}
Output:
Value of the 6th integer is 480
Syntax of calloc()
Here is a Syntax of malloc()
ptr = (cast_type *) calloc (n, size);
The above syntax is used to allocate n memory blocks of the same size. After the memory space is allocated, all the bytes are initialized to zero. The pointer, which is currently at the first byte of the allocated memory space, is returned.
Example of calloc() in C
The C language program below calculates the sum of the first ten terms. If the pointer value if null, then the memory space will not be allocated.
For loop is used to iterate the value of a variable "i" and print the sum. Lastly, function free is used to free-up the pointer.
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, * ptr, sum = 0;
ptr = calloc(10, sizeof(int));
if (ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
}
printf("Building and calculating the sequence sum of the first 10 terms \n");
for (i = 0; i < 10; ++i) { * (ptr + i) = i;
sum += * (ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}
Output:
Building and calculating the sequence sum of the first 10 terms n Sum = 45
I will not extend on the actual problem (specifying 0 as the number of elements requested to calloc()). I will point you to several other things found in your code.
The first problem in reading your code is that you lack to include the file <stdbool.h>, necessary to use the constants true and false and the type bool. I have added it in the first line.
#include <stdbool.h>
Next, you use at several places the value 30 as the size of several objects that are all related. If you decide in the future to change that value, it will be difficult to find all the ocurrences of the constan 30 and change all of them (and the risk you have used also 30 for anything else and it gets changed in the middle)
I have included a constan with the following lines:
#define NAME_LENGTH (30)
and all the definitions:
...
char name[NAME_LENGTH];
in the structure...
Element* insertElement(List* List, char name[NAME_LENGTH]) {
in the prototype of insertElement (you don't need as name is actually defined as char *, not as an array of NAME_LENGTH elements...
On other side, you need to include a pointer on each Element to link each to the next element of the list. This is done right after name:
struct Element *next; /* we need to include struct as the type Element is not yet defined */
Next, include sizeof *element as the second parameter to calloc() and 1 to the first. Better, if you are going to initialize all fields in the Element structure, then it is better to call malloc() (see the final code , posted at the end)
NEVER, NEVER, NEVER cast the value returned by malloc()
(and friends) This is a legacy that causes a lot of
errors, that get undetected (and very difficult to find),
due to the cast. When you cast you tell the compiler:
leave it in my hands, as I know what I'm doing. And this
makes the compiler silent, when it should be complaining.
The problem mainly has to do with forgetting to include
the header file where malloc (and friends) are declared
(<stdlib.h>) and you will take long time to detect and
see why your program has crashed.
For the same reason, don't use the size of the type, when
you can use the pointed to expression as template of the
type. This is because if you change the type of the
pointed to object, you need to remember that here you have
put the type of the object (and you need to change it too)
This way, this expression
will only be bad if you change the object into a non
pointer object. Also, you have requested for 0 elements
of the specified type, which has already been noticed in other answers. This will make calloc() to return NULL, value you don't check in your code, and you try to use it later on. This will crash your program, but in the best case, it is Undefined Behaviour (and a very difficult error to find, so be careful and always check the value returned by malloc()).
Next, don't use strncpy_s() as it is Microsoft specific routine, and isn't included in any standard. A proper substitute has been provided by strncpy():
strncpy(element->name, name, sizeof element->name);
also use the sizeof operator, as it protects you if you decide in the future to change the type of the pointer.
Finally, it is better to use fgets() as the test expression for the while statement in main(). The reason is that you can end the loop when the end of file is detected.
Finally, you code ends as (including the linking of Elements in the linked list):
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define NAME_LENGTH (30)
typedef struct Element {
char name[NAME_LENGTH];
struct Element *next;
} Element;
typedef struct List {
int size;
Element* first;
Element* last;
} List;
Element* insertElement(List* List, char name[NAME_LENGTH]) {
Element* element;
/* NEVER, NEVER, NEVER cast the value returned by malloc
* (and friends) This is a legacy that causes a lot of
* errors, that get undetected (and very difficult to find),
* due to the cast. When you cast you tell the compiler:
* leave it in my hands, as I know what I'm doing. And this
* makes the compiler silent, when it should be complaining.
* The problem mainly has to do with forgetting to include
* the header file where malloc (and friends) are declared
* (<stdlib.h>) and you will take long time to detect and
* see why your program has crashed. */
/* for the same reason, don't use the size of the type, when
* you can use the pointed to expression as template of the
* type. This is because if you change the type of the
* pointed to object, you need to remember that here you have
* put the type of the object. This way, this expression
* will only be bad if you change the object into a non
* pointer object. Also, you have requested for 0 elements
* of the specified type. */
element = malloc(sizeof *element);
/* don't use strncpy_s as it is not standard. Use the sizeof
* operator again, to protect the expression if you change
* the type of element->name */
strncpy(element->name, name, sizeof element->name);
element->next = NULL;
if (List->last) {
List->last->next = element;
List->last = element;
} else {
List->first = List->last = element;
}
return element;
}
List globalList;
char name[NAME_LENGTH];
int main() {
/* if you put the fgets() call as the test of the while
* statement below, you will process each line until you get
* an end of file condition. Then you can do both things: to
* null the occurence of the \n char, and the call to
* insertElement() I have not corrected because it's a
* question of taste. */
printf("insert the name >> ");
while (fgets(name, sizeof name, stdin) != NULL) {
/* sizeof name is better than the constant, as if you
* change the type definition of object name, you have to
* remember that you are using here its size. sizeof
* does the job for you. */
name[strcspn(name, "\n")] = 0;
insertElement(&globalList, name);
printf("insert the name >> ");
}
Element *p;
char *sep = "\n\n{ ";
for (p = globalList.first; p; p = p->next) {
printf("%s\"%s\"", sep, p->name);
sep = ", ";
}
printf(" };\n");
}

filling dynamically allocated struct inside function

I'm trying to make a function that accepts a pre-allocated memory pointer as input and fills an array of structs at that location with data. In this example, I expected the output to be:
W 100
L 200
However, the first line is correct, but the second line prints no character and a zero. What am I doing wrong?
typedef struct{
char word;
long number;
}record;
void makerec(record** data){
data[0]->word='W';
data[0]->number=100;
data[1]->word='L';
data[1]->number=200;
}
int main(){
record* data=(record*)malloc(sizeof(record)*1000);
makerec(&data);
printf("%c %ld\n",data[0].word,data[0].number);
printf("%c %ld\n",data[1].word,data[1].number);
free(data);
return 0;
}
You're not dealing with the right types. Simply change:
void makerec(record** data) {
to:
void makerec(record * data) {
and:
makerec(&data);
to:
makerec(data);
as well as changing data[0]->word='W'; and friends to data[0].word = 'W';
data is already a pointer, and you want to change the thing to which it points, so you can just pass it directly to makerec. You'd pass a pointer to data if you wanted makerec() to make it point to something different, but that's not what you're doing here, so just passing data itself is correct.
Incidental to your main issue, but:
record* data=(record*)malloc(sizeof(record)*1000);
should be:
record* data = malloc(1000 * sizeof *data);
if ( !data ) {
perror("memory allocation failed");
exit(EXIT_FAILURE);
}
Notes:
You don't need to (and, to my mind, shouldn't) cast the return value of malloc() and friends
sizeof *data is better than sizeof(record), since it continues to work if the type of data changes, and more importantly, it removes the possibility of you applying the sizeof operator to an incorrect type, which is a common mistake.
Reversing the positions of 1000 and sizeof *data is merely cosmetic, to make the multiple '*'s easier to comprehend.
You should always check the return value of malloc() in case the allocation failed, and take appropriate action (such as exiting your program) if it did.

how can i know the allocated memory size of pointer variable in c [duplicate]

This question already has answers here:
Determine size of dynamically allocated memory in C
(15 answers)
Closed 3 years ago.
I have faced some problem in this case can you please your ideas.
main()
{
char *p=NULL;
p=(char *)malloc(2000 * sizeof(char));
printf("size of p = %d\n",sizeof (p));
}
In this program Its print the 4 that (char *) value,but i need how many bytes allocated for
that.
You could also implement a wrapper for malloc and free to add tags (like allocated size and other meta information) before the pointer returned by malloc. This is in fact the method that a c++ compiler tags objects with references to virtual classes.
Here is one working example:
#include <stdlib.h>
#include <stdio.h>
void * my_malloc(size_t s)
{
size_t * ret = malloc(sizeof(size_t) + s);
*ret = s;
return &ret[1];
}
void my_free(void * ptr)
{
free( (size_t*)ptr - 1);
}
size_t allocated_size(void * ptr)
{
return ((size_t*)ptr)[-1];
}
int main(int argc, const char ** argv) {
int * array = my_malloc(sizeof(int) * 3);
printf("%u\n", allocated_size(array));
my_free(array);
return 0;
}
The advantage of this method over a structure with size and pointer
struct pointer
{
size_t size;
void *p;
};
is that you only need to replace the malloc and free calls. All other pointer operations require no refactoring.
There is no portable way but for windows:
#include <stdio.h>
#include <malloc.h>
#if defined( _MSC_VER ) || defined( __int64 ) /* for VisualC++ or MinGW/gcc */
#define howmanybytes(ptr) ((unsigned long)_msize(ptr))
#else
#error no known way
#endif
int main()
{
char *x=malloc(1234);
printf( "%lu", howmanybytes(x) );
return 0;
}
Although it may be possible that some libraries allows you to determine the size of an allocated buffer, it wouldn't be a standard C function and you should be looking at your library's own documentations for this.
However, if there are many places that you need to know the size of your allocated memory, the cleanest way you could do it is to keep the size next to the pointer. That is:
struct pointer
{
size_t size;
void *p;
};
Then every time you malloc the pointer, you write down the size in the size field also. The problem with this method however is that you have to cast the pointer every time you use it. If you were in C++, I would have suggested using template classes. However, in this case also it's not hard, just create as many structs as the types you have. So for example
struct charPtr
{
size_t size;
char *p;
};
struct intPtr
{
size_t size;
int *p;
};
struct objectPtr
{
size_t size;
struct object *p;
};
Given similar names, once you define the pointer, you don't need extra effort (such as casting) to access the array. An example of usage is:
struct intPtr array;
array.p = malloc(1000 * sizeof *array.p);
array.size = array.p?1000:0;
...
for (i = 0; i < array.size; ++i)
printf("%s%d", i?" ":"", array.p[i]);
printf("\n");
It is impossible to know how much memory was allocated by just the pointer. doing sizeof (p) will get the size of the pointer variable p which it takes at compile time, and which is the size of the pointer. That is, the memory the pointer variable takes to store the pointer variable p. Inside p the starting address of the memory block is stored.
Once you allocate some memory with malloc it will return the starting address of the memory block, but the end of the block cannot be found from it, as there is no terminator for a block. You define the end of the block therefore you need to identify it by any means, so store it somewhere. Therefore you need to preserve the block length somewhere to know where the block which is pointed to by p ends.
Note: Although the memory allocation structure keeps track of allocated and unallocated blocks, therefore we can know the allocated memory block length from these structures, but these structures are not available to be used by the users, unless any library function provides them. Therefore a code using such feature is not portable (pointed by #Rudy Velthuis) . Therefore it is the best to keep track of the structure yourself.
You need to keep track of it in a variable if you want to know it for later:
char *p = NULL;
int sizeofp = 2000*sizeof(char);
p = (char *)malloc(sizeofp);
printf("size of p = %d\n",sizeofp);
You cannot use the sizeof in this case, since p is a pointer, not an array, but since you allocate it, you already know:
main()
{
size_t arr_size = 2000;
char *p=NULL;
p=malloc(arr_size * sizeof(char));
printf("size of p = %d\n",arr_size);
}
Edit - If the malloc fails to allocate the size you wanted, it won't give you a pointer to a smaller buffer, but it will return NULL.

What to return if a function has an error?

gcc 4.5.1 c89
I have a function that assigns the elements of the following structure:
static struct Config_t {
char protocol[LINE_SIZE];
char mode[LINE_SIZE];
} *app_config = NULL;
The function using malloc and memset to assign and clear the memory.
Once that is done, I have functions that gets the individual elements:
char* get_mode()
{
if(app_config->mode != NULL) {
return app_config->mode;
}
return NULL;
}
Here I am checking that a value has been assigned. And returning NULL if it hasn't. So in the calling function I can check if a NULL is returned. However, if there a better way to do this?
Many thanks for any suggestions,
In C, returning NULL for an error condition is standard practice for functions returning pointers, so you're in good shape there. (For functions that don't return pointers, the usual convention is to return 0 on success and a non-zero error code on error.)
Separately, and perhaps a bit off-topic: mode can't be NULL, though, can it? app_config can certainly be NULL, it's a pointer to a structure, but your mode is defined as an array, an intrinsic part of the struct, not as a pointer. You'll either have the struct, or not, but you won't have only part of the struct. Simply allocating the memory for the struct will allocate the LINE_SIZE chars for mode; in fact, sizeof(struct Config_t) == LINE_SIZE + LINE_SIZE, the structure is an array of characters followed by another array of characters. There are no pointers involved (other than app_config, because you've defined it as a pointer to the structure).
Consequently, to fully allocate your struct Config_t, just do this:
app_config = malloc(sizeof(*app_config));
(or app_config = malloc(sizeof(struct Config_t)); if your platform won't allow the above.) That allocates mode, nothing else required.
If mode were defined as a char *, that would be different:
static struct Config_t {
char *protocol;
char *mode;
} *app_config = NULL;
Now sizeof(struct Config_t) == 2 * sizeof(void*) (see below), the structure itself consists only of two pointers, not any data that they may point to. Allocating the structure does not allocate any storage for them.
#include <stdio.h>
#define LINE_SIZE (200)
struct Config_t {
char protocol[LINE_SIZE];
char mode[LINE_SIZE];
};
struct Config_t_with_pointers {
char *protocol;
char *mode;
};
int main(int argc, char* argv[])
{
printf("sizeof(struct Config_t) = %zu\n", sizeof(struct Config_t));
printf("sizeof(struct Config_t_with_pointers) = %zu\n", sizeof(struct Config_t_with_pointers));
return 0;
}
(Given your compiler, I felt free to use the z format specifier for size_t arguments, as any recent gcc has it [and it's in the C99 standard, Matthew tells us].)
Output (on my 64-bit Linux system):
sizeof(struct Config_t) = 400
sizeof(struct Config_t_with_pointers) = 16
Based on the definition of built in C functions, such as gets(), returning NULL is the ideal way to indicate that it was not possible to make an assignment or allocation.
Well, since the only reason that this happens is one (if no value has been assigned), you don't need more than one value to distinguish between them.
Also, NULL is no valid pointer, so I don't see anything wrong with that.
If you had more possible errors, you could use errno.h for that purpose.

Resources