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);
}
Related
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.
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 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)).
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.
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.