Pointer addresses in multiples of 32 ?


 
Thread Tools Search this Thread
Top Forums Programming Pointer addresses in multiples of 32 ?
# 1  
Old 06-01-2010
Pointer addresses in multiples of 32 ?

1. Why are the pointers' addresses every 32 ?
2. Am I correct in stating that memset is writing to memory that is not allocated to any of the 3 pointers ? Is it writing to memory in between the pointers ?
3. Are the 3 pointers contiguous in memory ?
4. I only allocated 10 bytes for each pointer. So how can "a" be 73 chars long etc. ?

Code:
size_t bufsize = 10;

char *a = malloc(bufsize);
memset(a, 'a', bufsize - 1);
a[bufsize] = '\0';

char *b = malloc(bufsize);
memset(b, 'b', bufsize - 1);
b[bufsize] = '\0';

char *c = malloc(bufsize);
memset(c, 'c', bufsize - 1);
c[bufsize] = '\0';

size_t d = c - a;

memset(a, '-', d);
printf("a = %s => address: %i => number of chars: %i\n", a, a, strlen(a));
printf("b = %s => address: %i => number of chars: %i\n", b, b, strlen(b));
printf("c = %s => address: %i => number of chars: %i\n", c, c, strlen(c));
printf("d = %i\n", d);

Quote:
a = ----------------------------------------------------------------ccccccccc => address: 6299616 => number of chars: 73
b = --------------------------------ccccccccc => address: 6299648 => number of chars: 41
c = ccccccccc => address: 6299680 => number of chars: 9
d = 64
# 2  
Old 06-01-2010
Quote:
Originally Posted by cyler
Code:
size_t d = c - a;

memset(a, '-', d);

you can't mess about with memory like this and get away with it.
it may work once or twice, but it will cause a seg fault one day.

you cannot make assumptions about how malloc works
or where memory is allocated.
:-)
# 3  
Old 06-01-2010
Quote:
Originally Posted by cyler
1. Why are the pointers' addresses every 32 ?
That depends on the bitness of your machine...if it is 32-bit then pointers will store 4 bytes and if it is 64-bit then pointers will store 8 bytes.
Quote:
Originally Posted by cyler
2. Am I correct in stating that memset is writing to memory that is not allocated to any of the 3 pointers ? Is it writing to memory in between the pointers ?
Depends on what memory locations a b and c are pointing to...just dont assume anything like that.
Quote:
Originally Posted by cyler
3. Are the 3 pointers contiguous in memory ?
Again dont assume anything...be satisfied with what the os gives you.
Quote:
Originally Posted by cyler
4. I only allocated 10 bytes for each pointer. So how can "a" be 73 chars long etc. ?
See the line in red below for the answer.
Code:
size_t bufsize = 10;

char *a = malloc(bufsize);
memset(a, 'a', bufsize - 1);
a[bufsize] = '\0';

char *b = malloc(bufsize);
memset(b, 'b', bufsize - 1);
b[bufsize] = '\0';

char *c = malloc(bufsize);
memset(c, 'c', bufsize - 1);
c[bufsize] = '\0';

size_t d = c - a;

memset(a, '-', d);
printf("a = %s => address: %i => number of chars: %i\n", a, a, strlen(a));
printf("b = %s => address: %i => number of chars: %i\n", b, b, strlen(b));
printf("c = %s => address: %i => number of chars: %i\n", c, c, strlen(c));
printf("d = %i\n", d);

# 4  
Old 06-01-2010
Quote:
Originally Posted by shamrock
Depends on what memory locations a b and c are pointing to...just dont assume anything like that.
I am trying to follow this article: http://www.w00w00.org/files/articles/heaptut.txt

This article's example successfully uses memset to overwrite a pointer's address while making it overflow to overwrite another address. (Just wondering if both pointers must be contiguous in memory for this to happen.)

So basically:
1. Depends on 32 vs 64 bit machines.
2. No, it writes to unused memory in the memory allocated for each pointer.
3. ???
4. Has to do with 1 & 2.
# 5  
Old 06-02-2010
Quote:
Originally Posted by cyler
1. Why are the pointers' addresses every 32 ?
2. Am I correct in stating that memset is writing to memory that is not allocated to any of the 3 pointers ? Is it writing to memory in between the pointers ?
3. Are the 3 pointers contiguous in memory ?
4. I only allocated 10 bytes for each pointer. So how can "a" be 73 chars long etc. ?

Code:
size_t bufsize = 10;

char *a = malloc(bufsize);
memset(a, 'a', bufsize - 1);
a[bufsize] = '\0';

char *b = malloc(bufsize);
memset(b, 'b', bufsize - 1);
b[bufsize] = '\0';

char *c = malloc(bufsize);
memset(c, 'c', bufsize - 1);
c[bufsize] = '\0';

size_t d = c - a;

memset(a, '-', d);
printf("a = %s => address: %i => number of chars: %i\n", a, a, strlen(a));
printf("b = %s => address: %i => number of chars: %i\n", b, b, strlen(b));
printf("c = %s => address: %i => number of chars: %i\n", c, c, strlen(c));
printf("d = %i\n", d);

The following are also wrong:

Code:
char *a = malloc(bufsize);
memset(a, 'a', bufsize - 1);
a[bufsize] = '\0';  <--- memory corruption here!

The code "a[10]" refers to the 11th element of the array since the index starts at zero.
# 6  
Old 06-02-2010
On my BSD machine I get all sorts of differences...

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char ** argv){
        
    if(argc == 1) return 0;
    size_t sz = atoi(argv[1]);

    char *ab = malloc(sz);
    char *ap = malloc(sz);

    printf("size =  %d\n", (int)sz);
    printf("ab =  %lu\n", (long) ab );
    printf("ap =  %lu\n", (long) ap );
    printf("diff =  %td\n", ap - ab);
    return 0;

}

Code:
$ ./memdiff 1  
size =  1
ab =  34369184004
ap =  34369184006
diff =  2
$ ./memdiff 2
size =  2
ab =  34369184004
ap =  34369184006
diff =  2
$ ./memdiff 10
size =  10
ab =  34369183808
ap =  34369183824
diff =  16
$ ./memdiff 32
size =  32
ab =  34369183808
ap =  34369183840
diff =  32

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Separate file into multiples Linux

Hi guys I am newbie in using linux, how can i Separate file into multiples linux. I want the lines with same parent and id numbers print out into same files. For example Gm17 5135289 5136789 . Parent=533;ID=534;Name=Glyma17g07060 - Gm17 5135289 5136789 . ... (2 Replies)
Discussion started by: grace_shen
2 Replies

2. Shell Programming and Scripting

awk on multiples files

Ques from newbie I want to total column X from large number of files, and view totals for each file separately with the filename. I have tried: for i in `ls -1 *.pattern`; do cat "$i" | awk '{SUM += $4} END { printf("%8d\t%8d\n", $i,SUM) }'; done does not work. appreciate your help (5 Replies)
Discussion started by: analyst
5 Replies

3. UNIX for Dummies Questions & Answers

[Solved] lost multiples modules

hi guys I got linux Centos 5.5 on grub I got the latest kernel and updates which I really don't know what I did and I lost a lot of modules and my system is not working properly old kernel modules - this kernel is working fine # lsmod Module Size Used by autofs4 ... (0 Replies)
Discussion started by: karlochacon
0 Replies

4. Programming

multiples of 10 in java

Hi Guys, I wonder how can I determine when a given number is a multiple of another one in java. Let's say if I have 27 how can I determine whether is multiple of 5 using java programming. Thanks. (1 Reply)
Discussion started by: arizah
1 Replies

5. Shell Programming and Scripting

join multiples seds

Hi I have this string with 3 seds.. cat /tmp/roletmp|sed "s/$role2del//" | sed "s/,,/,/" |sed "s/^,//" |sed 's/,$//' How can I join these 3 seds in one? regards Israel. (3 Replies)
Discussion started by: iga3725
3 Replies

6. Shell Programming and Scripting

multiples menu in ksh

Hi, IS possible in ksh to make multiples menus? For example: My menu. 1)Option1 1.1)Option1.1 2.3)Option1.2 2)Option2 2.1)Option2.1 . . . x)Exit I've tried with case but no success. Thanks in advance. (3 Replies)
Discussion started by: iga3725
3 Replies

7. UNIX for Dummies Questions & Answers

Using cp for copying multiples files

Hi all, I've got this question about using cp for copying multiples files from the same source directory to another directory, considering that my working directory ain't the same of the source directory. Let me give you a simple example what I'm talking about: Suppose the following files... (2 Replies)
Discussion started by: chapeupreto
2 Replies

8. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

9. UNIX for Dummies Questions & Answers

Email multiples attachment files

I have a program the will split a large file into smaller files. It works great. I, however, have problem of email these file out to other people: I may have 1 or more files afer SPLIT datafileaa datafileab datafileac manually: mailx -s "data" email@email.com<datafileaa mailx -s... (5 Replies)
Discussion started by: bobo
5 Replies

10. Shell Programming and Scripting

How to sort multiples clolumns of a file?

Hi all! here is the file i am trying to sort : GREIMBAJ00;BAN_CAV;Loader.sh;2003/06/13;17:04:04 GREIMBAJ00;PER_COT;Loader.sh;2003/06/13;17:04:16 GREIMBAJ00;PER_COT;Traitement.sh;2003/06/13;17:04:18 GREIMBAJ00;BAN_PAK;Loader.sh;2003/06/13;17:04:11... (3 Replies)
Discussion started by: HowardIsHigh
3 Replies
Login or Register to Ask a Question