1 Star 0 Fork 0

yutao1115/hide-data-in-ptr

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
data_in_ptr.c 843 Bytes
Copy Edit Raw Blame History
Arjun Sreedharan authored 2014-12-15 19:37 . first code push
/*
* Copyright (C) 2014 Arjun Sreedharan
* License: GPL version 2 or higher http://www.gnu.org/licenses/gpl.html
*/
#include <stdio.h>
#include <assert.h>
void put_data(int*, unsigned int);
unsigned int get_data(unsigned int);
void cleanse_pointer(int*);
void put_data(int *p, unsigned int data)
{
assert(data < 4);
*p |= data;
}
unsigned int get_data(unsigned int p)
{
return (p & 3);
}
void cleanse_pointer(int *p)
{
*p &= ~3;
}
int main(void)
{
assert(sizeof(int) == 4);
assert(sizeof(void*) == 4);
unsigned int x = 701;
unsigned int p = (unsigned int) &x;
printf("Original ptr: %u\n", p);
put_data(&p, 3);
printf("ptr with data: %u\n", p);
printf("data stored in ptr: %u\n", get_data(p));
cleanse_pointer(&p);
printf("Cleansed ptr: %u\n", p);
printf("Dereferencing cleansed ptr: %u\n", *(int*)p);
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/yutao1115/hide-data-in-ptr.git
git@gitee.com:yutao1115/hide-data-in-ptr.git
yutao1115
hide-data-in-ptr
hide-data-in-ptr
master

Search