Thread: Initialising unsigned integer pointer location in memory

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    Initialising unsigned integer pointer location in memory

    Hi i am trying to initialise my bitcontents in my structure to zero while making a new structure.My current method is not working .Any suggestions



    Code:
    / a bit vector struct
        struct bitset{
            int size;
            unsigned int *bitcontents;
            char buffer[27];
    
    
        };
     /*initialisng a bitset struct to the size of the elements
     */
      struct bitset * bitset_new(int size){
          struct bitset * p=  ( struct  bitset *)  malloc(sizeof(*p));
          p->size=size;
              p->bitcontents=  (unsigned int*) malloc( (sizeof(int) *  size/8)  + (size % 8)? 1:0) ;
    
       int i=0;
              while(i<31){
              p->bitcontents[i]=0;
              i++;
    
              }
    
              return p;
      }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Not sure what errors you're getting, but...

    What is sizeof(*p) when not only has p not been declared yet, but even if it did, what would p be pointing to to dereference? Rethink Line 12.

    Line 14 ? So you are either allocating 1 byte or 0 bytes to assign to p->bitcontents ?

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    There are standard functions to do this kind of thing, qv. memset().

    FYI, though it would be bad practice to count on it, modern operating systems always zero out heap memory anyway (partially for security and because of virtual addressing).

    But your method should work. Why do you believe it has not?

    You should determine the size of bitcontents and use it consistently -- you have a formula in the malloc, then in the while loop you just hardcode 31. Tch.

    I notice you are using casts with malloc(). Are you still using a C++ compiler, as per bit abstract data type Why?
    Last edited by MK27; 11-20-2011 at 12:15 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Tclausex View Post
    What is sizeof(*p) when not only has p not been declared yet, but even if it did, what would p be pointing to to dereference? Rethink Line 12.

    Line 14 ? So you are either allocating 1 byte or 0 bytes to assign to p->bitcontents ?
    There's nothing wrong with sizeof(*p) In fact it's the best method for getting he size correct and writing maintainable code that there is. sizeof never cares when it's written as it doesn't exceute at runtime anyway, it executes at compile-time.

    Line 14, not he's allocating many bytes, and adding 1 or not just to round up. Now both of those lines do have issues, but they are not what you commented on.

    OP: You do need to explain what "not working" means. Not compiling? Not running? Not giving the right result?
    You should not be casting the return value of malloc.
    The easier way to round up is (size+7)/8
    Why is there a 31 in that code? You don't know that the buffer allocated is even this big.
    What is buffer for, it is unused?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by iMalc View Post
    There's nothing wrong with sizeof(*p) In fact it's the best method for getting he size correct and writing maintainable code that there is. sizeof never cares when it's written as it doesn't exceute at runtime anyway, it executes at compile-time.
    I was not aware of that. Thanks. And yeah, I misread the ternary.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by iMalc View Post
    There's nothing wrong with sizeof(*p) In fact it's the best method for getting he size correct and writing maintainable code that there is. sizeof never cares when it's written as it doesn't exceute at runtime anyway, it executes at compile-time.

    Line 14, not he's allocating many bytes, and adding 1 or not just to round up. Now both of those lines do have issues, but they are not what you commented on.

    OP: You do need to explain what "not working" means. Not compiling? Not running? Not giving the right result?
    You should not be casting the return value of malloc.
    The easier way to round up is (size+7)/8
    Why is there a 31 in that code? You don't know that the buffer allocated is even this big.
    What is buffer for, it is unused?
    when i try to initialise my bitcontents with 0.it does not work thats what i meant by the code not working.I am sorry for not beeing clear

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by MK27 View Post
    There are standard functions to do this kind of thing, qv. memset().

    FYI, though it would be bad practice to count on it, modern operating systems always zero out heap memory anyway (partially for security and because of virtual addressing).

    But your method should work. Why do you believe it has not?

    You should determine the size of bitcontents and use it consistently -- you have a formula in the malloc, then in the while loop you just hardcode 31. Tch.

    I notice you are using casts with malloc(). Are you still using a C++ compiler, as per bit abstract data type Why?
    ye i think its from the compiler cause it gave me a warning..When i put in zero to my bitcontents for 32 bits.. it initialises some index with zero but some are still left with junk in it. e.g this is my output when i print it out
    0000260108816100000026000000000.. it should be alll zeros


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assembly Integration (Unsigned Integer Multiplication)
    By LanguidLegend in forum C Programming
    Replies: 0
    Last Post: 05-03-2010, 06:10 PM
  2. range of unsigned integer
    By hitesh_best in forum C Programming
    Replies: 2
    Last Post: 12-23-2007, 05:52 AM
  3. convert from an integer to an unsigned char
    By Landroid in forum C Programming
    Replies: 4
    Last Post: 05-02-2005, 01:43 AM
  4. How do I print 64 bit unsigned integer in hex?
    By electrolove in forum C Programming
    Replies: 7
    Last Post: 02-11-2003, 12:43 PM
  5. packing two characters into an unsigned integer.
    By Nutshell in forum C Programming
    Replies: 17
    Last Post: 01-30-2002, 02:31 AM

Tags for this Thread