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

HALP

Name: Anonymous 2007-02-10 18:58

char temp4[3];
unsigned long int FileStart;
FileStart = (temp4[3] << 24) | (temp4[2] << 16) | (temp4[1] << 8) | (temp4[0]);

This was supposed to take four bytes and turn them into a long int. How do I do this so that the << doesn't turn every byte other than the first one into 'FF'?

Name: Anonymous 2007-02-10 19:54

char temp4[3];
That array is only three characters long.

This was supposed to take four bytes and turn them into a long int.

Have you considered just using a typecast?

FileStart = *(unsigned long *) temp4;

Of course, there's still endian issues to take into consideration.

Name: Anonymous 2007-02-10 19:57

>>2
[3] has four elements.

Name: Anonymous 2007-02-10 20:03

>That array is only three characters long.
Grrr, I thought array sizes were zero-based...

>Have you considered just using a typecast?

>FileStart = *(unsigned long *) temp4;
>Of course, there's still endian issues to take into consideration.
I had seen the code elsewhere, and I was hoping to avoid the endian issues by using it. It's supposed to work, but I don't know what I got wrong, and I lost the source of the code.

Name: Anonymous 2007-02-10 20:04

Err, if there's any confusion, it's C++.

Name: Anonymous 2007-02-10 20:08

>>5 what part of it is C++ you idiot?

Name: Anonymous 2007-02-10 20:18

>>3
char a[3] has 3 elements. You access them by [0] - [2]

Name: Anonymous 2007-02-11 4:24

>>7 you're just wrong.
a[3] has 4 elements.
a[0] a[1] a[2] a[3].
Try this code :

#include <stdio.h>
#include <string.h>

main() {
   
    char a[3];
    int i = 0;
    memset(a, 'A', 4);
   
    putchar((int)a[0]);
    putchar((int)a[1]);
    putchar((int)a[2]);
    putchar((int)a[3]);
}

Name: Anonymous 2007-02-11 5:03

>>8 is right, and you can access the elements background like in Python:
a[-1] is the last element, a[-2]•••

Name: Anonymous 2007-02-11 5:42

>>8

No. a[x] specifies x number of elements.
Access is from 0 to x-1. a[x] lvalues are defined as exactly the same as *(a + x * sizeof(a[0])), and therefore a[-1] accesses the memory location behind a[0].

This is undefined, because C is not a sissy language with bounds checking. a[3] appears to work but writing or reading from it is permitted to make your computer explode.

Name: 10 2007-02-11 5:45

(actually, *(a + x), because in pointer + x, x is automatically multiplied by the pointer's base type size)

Name: Anonymous 2007-02-11 5:46

>>10
a[3] = "hey";
works fine because it's "hey\0" which is 4 characters, which fit exactly in the four elements array.

Name: Anonymous 2007-02-11 5:51

>>8
Fuckin' wrong. See >>10 to understand why you are so wrong.
>>9
see above.

Name: Anonymous 2007-02-11 5:53

>>12
no.
a[3]="OH SHI-";
works too.

Name: Anonymous 2007-02-11 6:01

>>12
the code:
char a[3] = "hey";

produced the following error under a C++ compiler:
SandBox.cpp(5) : error C2117: 'hey' : array bounds overflow

In C, however, that code will compile, since the compiler performs no bounds checking. Using a memory viewer viewer however, you can clearly see how this is hazardous:
0012FF7C  68 65 79 CC C0 FF 12 00 C9 83 40  heyÌÀÿ..Ƀ@
Notice the abscence of the null byte to terminate the string. Very dangerous indeed.

Name: Anonymous 2007-02-11 6:03

>>12

As a statement, that is illegal. a is an array of char and not an array of char*.

As a declaration, 'char a[x] = "hey"' is the same as 'char *a = "hey"'. No copy is involved, the variable is just a pointer to the string which is in the const data segment.

sizeof() is different, though. For the second, sizeof(a) is sizeof(char*) which is usually 4.

For the first, sizeof(a) is sizeof(char[3]), which is 3, because there are three elements.

Name: Anonymous 2007-02-11 6:08

>>16
Partially wrong.
char a[x] is NOT the same as char* a = "hey";
The first creates the the string of bytes on the stack, including terminating null. The second merely assigns a pointer variable (4 bytes or however wide it is on your system) to a string "hey\0" which will usually be created in the .data section.

Name: Anonymous 2007-02-11 6:08

>>17
should read char a[x] = "hey";

Name: Anonymous 2007-02-11 6:24

The first creates the the string of bytes on the stack, including terminating null.

I checked with GCC and it doesn't generate any obvious memory copies. printf'ing an array declared as being too short does stop it from including the terminating NULL (which that post above didn't imply) and therefore messes up the output, but I only have access to a Linux shell and gcc 3, so no -fdump-tree-* and no stack format I understand to tell me how this works.

Regardless, it's a bad habit to think that the compiler generates copies if your code doesn't try to write to anything. You should know how optimization works.

Name: Anonymous 2007-02-11 6:36

>>17
and don't forget that the char* version is read-only if declared like:
char *s = "stuff";

Name: Anonymous 2007-02-11 21:27

USE UNSIGNED

or you'll get FFFFFFFFFFFs all over the place

Name: Anonymous 2007-02-12 2:03

>>1 - >>21
THIS IS WHY I HATE C/C++
FUCK YOURSELVES

Name: Anonymous 2007-02-12 16:05

>>19
Using msvc6, with no optimizations, a rep movs instruction is generated to copy the bytes to the stack address. Haven't bothered to check with optimization, though it's highly likely the copy will be optimized away if all you're doing is printf'ing it.

Name: Anonymous 2007-02-12 18:16

>>22

lrn2program, kthxbye

Name: Anonymous 2007-02-12 18:35

>>24
durrr saywhat

Name: Anonymous 2007-02-12 19:21

>>25
One word, the forced indentation of code. Thread over.

Name: Anonymous 2007-02-13 3:48

This board has been going down the hill more than usual lately. It's not even funny anymore, even copypasta is done terribly.

Name: Anonymous 2007-02-13 6:25

>>27

Whine less.  Anyone who comes to 4ch for real programming help deserves what they get.

Name: Anonymous 2007-02-13 7:46

>>27

Actually it's fine. Your perspective has changed. Perhaps you are depressed?

Name: Anonymous 2007-02-13 12:34

>>29
One word, the forced depression of /prog/. Thread over.

Name: Anonymous 2007-02-14 6:17

>>28
I don't come for real programming, I come for "lulz", but this has been going downhill even in the laughs department.

>>29
Not rly, it's just that I don't laugh with this one as much as I do with other trollchan boards, even though /prog used to be one of the best.

Name: EXPERT PROGRAMMER 2007-05-11 16:59 ID:Heaven

one word:
    FORCED INDENTATION OF CODE
        thread over

Name: Anonymous 2009-01-14 12:54

LISP

Name: Anonymous 2009-08-16 23:03

Lain.

Name: Anonymous 2010-11-28 3:30

Name: Anonymous 2011-02-04 11:26

Name: Anonymous 2011-02-04 18:29

Name: Anonymous 2012-03-23 23:38

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

Name: Sgt.Kabu칻kiman⼟꼬 2012-05-28 22:29

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

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