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

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);
        Thanks.
        \_ 1 is far more obvious for me...
        \_ 2 is better if you think you are going to change
          what type of struct p points to frequently. 1 is
          the preferred way of coding for readability.
        \_ Well, I like 2.  It's more concise, and you can't screw up
           and put in the wrong type in the sizeof.
           \_ My vote is for #2 too. Also, I like *q = *p better than
              the memcpy. And no, for someone familiar with C, I don't
              see how the memcpy makes anything clearer.
              \_I don't think that they are necessarily the same in straight C.
              In C++ that may be equivalent. Also, shouldn't you be mallocing q?
              \_I don't think that they are necessarily the same in straight
                C. In C++ that may be equivalent. Also, shouldn't you be
                malloc'ing q?
                        \_ It seems so. Also, shouldn't OP be checking if
                           the pointer returned by malloc is NULL?
                \_ You are exactly backwards. -pld
                \_ you think wrong.  In C bar=foo does a shallow copy.
                  \_ Yes, we know this, but the question is does memcpy
                     do a shallow copy also? Somehow I think this is
                     wrong. From what I remember memcpy does a bit for bit
                     copy, which is very problematic once you get into things
                     like structs and classes. The two may not be equivalent,
                     and it certainly would come out different in the
                     object code.
                     \_ How do shallow and bit-for-bit differ? Very curious-pld
                       \_ It may or may not, it depends on what the struct
                       actually contains. That's the crux of the argument.
                       If the struct contains a class or class pointer(assuming
                       you are doing this in c++) you
                       run into all sorts of different behaviors.
                       \_ The proper answer is "they're identical"  -pld
                          \_ No they are not.  Shallow copy copies pointers,
                             deep copy copies objects also (potentially
                             using copy constructors, etc.)
                  \_ Is it possible that this is a Visual C++ extension for C?
                     Possibly, if they are on crack. You can do this in C++ by
                     defining your own operator= method.
2025/05/25 [General] UID:1000 Activity:popular
5/25    

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
	...
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...
	...
1999/8/18-20 [Computer/SW/Languages/C_Cplusplus, Computer/SW/Compilers] UID:16336 Activity:nil
8/19    C question: I rarely, if ever, remember to include stdlib.h, which
        includes malloc, atoi, etc. But I use these functions on a regular
        basis. I haven't encountered a problem until I tried using atof(...)
        also in stdlib.h. Once I included stdlib.h, my problems went away.
        So why didn't I have problems with the other functions?
        \_ your compiler was assuming that undefined functions returned an int.
	...