Dynamic array of pointers, how many bytes to realloc?

This is a discussion on Dynamic array of pointers, how many bytes to realloc? within the C Programming forums, part of the General Programming Boards category; I'm writing an addToArray function in C like the one described here . However, instead of passing struct of type ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    8

    Dynamic array of pointers, how many bytes to realloc?

    I'm writing an addToArray function in C like the one described here.

    However, instead of passing struct of type DATA to this function, I want to pass a pointer to a struct of type zval -- the basic idea is that I want to remember the address of the zval object. Here's the actual code of my function:
    Code:
    static int amf_remember_object(zval *obj) /* {{{ */
    {
    	if(AMF_G(objects_and_arrays_count) == AMF_G(objects_and_arrays_allocated)) // Are more refs required?
    	{
    		// Feel free to change the initial number of refs
            // and the rate at which refs are allocated.
                if (AMF_G(objects_and_arrays_allocated) == 0)
                	AMF_G(objects_and_arrays_allocated) = 3; // Start off with 3 refs
                else
                	AMF_G(objects_and_arrays_allocated) *= 2; // Double the number of refs allocated
    
                // Make the reallocation transactional
                // by using a temporary variable first
                // TODO: use erealloc?
                void *_tmp = realloc(AMF_G(objects_and_arrays), (AMF_G(objects_and_arrays_allocated) * sizeof(obj)));
    
                // If the reallocation didn't go so well,
                // inform the user and bail out
                if (!_tmp)
                {
        			php_error_docref(NULL TSRMLS_CC, E_ERROR,
        					"ERROR! Couldn't realloc memory");
        			return(-1);
                }
    
                // Things are looking good so far
                AMF_G(objects_and_arrays) = (zval**)_tmp;
        }
    
    	AMF_G(objects_and_arrays)[AMF_G(objects_and_arrays_count)] = obj;
    	AMF_G(objects_and_arrays_count)++;
    
        return AMF_G(objects_and_arrays_count);
    } // amf_remember_object()
    /* }}} */
    Note that I'm using sizeof(obj) to determine how many bytes to allocate -- i have no idea if that is correct. Since I am storing pointers in my array, what expression should I use for calculating my byte count? will sizeof(obj) return the number of bytes required for a pointer or something else? Will this work on 32-bit and also 64-bit machines?

    Any help would be much appreciated.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > will sizeof(obj) return the number of bytes required for a pointer or something else?
    You just get the size of a zval pointer, which is what you want.

    > Will this work on 32-bit and also 64-bit machines?
    Yes.

    > if (!_tmp)
    If this is true, your OLD pointer is still valid.
    The caller will still need to call free() on it at some point, to avoid a memory leak.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    8
    Thank you, Salem. I truly appreciate your response.
    Quote Originally Posted by Salem View Post
    > if (!_tmp)
    If this is true, your OLD pointer is still valid.
    The caller will still need to call free() on it at some point, to avoid a memory leak.
    Thank you for your vigilance and the extra info.

    I'm not sure if you noticed, but I'm working on an extension to PHP. Instead of realloc, I'll be using a function called erealloc which wraps memory allocation inside a memory management layer. I'm not sure if php_error_docref will halt code execution. I hope that code execution will continue, resulting in a return value of -1 for this function and the point of execution will return to the function which is the entry point to my code, where the array/pointer AMF_G(objects_and_arrays) will be freed like so:
    Code:
    if (AMF_G(objects_and_arrays)) {
      efree(AMF_G(objects_and_arrays));
    }
    I hope that will properly handle the matter of freeing memory. If it does not, I'm given to understand that ZendMM tracks memory allocated using erealloc such that it knows which memory was allocated by which "request" and if the request bails out, then ZendMM will implicitly free it up. I hope I'm not missing anything here.

    And, if you have a moment, perhaps you could comment on whether you think this function is a suitable counterpart to my remember function above:
    Code:
    /**
     * Searches the global array of arrays/objects for a zval to assist with (de)serialization by reference
     * @static
     * @param zval *obj A pointer to the zval we'd like to find
     * @return int The index of the object in the global array of arrays/objects or -1 if the object is not found
     */
    static int amf_find_object(zval *obj) /* {{{ */
    {
    	if (!zval) {
    		return -1;
    	}
    	if (AMF_G(objects_and_arrays_count) == 0) {
    		return -1;
    	}
    
    	int i;
    	for(i = 0; i < AMF_G(objects_and_arrays_count); i++){
    		if (AMF_G(objects_and_arrays)[i] == obj) {
    			break;
    		}
    	}
    	if (i == AMF_G(objects_and_arrays_count)){
    		return -1;
    	} else {
    		return i;
    	}
    } // amf_find_object()
    /* }}} */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic array of pointers
    By rohams in forum C Programming
    Replies: 2
    Last Post: 05-12-2011, 03:13 PM
  2. Dynamic Array of Pointers to String
    By renjith.resmi in forum C++ Programming
    Replies: 0
    Last Post: 10-19-2009, 07:48 PM
  3. dynamic array of pointers?
    By baniakjr in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2006, 08:46 AM
  4. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM
  5. **Pointer to a Dynamic Array of Pointers
    By The Brain in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2005, 06:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21