LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-07-2011, 02:31 PM   #1
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Rep: Reputation: 78
dynamic array, 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.
 
Old 11-07-2011, 05:12 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,150

Rep: Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264
Since obj is a pointer, sizeof(obj) is the size of a pointer in bytes.
Your realloc looks ok as long as AMF_G(objects_and_arrays) is initially NULL.

The AMF_G() thing is a little weird. Its used to get global values. If you have a global structure declared somewhere, why not just use a pointer to it?
Code:
if (amf_global->objects_and_arrays_allocated == 0)
        amf_global->objects_and_arrays_allocated = 3;
 
1 members found this post helpful.
Old 11-07-2011, 05:30 PM   #3
ewqdsacxz
LQ Newbie
 
Registered: Aug 2011
Posts: 5
Blog Entries: 1

Rep: Reputation: 1
sizeof returns the size in bytes of the datatype of the variable you pass to it, in this case obj of type (zval*), on the architecture you are compiling for (x86, x86-64, ARM...). You are storing (zval*)s, so this is what you want. At compile time, it just takes the size of a pointer to zvals for that architecture and substitutes in the literal number: 4 in the case of x86, 8 in the case of x86-64.

Also see http://en.wikipedia.org/wiki/Sizeof
Edit: ninja'd

Last edited by ewqdsacxz; 11-07-2011 at 05:36 PM. Reason: 忍者
 
1 members found this post helpful.
Old 11-07-2011, 06:40 PM   #4
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,150

Rep: Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264
Quote:
Originally Posted by ewqdsacxz View Post
Edit: ninja'd
With cat-like tread,
Upon our prey we steal.
In silence dread,
Our cautious way we feel
 
1 members found this post helpful.
Old 11-07-2011, 09:44 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,673
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
If it were me, I'd be looking into setting up an abstract data type (or, more likely, finding one that already exists) which provides "an arbitrary-sized collection of objects indexed by an integer." Reallocating a contiguous block of storage can get messy, but who says it has to actually be one contiguous block?
 
  


Reply

Tags
dynamic array, pointers



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C realloc resize array? Thesniperofdeath Programming 2 03-06-2011 07:33 PM
c++ dynamic array Fredstar Programming 16 03-19-2009 08:26 PM
in external function: realloc of array within a struct ruh31 Programming 1 01-05-2008 10:02 AM
C: a float from 2 bytes of a 4-byte array carcassonne Programming 8 01-17-2007 06:23 PM
how to transform an object into an array of bytes on Java! poeta_boy Programming 3 02-15-2004 07:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration