Malloc and Free unsigned char!?

please help me.
i want to Read/Write fingerprint module by serial port, and to this approaches
i should be send and received HEX code, so i defined any unsigned char operator to this work, Do is this beater solve to this problem?
i should be controlled memory leakage because memory of embedded processor is limited.
i want to defined unsigned char by "malloc" and "free" for this problem, but this problem annoy me..
Please help..
simple my problem

/* malloc example:*/
#include <stdio.h> /* printf, scanf, NULL */
#include <stdlib.h> /* malloc, free, rand */

//////////////////////////////////////////////
unsigned char mehdi[24] = {0x55, 0xAA, 0x02, 0x01, 0x03, 0x02, 0xc0, 0xd0, 0x0e, 0x07, 0x60, 0x90, 0xf0, 0x40, 0x50, 0xa0, 0xf0, 0xc0, 0xb0, 0x04, 0x80, 0xd0, 0x02, 0x01};





int main ()
{
int i,n;
char * buffer;
unsigned char* man;
man = (unsigned char*)malloc(64);


printf("/////////////////////////////befor free()/////////////////////////////////////\n");

for(int i=0;i<24;i++)
man[i] = mehdi[i];
man[24]='\0';


for(i=0;i<24;i++)
{
printf("befor free man[%d]==0x%x\n",i,man[i]);
}



free ((unsigned char*)man);


printf("/////////////////////////////after free()/////////////////////////////////////\n");


for(i=0;i<24;i++)
{
printf("befor free man[%d]==0x%x\n",i,man[i]);
}

printf("\nonly just 0-8 data of pointer ereased!!\nwhat dont erease all data from pointer??\nplease help me :-(");
return 0;
}
your code, indented
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* malloc example:*/
#include <stdio.h>  /* printf, scanf, NULL */
#include <stdlib.h> /* malloc, free, rand */

//////////////////////////////////////////////
unsigned char mehdi[24] = {0x55, 0xAA, 0x02, 0x01, 0x03, 0x02, 0xc0, 0xd0,
                           0x0e, 0x07, 0x60, 0x90, 0xf0, 0x40, 0x50, 0xa0,
                           0xf0, 0xc0, 0xb0, 0x04, 0x80, 0xd0, 0x02, 0x01};

int main() {
	int i, n;
	char *buffer;
	unsigned char *man;
	man = (unsigned char *)malloc(64);

	printf("/////////////////////////////befor "
	       "free()/////////////////////////////////////\n");

	for(int i = 0; i < 24; i++)
		man[i] = mehdi[i];
	man[24] = '\0';

	for(i = 0; i < 24; i++) {
		printf("befor free man[%d]==0x%x\n", i, man[i]);
	}

	free((unsigned char *)man);

	printf("/////////////////////////////after "
	       "free()/////////////////////////////////////\n");

	for(i = 0; i < 24; i++) {
		printf("befor free man[%d]==0x%x\n", i, man[i]);
	}

	printf("\nonly just 0-8 data of pointer ereased!!\nwhat dont erease all "
	       "data from pointer??\nplease help me :-(\n");
	return 0;
}


your question, formatted
> only just 0-8 data of pointer ereased!!
> what dont erease all data from pointer??
> please help me :-(


In line 27 you have released the memory owned by the pointer.
In the loop on line 32 you try to access memory that you don't own anymore, invoking undefined behaviour.
Further analysis is pointless.


Also, I don't understand why you associate `free()' with `destroy()' `throw_acid_on_it()'
Last edited on
Why do you use a pointer at all?
Since you use a fixed size you would be better off with a static array.
So i should changed line 27 to "free (&man);" ?
Please write code for me..
I am not sure what the point of this exercise is, but try this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* malloc example:*/
#include <stdio.h>  /* printf, scanf, NULL */
#include <stdlib.h> /* malloc, free, rand */

//////////////////////////////////////////////
unsigned char mehdi[24] = 
{ 
  0x55, 0xAA, 0x02, 0x01, 0x03, 0x02, 0xc0, 0xd0,
  0x0e, 0x07, 0x60, 0x90, 0xf0, 0x40, 0x50, 0xa0,
  0xf0, 0xc0, 0xb0, 0x04, 0x80, 0xd0, 0x02, 0x01 
};

int main() 
{
  int i, n;
  char *buffer;
  unsigned char *man;
  man = (unsigned char *) malloc(64);
  if (man == NULL)
  {
    fprintf(stderr, "Out of memory");
    return -1;
  }

  printf("/////////////////////////////befor "
    "free()/////////////////////////////////////\n");

  for (int i = 0; i < 24; i++)
    man[i] = mehdi[i];
  man[24] = '\0';

  for (i = 0; i < 24; i++) {
    printf("befor free man[%d]==0x%x\n", i, man[i]);
  }

  free(man);

  printf("/////////////////////////////after "
    "free()/////////////////////////////////////\n");
  /* CAUTION - accessing memory after free is undefined behaviour */ 
  for (i = 0; i < 24; i++) {
    printf("after free man[%d]==0x%x\n", i, man[i]);
  }

  printf("\nall data of pointer ereased!!");
  return 0;
}
¿diff?


> So i should changed line 27 to "free (&man);" ?
¿how did you come to that conclusion?

Line 27 is fine, the cast is unneeded but it makes no difference.
Lines 32--34 are an aberration that must be eradicated from existence.
Thank you so much dear friends 🌺🌺🌺
I can't understand the use of a pointer in this piece of code, as the values doesn't escape the scope. Freeing space is the most important as if you won't do it a segmentation fault may arise and those are the worst kind so you should be able to detect those kind of vulnerabilities among the code. You can try using any software as a help, Checkmarx might help. heard about it from a friend. nor sure of their costs.
Anyway. good luck!
Ben.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* malloc example:*/
#include <stdio.h>  /* printf, scanf, NULL */
#include <stdlib.h> /* malloc, free, rand */

//////////////////////////////////////////////
unsigned char mehdi[24] = {0x55, 0xAA, 0x02, 0x01, 0x03, 0x02, 0xc0, 0xd0,
                           0x0e, 0x07, 0x60, 0x90, 0xf0, 0x40, 0x50, 0xa0,
                           0xf0, 0xc0, 0xb0, 0x04, 0x80, 0xd0, 0x02, 0x01};

int main() {
	int i, n;
	char *buffer;
	unsigned char *man;
	man = (unsigned char *)malloc(64);

	printf("/////////////////////////////befor "
	       "free()/////////////////////////////////////\n");

	for(int i = 0; i < 24; i++)
		man[i] = mehdi[i];
	man[24] = '\0';

	for(i = 0; i < 24; i++) {
		printf("befor free man[%d]==0x%x\n", i, man[i]);
	}

        for(i=0; i<24; i++)
		man[i] = 0x0;

	printf("/////////////////////////////after "
	       "free()/////////////////////////////////////\n");

	for(i = 0; i < 24; i++) {
		printf("after free man[%d]==0x%x\n", i, man[i]);
	}

	free(man);
	return 0;
}
Topic archived. No new replies allowed.