Berkeley CSUA MOTD:Entry 35200
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/05/24 [General] UID:1000 Activity:popular
5/24    

2004/12/7-8 [Computer/SW/Languages/C_Cplusplus, Computer/SW/Languages] UID:35200 Activity:low
12/7    Optimization time: gprof shows 310k calls to a certain constructor
        (a very simple, very important object that is often stack-allocated
        into large arrays- eg "MyObject msgs[1000];").  Rather than calling
        the constructor 1000 times, is there a way to have a special array ctor
        that's called (once) and zeroes out the array en masse?  TIA.
        \_ Geezus, why are you constructing 310k objects? Make a static array
           of objects at the beginning of runtime.
           \_ I am, when possible.  It's a realtime system (= no dynamic memory
              allocation) so e.g. several queue objects have to buffer
              about 15k messages statically.  The rest are open to optimization,
              but a specialized array constructor (if it exists) would be nice.
              (nb: by "nice" I mean "not at all critical")
              Oh, and if it helps for those in a similar situation: MyObject
              contains a couple small arrays; it's much faster to zero these out
              manually with a for loop than with memset(). -op
              \_ Man, I hate people like you. Why didn't you give the complete
                 environment information in the first place? What type of
                 RTS system are you using and in what form factor? How much
                 total RAM do you have and what form is it in? And why the hell
                 are you using an OO language for small RTS apps?
                 \_ Sorry, I was just curious if C++ had a specialized ctor for
                    objects created in an array; I didn't realize I was creating
                    a tone of urgency.  Anyway, system is not resource-
                    constrained at all (P3 in a VXI chassis, 512M ram, etc) &
                    I'm coming to the conclusion that I can't just define
                    MyObject::MyObject[] ().
                    \_ Yes you can, you can overload new[].
                       \_ Ok, this might be helpful; can you elaborate?  What
                          would the constructor declaration look like?
       \_ If you want the array zeroed out without any object construction
          occuring, there are a few things you can do:
          (1) Create a default constructor which doesn't do anything
          (2) Overload new[] as suggested above to accomplish this
          (3) Allocate the space for the array using malloc or calloc instead
              of new and then use placement new to do the construction.
              Specifically, placement new lets you construct an object into
              a memory location you have allocated yourself.  The benefit of
              this is that your program would only need to spend time
              constructing an object when you want to put it in the array
              instead of when you allocate the whole array.  -emin
2025/05/24 [General] UID:1000 Activity:popular
5/24    

You may also be interested in these entries...
2014/1/14-2/5 [Computer/SW/Languages/C_Cplusplus] UID:54763 Activity:nil
1/14    Why is NULL defined to be "0" in C++ instead of "((void *) 0)" like in
        C?  I have some overloaded functtions where one takes an integer
        parameter and the other a pointer parameter.  When I call it with
        "NULL", the compiler matches it with the integer version instead of
        the pointer version which is a problem.  Other funny effect is that
        sizeof(NULL) is different from sizeof(myPtr).  Thanks.
	...
2011/3/7-4/20 [Computer/SW/Languages/C_Cplusplus] UID:54056 Activity:nil
3/7     I have a C question.  I have the following source code in two identical
        files t.c and t.cpp:
                #include <stdlib.h>
                int main(int argc, char *argv[]) {
                  const char * const * p1;
                  const char * * p2;
	...
2009/7/21-24 [Computer/SW/Languages/Java] UID:53168 Activity:moderate
7/20    For those who care btw, it looks like eclipse is now A Standard Tool
        at UCB ugrad cs, probably replaced emacs.  Furthermore, people get
        angry at seeing Makefiles, (since eclispe takes care of that).  I
        guess it's just a sign of the times.
        \_ The more people at my work use eclipse the less the code is
           managable in emacs.  I'm not sure which application's fault
	...
2005/2/15-16 [Computer/SW/Languages/C_Cplusplus, Computer/SW/Compilers] UID:36174 Activity:moderate
2/15    Technical question:  we have a memory leak in our C code, we think,
        but it's not the sort of memory leak where the memory's unreferenced.
        What we'd like to do is sort of a poor-man's profile, we want
        to know who calls our memory allocator "New"...  Sorta like a stack
        trace.  Using an actual profiler is sort of difficult 'cause it's
        a parallel application.  Thanks,  --peterm
	...
2004/9/23 [Computer/SW/Languages/C_Cplusplus] UID:33716 Activity:high
9/23    Is the a C++ equivelent to the C realloc function?
        \_ realloc(). But seriously, maybe you want an STL container
           that automatically grows.
           \_ Not in this case.  I just need to grow a char array
              automatically myself.   (For speed reasons) Really, It's a
              very isolated place in the code, and I'll probably just end
	...
2004/8/10-11 [Computer/SW/Languages/C_Cplusplus, Computer/SW/Compilers] UID:32805 Activity:high
8/10    C question.  Is there anything wrong with the following?
        const my_struct_t **pp;
        pp = malloc(sizeof(my_struct_t *));
        pp = realloc(pp, sizeof (my_struct_t *) * 2);
        "gcc -Wall" doesn't complain.  But the M$ compiler (cl.exe) complains
        about the realloc line:
	...
2004/4/13-14 [Computer/SW/Languages/C_Cplusplus] UID:13175 Activity:high
4/13    How much C++/C knowledge do recent Berkeley CS/EECS grad have?
        \_ Class CSGrad inherits FromDaddy and does not implement C++Knowledge
           very well.
           \_ funny.  just the rich and poor as always.  the middle class can't
              afford education.
        \_ They know how to deal with pointers and addresses, malloc and free.
	...
2003/12/4-5 [Computer/SW/Languages/C_Cplusplus] UID:11317 Activity:nil
12/4    Is there any reason to worry about mixing the usage of malloc and new?
        Obviously, you can't delete() malloc'd memory (and vv), but aside from
        that, will bad things happen?  Oh, I should note that this is for a C
        library that is used by C++ -- a coworker suggested wrapping my memory
        allocations (and deletions) with
        #ifdef __cplusplus
	...
2003/1/18-19 [Computer/SW/Languages/C_Cplusplus] UID:27139 Activity:very high
1/17    If I have "foo_bar_baz_struct_t *p, *q;", which of the following
        is better?
        1.      p = malloc(sizeof (foo_bar_baz_struct_t));
                memcpy(q, p, sizeof (foo_bar_baz_struct_t));
        2.      p = malloc(sizeof *p);
                memcpy(q, p, sizeof *p);
	...
2001/2/27 [Computer/HW/Memory, Computer/SW/Languages/C_Cplusplus] UID:20713 Activity:high
2/26    Hey folks, I'm looking for something like Purify but cheap.
        What do people do for C programming to guard against memory
        bashing and leaks, other than write their own malloc and
        buy expensive software from Rational?
        \_ wasn't there some kind of program to analyze C code for
           no no's. I think it was called something like lint.
	...
2001/1/25-26 [Computer/SW/Languages/C_Cplusplus] UID:20431 Activity:high
1/25    C question.  I want to declare a variable p which stores the pointer to
        a malloc'ed array of 65536 elements.  Each of these 65536 elements
        in turn stores the pointer to a malloc'ed array or 16 int's.  What's
        the best way to declare p to include as much type information as
        possible?  Sure I can do "int ***p;", but that doesn't contain much
        type information.  Thanks.
	...
2000/2/11-13 [Computer/SW/Languages/C_Cplusplus] UID:17485 Activity:very high
2/10    I get an impression that new grads coming out of berkeley don't
        have much exposure to C.  I mean pure C, not C++.  How do most
        people feel about this?  I guess I'm asking alumni who are hiring
        and also current students.
        \_ I've spent the last year doing project in only C or Java, no C++.
        \_ they don't necessarily have much exposure to C++ either...
	...
2012/12/18-2013/1/24 [Computer/SW/Languages/Perl] UID:54561 Activity:nil
12/18   Happy 25th birthday Perl, and FUCK YOU Larry Wall for fucking up
        the computer science formalism that sets back compilers development
        back for at least a decade:
        http://techcrunch.com/2012/12/18/print-happy-25th-birthday-perl
        \_ I tried to learn Perl but was scared away by it.  Maybe scripting
           lanauages have to be like that in order to work well?
	...
2012/4/2-6/4 [Computer/SW/Languages/Java, Computer/SW/RevisionControl] UID:54353 Activity:nil
4/02    We use Perforce at work for revision control. It seems to work okay.
        Lately, a lot of the newer developers are saying that Perforce
        sucks and we should switch to Mercurial or Git. I have done some
        searching on the Internet and some others have this opinion. Added
        advantage is that Mercurial and Git are free. However, there would
        be some work to switch for the sysadmins and the developers.
	...
2011/4/16-7/13 [Computer/SW/Languages/Python] UID:54086 Activity:nil
4/16    Whoa, I just heard that MIT discontinued 6.001 (classic scheme)
        to 6.01. In fact, 6.00, 6.01 and 6.02 all use Python. What the
        hell? What has the world become? It's a sad sad day. SICP forever!
        \_ old story, they've ditched that shitty book and lang for a while.
        \_ I used to think scheme was cool, then I saw Ka Ping Yee's
           "Beautiful Code" class aka 61a in python, and converted.
	...
2011/4/26-7/13 [Computer/Theory, Health/Women] UID:54095 Activity:nil
4/26    Is it correct to say that Godel's work on the incompleteness thm
        proved the Principia Mathematica wrong?
        \_ It didn't exactly prove it wrong; it proved that the true goal of
           PM (a complete and consistent set of mathematical truths)
           is unattainable.  -tom
           \_ Ah cool, no this is good. See ok yeah so the main goal of PM
	...
2011/2/24-4/20 [Computer/SW/Languages/Java] UID:54048 Activity:nil
2/24    Go Programming Language.  Anyone here use it?  It kind of
        reminds me of java-meets python, and well, that is fitting given it's
        a GOOG product.  What is so special about it?
        \_ as I understand it, it's a suitable OOP-y systems language with more
           structure than C, less complexity than C++, and less overhead than
           Java/Python.
	...
2010/8/8-9/7 [Computer/SW/Languages/C_Cplusplus, Computer/SW/Languages/Web] UID:53914 Activity:nil
8/8     Trying to make a list of interesting features languages have
        touted as this whole PL field comes around, trying to see if they
        have basis in the culture of the time: feel free to add some/dispute
        1970 C, "portability"
        1980 C++, classes, oop, iterators, streams, functors, templates
             expert systems
	...