Re: Issues with memcpy

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Finally i've understood the point, but, why in the first example the output is "clean" and in the second example is printing the "empty" stuff if the code is the same for the static buffer?

Thanks

I've attached the modified examples

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{
	char ctype;
	int itype;
	int itype2;
} t_st;

int main()
{
	int a;
	char buffer[sizeof(t_st)];
	t_st mystruct;
	
	mystruct.ctype = 5;
	mystruct.itype = 5;
	mystruct.itype2 = 5;
	
	bzero(buffer, sizeof(t_st));
	
	memcpy(buffer, (char*)&mystruct, sizeof(t_st));
	
	for(a=0; a<sizeof(t_st); a++)
		printf("%x ", buffer[a]);
	printf("\n");

	
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{
	char ctype;
	int itype;
	int itype2;
} t_st;

int main()
{
	int a;
	char buffer[sizeof(t_st)];
	char *buffer2;
	t_st mystruct;
	
	mystruct.ctype = 5;
	mystruct.itype = 5;
	mystruct.itype2 = 5;
	
	bzero(buffer, sizeof(t_st));
	
	memcpy(buffer, (char*)&mystruct, sizeof(t_st));
	
	for(a=0; a<sizeof(t_st); a++)
		printf("%x ", buffer[a]);
	printf("\n");

	buffer2 = (char*)malloc(sizeof(char)*sizeof(t_st));
	bzero(buffer2, sizeof(t_st));
	
	memcpy(buffer2, (char*)&mystruct, sizeof(t_st));
	
	for(a=0; a<sizeof(t_st); a++)
		printf("%x ", buffer2[a]);
	printf("\n");

	return 0;
}


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux