Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

shitty C pointer arithmetic

Name: Anonymous 2010-05-29 14:38

Why are pointers in C aligned to the size of their data? Why cant they just reference memory bytewise?

How can i manipulate a pointer so that it references a address not by its own size, without typecasting it?

Name: Anonymous 2010-05-29 14:42

You can't, the behaviour would be undefined, and it's a stupid thing to do anyway. Don't do it. If you really wanted to, learn assembly. Then realize that it's a stupid idea. Then don't do it.

Name: Anonymous 2010-05-29 15:03

TYPE *a = /* something */;
char *b = a;


That was difficult.

Name: Anonymous 2010-05-29 15:04

>>2
fuck you! its my memory and i can reference and dereference what i want!

you can go and stick your segfaults up your anus!

Name: Anonymous 2010-05-29 15:06

>>3

>>1
Without typecasting

Name: Anonymous 2010-05-29 19:20

>>5
Yeah, that's a bullshit requirement. That's like asking how you output text without printing to the screen.

Name: Anonymous 2010-05-29 20:38

>>1
Enjoy your suboptimal programs as your CPU drags whole memory blocks, that are crossed by your nonconformatly lengthed variables, from memory.

IHBT

Name: Anonymous 2010-05-29 22:28

>>7
Enjoy your inferior CPU

Name: Anonymous 2010-05-29 23:02

I'll just enjoy my data abort. Alone.

Name: Anonymous 2010-05-30 2:43

>>4
I happen to enjoy seg faults.

Name: Anonymous 2010-05-30 4:12

>>6
So there is no way to do what i want other then casting it to an other type?
>>7
What?

Name: Anonymous 2010-05-30 4:25

>>1
You can reference memory bytewise, if you wish to do so, for example:


char *mem = (char*) 0x401000;
char *p;
char *mem_end = mem + 0x1000;

for ( p = mem; p < mem_end ; p++)
  printf("%02X", *p)


That first cast is implmentation dependent behaviour, but on common arch's like x86 (MSVC or GCC or ICC compilers), pointers are internally kept as integers, so casting an integer to a pointer will just set the pointer's memory address to it. You do however need at least one cast.

You can change the alignment of fields in structures and sometimes even of data on the stack by using compiler-specific pragamas, but you should only do this if you really need it. A legitimate example is for example reading entire structures from binary files and accessing the data directly. However, not letting your compiler align your data (for example align each field to sizeof(int) will lead to slower memory accesses).

Can you describe what exactly you want to achieve?

Name: Anonymous 2010-05-30 4:47

>>8
These days that's every CPU that doesn't outright fault.

Name: Anonymous 2010-05-30 4:58

>>12
I want to do something like this:


int *mem = malloc(10);
int shit = 0;
*(mem + 1) = shit; /* should be mem+1 and not mem+1*sizeof(int) so that i have shit in *(mem + 1) to *(mem + 5) */

Name: Anonymous 2010-05-30 5:13

You are an idiot.

Name: Anonymous 2010-05-30 5:15

>>14
how is one int supposed to take up space for 4 ints?

Name: Anonymous 2010-05-30 5:17

LOOKS TO ME LIKE YOU WANT A UNION

Name: Anonymous 2010-05-30 5:41

>>16
its not.
i dont know how much space an int takes up, i just assumed 4 bytes.
mem + 1 is supposed to be the first byte and mem + 5 the last.

and no, i dont want to use unions or structs, i just want to insert a byte offset directly, is it so much to ask for?

Name: Anonymous 2010-05-30 5:46

this is so retarded

Name: >>12 2010-05-30 6:45

>>14
If you want to address bytes, use bytes (char or unsigned char). If you want ints use ints.

Pointer arithmetic is fine as it is. Changing it to only operate on bytes would only make everyone have to add +n*sizeof(...) to their code, which would be stupid. If you like to do everything manually, just use ASM.

I'm really not seeing the problem here, unless you don't know of the char/unsigned char type?

Name: Anonymous 2010-05-30 7:50

>>20
Changing it to only operate on bytes would only make everyone have to add +n*sizeof(...) to their code, which would be stupid.

Thats what arrays are for, it should be like i say with pointers.

A pointer is a just pointer for gods sake. it just points to some location in memory, independend of the size of the object at that address. This behaviour doesnt make much sense.

Name: Anonymous 2010-05-30 9:06

What you see, e.g. Int* is a variable address with variable length.
It points not at the raw memory but at some variable chunk location(a layer of abstraction above direct memory reference).

Name: Anonymous 2010-05-30 9:17

>>21
Use void* or char* (if you want to do pointer arith with it) if you want "raw pointers", it would behave the same. Arrays and pointers are directly connected and quite similar constructs in C.

Name: Anonymous 2010-05-30 9:19

I really don't understand why is OP whining about this when he can just use casts if he really wishes to do this silly thing.

Name: Anonymous 2010-05-30 10:39

>>24
I whine because it looks butt ugly when i do it. My code is ugly enough as it is.

Name: Anonymous 2010-05-30 10:48

>>25
I believe you.

Name: Anonymous 2010-05-30 11:02

>>25
You're an idiot. Stop trying to use C.

Name: Anonymous 2010-05-30 11:36

>>25
Yes, It's ugly. Guess what? Its even fuglier in every other language you can use, except assembly.

Name: Anonymous 2010-05-30 12:55

>>28
>fuglier
Get the fuck out.

Name: Anonymous 2010-05-30 13:29

i dont know how much space an int takes up, i just assumed 4 bytes.
Get a C book or go learn an easier language for you.

Name: Anonymous 2010-05-31 0:19

cast to byte
add
cast back to type

then make a marco

what is the problem?

Name: Anonymous 2010-11-13 13:18

Name: Anonymous 2010-12-28 12:03

Don't change these.
Name: Email:
Entire Thread Thread List