Herp derp
1
Name:
Anonymous
2011-04-20 12:30
ITT: How you decide the amount of elements in an array in C++ with only a pointer to the array. I bet you can't do it.
2
Name:
Anonymous
2011-04-20 12:34
Uuhh....use a fucking sentinel.
3
Name:
Anonymous
2011-04-20 12:40
ITT: How you decide the amount of elements in an list in Lisp with only a reference to the list. I bet you can't do it.
4
Name:
Anonymous
2011-04-20 12:48
sizeof(*x)/sizeof(x[0])
Derp
5
Name:
Anonymous
2011-04-20 13:30
$ man 3 memset
6
Name:
Anonymous
2011-04-20 13:54
>>4
sizeof(*x)
Ditch the "*" first of all, if you're not talking about a pointer to a pointer (array). But even if you correct you'reself, I refuse to believe this works in a general case. Please see
http://en.wikipedia.org/wiki/Sizeof#sizeof_and_incomplete_types
See:
char x[123];
int derp = sizeof(x);
works since compiler can figure shit out, but
int herp(char* x)
{
return sizeof(x);
}
will not for obvious reasons.
7
Name:
Anonymous
2011-04-20 13:58
typedef struct {
void *data;
size_t len;
} ArrayList;
8
Name:
Anonymous
2011-04-20 14:12
>>1
No shit you stupid fucking nigger. Maybe that's because C was designed to be close the hardware and wasn't designed to abstract memory so that the length of an array is always known.
Who the fuck would go passing around pointers to arrays without also storing the length of the array, though?
You?
9
Name:
Anonymous
2011-04-20 14:36
>>1
"
Hail to Odin! ", he cried, as he tore the family asunder with his ferocious axe. He left alive only the adolescent daughter, whom he promptly mounted, ravaging her as if she were but a ragdoll on the end of his throbbing penis.
10
Name:
Anonymous
2011-04-20 14:49
>>7
u mena
struct hax {
size_t len;
void *data[];
};
struct hax *hax_myanus(size_t len)
{
struct hax *haxed = malloc(sizeof(struct hax) + len * sizeof(void *));
if (haxed)
haxed->len = len;
return haxed;
}
11
Name:
Anonymous
2011-04-20 15:08
struct hax {
size_t len;
void* data;
};
struct hax* hax_myanus(size_t len)
{
hax* myanus = malloc(sizeof(struct hax));
myanus->data = rand() % pow(2, sizeof(void*));
return myanus;
}
12
Name:
Anonymous
2011-04-20 16:27
13
Name:
Anonymous
2011-04-20 19:16
>>12
Fuck you fag, my random memory allocation model is the most optimized thing your eyes will ever see.
14
Name:
Anonymous
2011-04-20 21:12
MEMORY LEAK
15
Name:
Anonymous
2011-04-21 3:18
>>11,13
Well played.
I think you meant rand() % pow(2, CHAR_BIT * sizeof(void *)?
>>12
>>10 is valid C99.
Fuck you, ``faggot''.
16
Name:
Anonymous
2011-04-21 10:05
>>15
Yeah that's what I meant.
17
Name:
Anonymous
2011-04-21 11:37
#define SIZE 100
void herp(int* derp) {
int size = derp[-1];
printf("size is %d\n", size);
}
int* myanus = malloc(SIZE * sizeof(int));
myanus[0] = SIZE;
int* hax = &myanus[1];
herp(hax);
18
Name:
Anonymous
2011-04-21 11:41
>>15
When you are expelled from an establishment for
rolling in your own fæces , adherence to dress code is not an adequate defence.
19
Name:
Anonymous
2011-04-21 12:09
How do you think strlen works?
Here it is, in FV -style indentation:
[m] int my_strlen(char *s){char *p; for(p=s; *p; p++); return p-s;}/m]
Doing it for any other types would be as trivial.
If you're using a linked list, something like:
for(size=0;*p;p = p->next,size++); would do the job of calculating the total size/length.
20
Name:
Anonymous
2011-04-21 12:33
>>19
BBfail
go back to /g
21
Name:
Grandma
2011-04-21 13:47
int main()
{
int (*p)[200];
int arr[200];
int size;
p = &arr;
size = sizeof *p / sizeof (*p)[0];
return 0;
}
Silly geese.
22
Name:
Anonymous
2011-04-21 13:58
Expert programmers inherently know how long an array is going to be at any time without needing to check.
23
Name:
Anonymous
2011-04-21 14:31
>>22
This is true, and this is how an infinite set is derived. Try it. Consider it a dare.
24
Name:
Anonymous
2011-04-21 16:51
Give me a link to pic: "Have you read your SICP today?" -with-the-crazy-trouser-snake-guy !
25
Name:
Anonymous
2011-04-21 16:52
IN THIS THREAD: IDIOTS
[CONFIDENTIAL INFORMATION REMOVED]
$ cat ohnoes.c
#include <stdio.h>
int stfu(char faggot[])
{
return sizeof(faggot);
}
int main()
{
char nigger[123];
printf("this shit works: %d\n", sizeof(nigger));
printf("this shit does not work: %d\n", stfu(nigger));
return 0;
}
[CONFIDENTIAL INFORMATION REMOVED]
$ gcc ohnoes.c -o jesusfuckingchrist
[CONFIDENTIAL INFORMATION REMOVED]
$ ./jesusfuckingchrist.exe
this shit works: 123
this shit does not work: 4
26
Name:
Anonymous
2011-04-21 16:57
>>1
std::vector<int> arr;
int j = rand() % 1000;
arr.reserve(j);
for(int i = 0; i < j; ++i)
arr.push_back(i);
std::cout << arr.size();
Wow, would you look at that.
27
Name:
Anonymous
2011-04-21 18:29
YHABT.
This ``challenge'' is a thinly-veiled help thread. OP needed to know how to do it, so he added the ``I bet you can't do it'' to make it seem like more of a challenge than a mere help thread for a rather simple question.
I'm disappointed in you, /prog/. I thought you were better than helping Intro to Comp-Sci students.
28
Name:
Anonymous
2011-04-21 22:16
>>27
Is this a meta-troll or do you seriously believe that you are informing us with this banality?
29
Name:
Anonymous
2011-04-22 5:52
Newer Posts