Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
What I wrote previously:
int abc = 0;
char *str = NULL;
struct B *b = NULL;
My boss asked me to align equal like this:
int abc = 0;
char *str = NULL;
struct B *b = NULL;
Well, I can live with this above one, but how about this??
int abc = 0;
char *str = NULL;
struct B *b = NULL;
struct VeryLongStruct *very_long_struct = NULL;
What is the suggested way?
As with most subjective questions, there are good and bad points. Selecting one from each side at random:
It's a good thing because it makes the code easier to read.
It's a bad thing because keeping them aligned (when you add a longer variable name for example) will result in unnecessary changes. Specifically, it's annoying when git history shows a slew of lines that have changed when they actually haven't.
Related
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);
}
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));
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
im having a txt file
abc=123 def=456 ...
I have defined my struct like this:
typedef struct rule {
char* old;
char* new;
}Rule;
I have counted the ammount of these rules via function
int count_rules();
Now I'm calling this function in another, while making dictionary of rules
void make_dic(){
ammount_rules = count_rules();
//here goes the problem
Rule *dictionary = malloc(ammount_rules * sizeof(Rule));
}
I want to scan another txt and replace old with new so I'd like to acces every twin with simple command
for (i=0; i<ammount_rules;i++){
if ( (string_in_text) == (dictionary.old[i]) )
{
printf("%s" dictionary.new[i]);
}
}
Your malloc seems to be fine. It allocates an array of Rule with ammount_rules elements.
However, the way you use dictionary seems wrong.
dictionary.old[i] // wrong
dictionary[i].old // correct
dictionary.new[i] // wrong
dictionary[i].new // correct
BTW: Notice that you are comparing pointers - not strings. Use strcmp for comparing strings.
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));
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.