Issues with memcpy

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

 



I'm having some issues with memcpy, i've attached two example files, just copying a structure in a char array. The result is different in both files, but they do the same.

The output is this:

tigre@enigma collector $ gcc pro.c
tigre@enigma collector $ ./a.out
5 0 0 0 5 0 0 0 5

tigre@enigma collector $ gcc pro2.c
tigre@enigma collector $ ./a.out
5 fffffffa ffffffff ffffffbf 5 0 0 0 5
5 fffffffa ffffffff ffffffbf 5 0 0 0 5

Can someone explain it to me?

Thanks
#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[9];
	t_st mystruct;
	
	mystruct.ctype = 5;
	mystruct.itype = 5;
	mystruct.itype2 = 5;
	
	bzero(buffer, 9);
	
	memcpy(buffer, (char*)&mystruct, 9);
	
	for(a=0; a<9; 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[9];
	char *buffer2;
	t_st mystruct;
	
	mystruct.ctype = 5;
	mystruct.itype = 5;
	mystruct.itype2 = 5;
	
	bzero(buffer, 9);
	
	memcpy(buffer, (char*)&mystruct, 9);
	
	for(a=0; a<9; a++)
		printf("%x ", buffer[a]);
	printf("\n");

	buffer2 = (char*)malloc(sizeof(char)*9);
	bzero(buffer2, 9);
	
	memcpy(buffer2, (char*)&mystruct, 9);
	
	for(a=0; a<9; 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