Void* when allocating [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
When is it appropriate to use
void* space_to_use = malloc(size);

void* space_to_use = malloc(size);
// malloc always return void pointer that means it can be typecast to any type.
// before using void pointer it is necessary to typecast it into proper type.
// for example:-
// if size is 8 byte.It will allocate 8 byte of memory.
/*
void* space_to_use = malloc(size);
char * ptr = (char*)space_to_use;
*/
// These two line can be combine in one statement.
char * ptr = (char*)malloc(size*sizeeof(char));
// NOTE:sizeof(char) is to make sure platform independent.
// Same for int if we want to store some integer.
int * ptr = (int*)malloc(size*sizeeof(int));

Related

does the following code in c produce a memory leak? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to know if the following piece of code will produce a memory leak:
m = malloc(5);
m = NULL;
Yes, there is a memory leak. The 5 allocated bytes are no longer accessible as you don't have a pointer to them.
If you save the pointer, you can still use, and free, the resources
unsigned char *m = malloc(5);
if (m) {
unsigned char *p = m;
m = NULL; // can no longer access the memory through m
p[2] = 1; // but p is ok
free(p); // p is ok to free
} else {
fprintf(stderr, "Problem! malloc failed!\n");
exit(EXIT_FAILURE);
}

There are pointer members in the C language structure, and how to output the structure data to the serial port [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
char *ssid,num;
ssid = malloc(sizeof(char)*6);
memcpy(ssid,"123456",6);
struct stu{
uint8_t head;
char *name;
uint8_t end;
} stu1 = {0x55, ssid, 0xf0 };
//printf("%s",stu1.name);
//how to output the structure data to the serial port.
send_msg((char *)&stu1,sizeof(stu1));
free(ssid);
Since your struct contains a pointer (i.e. name), it makes no sense to send the whole struct to the serial port. A pointer is something local to the program that it doesn't make sense to print.
So you need to print the members one by one. Like:
send_msg(&stu1.head, sizeof stu1.head);
send_msg(stu1.name, 6);
send_msg(&stu1.end, sizeof stu1.end);
BTW....
Notice that name is not zero terminated. That means that name is not a string. I guess you want it to be a string. So you should do:
int len = strlen("123456");
ssid = malloc(len + 1); // notice + 1 for the zero termination
memcpy(ssid,"123456", len + 1); // or strcpy(ssid, "123456");
and then you can do
send_msg(stu1.name, strlen(stu1.name));

Allocating memory to ** [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have this struct
typedef struct{
char **palavras;
}no;
and I want to allocate memory to this array of strings basically
and I can't do this, since it says it expects something before the '('
no *atual;
atual->(*palavras)=calloc(1,sizeof(char*));
You need to do it in several stages:
First, allocate memory to atual,
Next, allocate memory to palavras
Finally, allocate memory to elements of palavras
Assuming that you need to allocate 10 palavras, you can do it like this:
no *atual = malloc(sizeof(no));
atual->palavras = malloc(sizeof(char*)*10);
atual->palavras[0] = malloc(20);
...
You should access palavras by atual->palavras, e.g. atual->palavras = calloc(5, sizeof(char *)), and dereference the char ** by *atual->palavras. You may also allocate memory for the char * pointers by atual->palavras by, say, atual->palavras[0] = malloc(10 * sizeof(char)).

Unable to malloc char** [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is someone possible to explain me what I am doing wrong here?
I am trying to malloc "multi-dimensional" dynamic array like this.
Thank you
enum { MAX_WORDS = 100, MAX_LENGHT = 20 };
char **words;
// it fails here "void * cannot be assigned... type of char**"
words = malloc(MAX_WORDS * sizeof(char*));
for (int i = 0; i < MAX_WORDS; i++) {
words[i] = malloc(MAX_LENGHT * sizeof(char));
}
words = malloc(MAX_WORDS * sizeof(char*));
Should read
words = (char**)malloc(MAX_WORDS * sizeof(char*));
Your compiler is mad because you are attempting to assign a void pointed to a char pointer, so you need to typecast it to char** to work properly.
EDIT: Apparently this is because I am using a C++ compiler. In C you should not be casting the result of malloc(). If you are using C++ you should switch to new and delete if possible.

Pointer errors with arrays [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
DIR * d;
int dsize=0;
struct dirent * de;
char *dir[1024];
d=opendir(".");
while ((de = readdir(d)))
{
if((de->d_type) & DT_DIR)
{
dir[dsize]= de->d_name;
dsize++;
}
}
I'm trying to store the address of the file names into a array of char pointers.
A bit rusty on pointers I went back and read some pages of pointer review but I'm
not sure what I'm doing wrong.. Keeps telling me "warning: assignment makes integer from pointer without a cast". Is my syntax just off because of the struct?
You cannot store the pointers that way. They are overwritten every time, you call readdir and then you have a dangling pointer to invalid memory. If you want to store the dir entries, you must copy the whole name, not just the pointer
char dir[1024][256];
while (de = readdir(d)) {
if (de->d_type & DT_DIR) {
if (dsize < 1024) {
strcpy(dir[dsize], de->d_name);
dsize++;
}
}
}
Don't forget the check for the dir array bounds. Otherwise you risk overwriting the stack, which might result in a crash.

Resources