Get offset


1 #include <stdio.h>
2
3 struct foo {
4     char a;
5     char b[10];
6     char c;
7 };
8
9 int main()
10 {
11
12
13     printf("offsetof is %d\n", &(((struct foo*)0)->a));
14     printf("offsetof is %d\n", &(((struct foo*)0)->b));
15     printf("offsetof is %d\n", &(((struct foo*)0)->c));
16 }

Result:

offsetof is 0
offsetof is 1
offsetof is 11

More details on:

http://en.wikipedia.org/wiki/Offsetof

Get offset

Pitfall about free function

1. Still can access the pointer after free the memory.


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

int main()
{
char *buf;
buf = (char *)malloc(sizeof(char));
*buf = 'a';
free(buf);

if (buf)
{
printf("buf is not NULL\n");
printf("buf %c\n", *buf);
}
else
printf("buf is NULL\n");

return 0;
}

<section>
Result:
buf is not NULL
buf a
2.The memory is reused which caused undesired results.

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

int main()
{
char *buf;
buf = (char *)malloc(sizeof(char));
*buf = 'a';
free(buf);

char *buf_b = (char *)malloc(sizeof(char));
*buf_b = 'b';
free(buf_b);

/*suppose lots of function called, lots of code runed at here*/
/*I really don't know if buf and buf_b have been freed or not*/
/*the following logic may also be called in other thread*/

if (buf)
{
printf("buf is not NULL\n");
printf("buf %c\n", *buf);
printf("Probably not the desired results at here...\n");
}
else
printf("buf is NULL\n");

return 0;
}

Result:

buf is not NULL
buf b
Probably not the desired results at here…

free

void free (void* ptr);
Deallocate memory block
A block of memory previously allocated by a call to malloc, calloc or realloc is deallocated, making it available again for further allocations.

If ptr does not point to a block of memory allocated with the above functions, it causes undefined behavior.

If ptr is a null pointer, the function does nothing.

Notice that this function does not change the value of ptr itself, hence it still points to the same (now invalid) location.

Solution: After free the memory, set the pointer to NULL.
Pitfall about free function

stack, heap related tests

1. How to know stack grows up or down?


#include <stdio.h>;

void bar()
{
int b = 50;
printf("b address %p\n", &b);
}

void foo()
{
int a = 0;
printf("a address %p\n", &a);
bar();
}

int main()
{
foo();
}

Result:

a address 0x7fffcca6f6ec
b address 0x7fffcca6f6cc

b’s address is smaller than a’s. So stack goes down. Usually, stack goes down.However, it is different question with local variables laid out inside its stack frame?

2. Local variables in a stack frame?


#include <stdio.h>;

void test()
{
int a = 0;
int b = 0;
printf("a address %p\n", &a);
printf("b address %p\n", &b);
}

int main()
{
int a = 0;
int b = 0;
printf("ma address %p\n", &a);
printf("mb address %p\n", &b);

test();
return 0;
}

Result(on Linux ip 3.13.0-44-generic #73-Ubuntu SMP Tue Dec 16 00:22:43 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux) :

ma address 0x7fffd5b8bdf8
mb address 0x7fffd5b8bdfc
a address 0x7fffd5b8bdd8
b address 0x7fffd5b8bddc

Result(on FreeBSD):

ma address 0xffffcaf0
mb address 0xffffcaec
a address 0xffffcac4
b address 0xffffcac0

2. How to get BSS, data, stack, heap address?


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

int g_a = 123;
int g_b;

int main()
{
char c;
char *p = (char *)malloc(1);
printf("data at %p\nbss at %p\n", &g_a, &g_b);
printf("heap at %p\nstack at %p\n", p, &c);

free(p);
}

Result:

data at 0x601048
bss at 0x601050
heap at 0x1eb3010
stack at 0x7fff4ee65ad7

(More related info at http://www.geeksforgeeks.org/memory-layout-of-c-program/)

3. How to get stack size?

ulimit -a

core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7862
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 7862
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

Per my understanding, stack size should be equals to (stack address – heap address) according to http://www.geeksforgeeks.org/memory-layout-of-c-program/. However, it’s obvious not in this case. Anyone knows why?

My machine:

Linux ip 3.13.0-44-generic #73-Ubuntu SMP Tue Dec 16 00:22:43 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

stack, heap related tests