Allocating memory to ** [closed] - c

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)).

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));

Void* when allocating [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 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));

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.

Can't understand program (sorting of array of structures) [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
I have a code: array of structures that must be sorted. Program works, but:
I can't understand, if Mat is pointer, why not void sort(tArt *sMat), but void sort(tArt sMat[]). I'm really puzzled.
typedef struct{
char data[26];
}tArt;
...
int main(void)
{
FILE* fMat; fMat=fopen..........
tArt* Mat;
...
Mat=malloc(sizeof(tArt));
for(i=0;i<N;i++) fread(&Mat[i],sizeof(tArt),1,fMat);
fclose(fMat);
sort(Mat,N);
...
}
void sort(tArt sMat, int num) {...........}
My guess since we can not see the entire code, is that when you use malloc to dynamically allocate the array you forget to allocate the array for N 'objects'. In other words, I suspect your problem lies in the line
Mat = malloc(sizeof(tArt));
where it should be
Mat = malloc(sizeof(tArt) * N);
On the other hand, when you create explicitly your array with a declaration of the form
tArt Mat[N];
where N is defined somewhere earlier in the ellipses, everything is working as expected.
Hope this helps.
There is no difference between tArt *sMat and tArt sMat[] and tArt sMat[1234], the compiler treat them all as tArt *sMat and ignore the length information.

Resources