Blame global variables

/****************************************************************************
@File Name: foo.cpp
@Author: 
@mail: 
@Created Time: Fri 24 Mar 2017 07:12:01 PM CST
****************************************************************************/
#include <stdio.h>
//#include "t.h"
struct
{
	char a;
	int b;
}b = {2, 4};

void foo()
{
//	printf("&a = %x\n", &a);
	b.a = 4;
	b.b = 4;
	printf("&b = %x\n", &b);
	printf("sizeof b = %d\n", sizeof(b));
	printf("b.a = %d\n", b.a);
	printf("b.b = %d\n", b.b);
}

/****************************************************************************
@File Name: foo.c
@Author: 
@mail: 
@Created Time: Fri 24 Mar 2017 07:12:01 PM CST
****************************************************************************/
#include <stdio.h>
int b;
int c;
void t()
{
	printf("&b = %x\n", &b);
	printf("b = %d\n", b);
	printf("&c = %x\n", &c);
	printf("c = %d\n", c);
}

/****************************************************************************
@File Name: main.cpp
@Author: 
@mail: 
@Created Time: Fri 24 Mar 2017 07:15:40 PM CST
****************************************************************************/
//#include "t.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int b = 1;
int c = 1;
int main()
{
/*	foo();
	printf("foo out...!\n");
	printf("&a = %x\n", &a);
	printf("a = %x\n", a);
	printf("&b = %x\n", &b);
	printf("b = %x\n", b);
	printf("sizeof b = %d\n", sizeof(b));
	printf("&c = %x\n", &c);
	printf("c = %x\n", c);
	b = 10;
	foo();
*/
/*	pid_t pid = fork();
	if(pid < 0)
	{
		printf("fork failed...!\n");
		exit(1);
	}
	if(!pid)
	{
		printf("I am child...!\n");
		sleep(1);
		b = 1;
		printf("&b = %x\n", &b);
		printf("b = %d\n", b);
		printf("&c = %x\n", &c);
		printf("c = %d\n", c);
		printf("call foo...!\n");
		foo();
	}
	else
	{
		printf("I am parent...!\n");
		printf("call foo...!\n");
		foo();
		printf("call foo end...!\n");
		printf("&b = %x\n", &b);
		printf("b = %d\n", b);
		printf("&c = %x\n", &c);
		printf("c = %d\n", c);
		wait(-1);					// wait for any child process
		printf("&b = %x\n", &b);
		printf("b = %d\n", b);
		printf("&c = %x\n", &c);
		printf("c = %d\n", c);
	}
	*/
	int cnt = 5;
	while(cnt-- > 0)
	{
		printf("call t()...!\n");
		t();
		printf("call foo()...!\n");
		foo();
		printf("call end...!\n");
		printf("&b = %x\n", &b);
		printf("b = %d\n", b);
		printf("&c = %x\n", &c);
		printf("c = %d\n", c);
		sleep(1);
		printf("----------------------------------------\n");
	}
	return 0;
}

export LD_LIBRARY_PATH:=.
all:test
	./test
test:main.o t.o
	gcc -shared -fPIC -o libfoo.so foo.c
	gcc -o test main.o t.o -L. -lfoo
main.o:main.c
t.o:t.c
.PHONY:clean
clean:
	rm -rf *.o *.so test

Guess you like

Origin blog.csdn.net/wangzhicheng2013/article/details/66478019