ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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;
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.