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

Pages: 1-4041-8081-120121-

C arrays

Name: Anonymous 2008-11-15 10:18

is there a way to do this all in one function instead of splitting it into two?
#include <stdlib.h>

void stuff_with_a(char a[20000000][20]){
 for(int i = 0; i < 20000000; ++i){
  for(int j = 0; j < 20; ++j)
   a[i][j] = 0;
 }
}

int main(){
 char *a = malloc(20000000*20);
 stuff_with_a(a);
 sleep(60);
 return 0;
}

Name: Anonymous 2008-11-15 10:26

char* a = (char*) calloc(20000000*20, sizeof(char));

Enjoy your OUT OF MEMORY faults.

Name: Anonymous 2008-11-15 10:43

1. Just use memset


#define N 2000000*20
char *a = malloc(N);
memset((void *)a, 0, N);


2. Anyway you are doing it wrong.

The formal parameter of the stuff_with_a function declares at stack level a huge matrix, while the correct way of doing this is using pointers.

Just try to compare assembly code obtained by the following versions of the same function:


void foo(char **x, size_t n, size_t m) {
    uint32_t i,j;
    for (i=0; i<n; i++)
        for (j=0; j<m; j++)
            x[i][j] = 0;
}



void foo(char x[1000][10]) {
    uint32_t i,j;
    for (i=0; i<1000; i++)
        for (j=0; j<10; j++)
            x[i][j] = 0;
}


You will notice that the first version is much more good!

Name: Anonymous 2008-11-15 11:10

char* a = (char*) calloc(20000000*20, sizeof(char));
1. Just use memset
way to entirely miss the point, faggots. the idea is to use malloc and then use the a[i][j] syntax to access the array, without wasting 20000000 pointers worth of memory.

>>3
a isn't a [/code]**[/code], it's just a *.

Name: Anonymous 2008-11-15 11:26

Sage.

Name: Anonymous 2008-11-15 12:33

Observe the EXPERT C PROGRAMMER in his natural habitat.

Name: Anonymous 2008-11-15 12:42

>>2
I won't, along with my memory overcommit.

Name: Anonymous 2008-11-15 12:50

>>4
Try to formulate your problem more clearly next time maybe?

Then again I'm pretty sure I'm getting trolled.

Name: Anonymous 2008-11-15 12:54

>>4
without wasting 20000000 pointers worth of memory

Wow, just wow.

Name: Anonymous 2008-11-15 12:55

I wasted 20000000 pointers worth of memory on your mother.

Name: Anonymous 2008-11-15 14:32

                       //`'''```,
             o        // LISP   `.,
       ,....OOo.   .c;.',,,.'``.,,.`
    .'      ____.,'.//
   / _____  \___/.'
  | / ||  \\---\|
  ||  ||   \\  ||
  co  co    co co

Name: Anonymous 2008-11-15 14:41

troll: you can't be this dumb

Name: Anonymous 2008-11-15 15:59

>>3
The formal parameter of the stuff_with_a function declares at stack level a huge matrix, while the correct way of doing this is using pointers.
I was under the impression that arrays were passed by-reference: if you declared the array in the body of the function, it would be stack-allocated. In this case, however, I think the compiler would implicitly convert the pointer to an array and not push it onto the stack.

Name: Anonymous 2008-11-15 16:05

>> 13
Here 3 speaking.

If you don't believe me, just try to produce the relative code with gcc -S, and than you'll shit bricks!

Name: Anonymous 2008-11-15 18:03

>>11
This won't surprise you, but I invented that ASCII art.

Name: Anonymous 2008-11-15 18:05

>>11
Shutup suave yoshi lisp

Name: Anonymous 2008-11-15 18:31


for(int y;y<20000000;++y):
  for(int x;x<20;++x):
    a[y*20+x]=0

Name: Anonymous 2008-11-16 0:54

>>14
Given the input file, test.c:
void func( int a[9001] ) {
    a[0] = 1;
}


gcc -S produces the following --
    .file    "test.c"
    .text
    .p2align 4,,15
.globl func
    .type    func, @function
func:
    pushl    %ebp
    movl    %esp, %ebp
    movl    8(%ebp), %eax
    movl    $1, (%eax)
    popl    %ebp
    ret
    .size    func, .-func
    .ident    "GCC: (GNU) 4.2.1 20070719  [FreeBSD]"

tl;dr it's passing by-reference, as I thought it did. No bricks have been shat.

Name: Anonymous 2008-11-16 3:01

>>18
Now with two-dimensional arrays please, smartass

Name: Anonymous 2008-11-16 3:09

>>18
You're right, putting an array size in the formal parameter does nothing--it's just to make the programmer feel warm and fuzzy. The compiler just ignores that size and treats it like a pointer, so there's no harm.

Name: Anonymous 2008-11-16 7:51

KIND OF GOOD:)

Name: Anonymous 2008-11-16 12:33

>>19
void func( int a[88][88] ) {
    a[1][1] = 4;
}


    .file    "test2.c"
    .text
    .p2align 4,,15
.globl func
    .type    func, @function
func:
    pushl    %ebp
    movl    %esp, %ebp
    movl    8(%ebp), %eax
    addl    $352, %eax
    movl    $4, 4(%eax)
    popl    %ebp
    ret
    .size    func, .-func
    .ident    "GCC: (GNU) 4.2.1 20070719  [FreeBSD]"

Name: Anonymous 2008-11-16 18:16


#include <stdlib.h>

int main()
{
        char a[20000000][20];
        memset(a, 0, 20000000*20);
        sleep(60);
        return 0;
}


Done. Also, K&R'd your code for ya.

Name: Anonymous 2008-11-16 18:25

>>23


#include <stdlib.h>
#include <string.h>

#define MAXI 20000000
#define MAXJ 20

int main()
{
    char a[MAXI][MAXJ];
    memset(a, 0, MAXI*MAXJ);
    return EXIT_SUCCESS;
}


OPTIMIZED AND STANDARD-COMPLIANT

Name: Anonymous 2008-11-16 18:41

>>23
forgot to use malloc.

Name: Anonymous 2008-11-16 18:47

>>24
You didn't #define EXIT_SUCCESS faggot.

>>25
No I didn't. Please note I used array notation for a so I wouldn't need to malloc.

Name: Anonymous 2008-11-16 18:50

>>19
given the code in >>1, gcc produces:
        .file   "test.c"
        .text
.globl stuff_with_a
        .type   stuff_with_a, @function
stuff_with_a:
        pushl   %ebp
        xorl    %ecx, %ecx
        movl    %esp, %ebp
        movl    8(%ebp), %edx
        jmp     .L2
.L3:
        movb    $0, -1(%eax,%edx)
        incl    %eax
        cmpl    $21, %eax
        jne     .L3
        incl    %ecx
        addl    $20, %edx
        cmpl    $20000000, %ecx
        je      .L6
.L2:
        movl    $1, %eax
        jmp     .L3
.L6:
        popl    %ebp
        ret
        .size   stuff_with_a, .-stuff_with_a
.globl main
        .type   main, @function
main:
        leal    4(%esp), %ecx
        andl    $-16, %esp
        pushl   -4(%ecx)
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %ecx
        subl    $16, %esp
        pushl   $400000000
        call    malloc
        pushl   %eax
        call    stuff_with_a
        popl    %eax
        popl    %edx
        pushl   $60
        call    sleep
        movl    -4(%ebp), %ecx
        xorl    %eax, %eax
        leave
        leal    -4(%ecx), %esp
        ret
        .size   main, .-main
        .ident  "GCC: (GNU) 4.2.1 20070719  [FreeBSD]"

Name: Anonymous 2008-11-16 19:44

>>27
--fomg-optimized!!

Name: Anonymous 2008-11-16 21:02

--f
WHAT IS THIS ABOMINATION?  HAVE YOU NEVER USED GCC IN YOUR LIFE?

Name: Anonymous 2008-11-17 2:44

>>27
Yay, another FreeBSD user <3

Name: Anonymous 2008-11-17 3:22

>>26
#include <stdlib.h>
Know your C.

Name: Anonymous 2008-11-17 16:07

>>26

Enjoy your stack memory!!! 


#include <stdlib.h>

inr main() {
  char *a = calloc(20000000, 20);
  return(0);
}


Am I the only one who uses calloc?

Name: Anonymous 2008-11-17 16:46

>>32
No, but you are the only one that parenthesises the return value.

Name: Anonymous 2008-11-17 16:50

>>33
Ugh, I hate that, too. return is a statement, not a function! I also hate it when programmers put extraneous parenthesis, especially around Boolean logic. "foo > bar && bar != baz" is much easier to read than "(foo > bar) && (bar != baz)".

Name: Anonymous 2008-11-17 17:07

>>33
>>34
Superfluous parentheses are pig disgusting. However, I have the habit to always parenthesize my use of sizeof even though it's not necessary. I like to think of it as a function returning a size_t. Don't think I've ever seen anyone else do that. An expression like sizeof(x)*4 instead of sizeof x * 4 lets me sleep at night.

Name: Anonymous 2008-11-17 17:35

>>35
Huh, I was under the impression that sizeof required parenthesis.


#include <stddef.h>

int main()
{
    size_t a = sizeof int;
    return 0;
}



test.c: In function ‘main’:
test.c:5: error: expected expression before ‘int’


Yep, I think sizeof needs those parens.

Name: Anonymous 2008-11-17 17:53

>>32
now try assigning a value to a[1][1]

Name: Anonymous 2008-11-17 18:11

>>36
C99 6.5.3.4.2
The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type.

Name: Anonymous 2008-11-17 19:05

>>34,38
10/10

Name: Anonymous 2008-11-17 19:28

Too bad >>34 >>38 are not the same poster. I know, because I'm >>34 .

Name: Anonymous 2008-11-18 2:03

>>34,38,40
Hahaha, you think you can fool me? I, the expert programmer, laugh at you're insolence. You're all the same people, and we have been trolled constantly.

Name: Anonymous 2008-11-18 2:18

>>39-42
Same person.

Name: Anonymous 2008-11-18 3:02

>>42
>>41 here. You are wrong, since this is my second post in this thread. I would know, you know how? I am >>41.

Name: Anonymous 2008-11-18 14:31

>>35

#include <stdio.h>

int main(void) {
    printf("%d\n", (int)sizeof(42)["where is your god now?"]);
    return 0;
}

Name: Anonymous 2008-11-18 20:11

>>44
Oldest trick in the world.

Name: Anonymous 2008-11-18 20:34

>>44
error 4:35 - Syntax error at '[', expected ');'

Name: Anonymous 2008-11-19 2:02

>>46
Use a C compiler.

Name: Anonymous 2008-11-20 2:56

sizeof 42["where is your god now?"]
1

Holy fucking shit sizeof really does work without parenthesis

Name: Anonymous 2008-11-20 6:55

>>48
because it's an operator

Name: Anonymous 2008-11-20 10:34

>>49
(sizeof (sizeof))

Name: Anonymous 2008-11-20 17:20

>>50
6

Name: Anonymous 2008-11-20 22:34

Name: Anonymous 2010-11-15 16:13

Name: Anonymous 2013-06-18 19:47

I ARE ANDRU

Name: Anonymous 2013-06-18 19:54

I ARE ANDRU

Name: Anonymous 2013-06-18 20:01

I ARE ANDRU

Name: Anonymous 2013-06-18 20:08

I ARE ANDRU

Name: Anonymous 2013-06-18 20:15

I ARE ANDRU

Name: Anonymous 2013-06-18 20:23

I ARE ANDRU

Name: Anonymous 2013-06-18 20:30

I ARE ANDRU

Name: Anonymous 2013-06-18 20:37

I ARE ANDRU

Name: Anonymous 2013-06-18 20:44

I ARE ANDRU

Name: Anonymous 2013-06-18 20:51

I ARE ANDRU

Name: Anonymous 2013-06-18 20:58

I ARE ANDRU

Name: Anonymous 2013-06-18 21:05

I ARE ANDRU

Name: Anonymous 2013-06-18 21:12

I ARE ANDRU

Name: Anonymous 2013-06-18 21:19

I ARE ANDRU

Name: Anonymous 2013-06-18 21:26

I ARE ANDRU

Name: Anonymous 2013-06-18 21:34

I ARE ANDRU

Name: Anonymous 2013-06-18 21:41

I ARE ANDRU

Name: Anonymous 2013-06-18 21:48

I ARE ANDRU

Name: Anonymous 2013-06-18 21:55

I ARE ANDRU

Name: Anonymous 2013-06-18 22:02

I ARE ANDRU

Name: Anonymous 2013-06-18 22:09

I ARE ANDRU

Name: Anonymous 2013-06-18 22:17

I ARE ANDRU

Name: Anonymous 2013-06-18 22:24

I ARE ANDRU

Name: Anonymous 2013-06-18 22:31

I ARE ANDRU

Name: Anonymous 2013-06-18 22:38

I ARE ANDRU

Name: Anonymous 2013-06-18 22:45

I ARE ANDRU

Name: Anonymous 2013-06-18 22:52

I ARE ANDRU

Name: Anonymous 2013-06-18 22:59

I ARE ANDRU

Name: Anonymous 2013-06-18 23:06

I ARE ANDRU

Name: Anonymous 2013-06-18 23:13

I ARE ANDRU

Name: Anonymous 2013-06-18 23:20

I ARE ANDRU

Name: Anonymous 2013-06-18 23:27

I ARE ANDRU

Name: Anonymous 2013-06-18 23:34

I ARE ANDRU

Name: Anonymous 2013-06-18 23:41

I ARE ANDRU

Name: Anonymous 2013-06-18 23:48

I ARE ANDRU

Name: Anonymous 2013-06-18 23:55

I ARE ANDRU

Name: Anonymous 2013-06-19 0:02

I ARE ANDRU

Name: Anonymous 2013-06-19 0:09

I ARE ANDRU

Name: Anonymous 2013-06-19 0:17

I ARE ANDRU

Name: Anonymous 2013-06-19 0:24

I ARE ANDRU

Name: Anonymous 2013-06-19 0:31

I ARE ANDRU

Name: Anonymous 2013-06-19 0:38

I ARE ANDRU

Name: Anonymous 2013-06-19 0:45

I ARE ANDRU

Name: Anonymous 2013-06-19 0:52

I ARE ANDRU

Name: Anonymous 2013-06-19 0:58

I ARE ANDRU

Name: Anonymous 2013-06-19 1:05

I ARE ANDRU

Name: Anonymous 2013-06-19 1:12

I ARE ANDRU

Name: Anonymous 2013-06-19 1:19

I ARE ANDRU

Name: Anonymous 2013-06-19 1:26

I ARE ANDRU

Name: Anonymous 2013-06-19 1:33

I ARE ANDRU

Name: Anonymous 2013-06-19 1:40

I ARE ANDRU

Name: Anonymous 2013-06-19 1:47

I ARE ANDRU

Name: Anonymous 2013-06-19 1:54

I ARE ANDRU

Name: Anonymous 2013-06-19 2:01

I ARE ANDRU

Name: Anonymous 2013-06-19 2:08

I ARE ANDRU

Name: Anonymous 2013-06-19 2:15

I ARE ANDRU

Name: Anonymous 2013-06-19 2:22

I ARE ANDRU

Name: Anonymous 2013-06-19 2:29

I ARE ANDRU

Name: Anonymous 2013-06-19 2:36

I ARE ANDRU

Name: Anonymous 2013-06-19 2:43

I ARE ANDRU

Name: Anonymous 2013-06-19 2:50

I ARE ANDRU

Name: Anonymous 2013-06-19 2:56

I ARE ANDRU

Name: Anonymous 2013-06-19 3:04

I ARE ANDRU

Name: Anonymous 2013-06-19 3:11

I ARE ANDRU

Name: Anonymous 2013-06-19 3:17

I ARE ANDRU

Name: Anonymous 2013-06-19 3:24

I ARE ANDRU

Name: Anonymous 2013-06-19 3:31

I ARE ANDRU

Name: Anonymous 2013-06-19 3:38

I ARE ANDRU

Name: Anonymous 2013-06-19 3:45

I ARE ANDRU

Name: Anonymous 2013-06-19 3:52

I ARE ANDRU

Name: Anonymous 2013-06-19 3:59

I ARE ANDRU

Name: Anonymous 2013-06-19 4:06

I ARE ANDRU

Name: Anonymous 2013-06-19 4:13

I ARE ANDRU

Name: Anonymous 2013-06-19 4:20

I ARE ANDRU

Name: Anonymous 2013-06-19 4:27

I ARE ANDRU

Name: Anonymous 2013-06-19 4:34

I ARE ANDRU

Name: Anonymous 2013-06-19 4:41

I ARE ANDRU

Name: Anonymous 2013-06-19 4:48

I ARE ANDRU

Name: Anonymous 2013-06-19 4:55

I ARE ANDRU

Name: Anonymous 2013-06-19 5:02

I ARE ANDRU

Name: Anonymous 2013-06-19 5:09

I ARE ANDRU

Name: Anonymous 2013-06-19 5:16

I ARE ANDRU

Name: Anonymous 2013-06-19 5:23

I ARE ANDRU

Name: Anonymous 2013-06-19 5:30

I ARE ANDRU

Name: Anonymous 2013-06-19 5:37

I ARE ANDRU

Name: Anonymous 2013-06-19 5:44

I ARE ANDRU

Name: Anonymous 2013-06-19 5:51

I ARE ANDRU

Name: Anonymous 2013-06-19 5:58

I ARE ANDRU

Name: Anonymous 2013-06-19 6:05

I ARE ANDRU

Name: Anonymous 2013-06-19 6:12

I ARE ANDRU

Name: Anonymous 2013-06-19 6:19

I ARE ANDRU

Name: Anonymous 2013-06-19 6:26

I ARE ANDRU

Name: Anonymous 2013-06-19 6:33

I ARE ANDRU

Name: Anonymous 2013-06-19 6:40

I ARE ANDRU

Name: Anonymous 2013-06-19 6:47

I ARE ANDRU

Name: Anonymous 2013-06-19 6:54

I ARE ANDRU

Name: Anonymous 2013-06-19 7:01

I ARE ANDRU

Name: Anonymous 2013-06-19 7:08

I ARE ANDRU

Name: Anonymous 2013-06-19 7:15

I ARE ANDRU

Name: Anonymous 2013-06-19 7:22

I ARE ANDRU

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