By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,647 Members | 1,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,647 IT Pros & Developers. It's quick & easy.

sizeof and dynamically allocated arrays

P: 7
According to a couple of other threads you can't use sizeof with dynamic arrays - you'll have to keep track of the memory allocated.

In my case, strings are filled into a dynamic array by a database query (postgresql), meaning that it is not readily possible to keep track (a preprocessor converts the database query to C code, using some functions that in turn do the memory allocation). I want to hand the array to a function that needs to make a copy of it (original array must stay intact); hence I must know the size.

Before you say this is impossible, take a look at this:
http://cm.bell-labs.com/cm/cs/who/dmr/vararray.pdf
Dennis Ritchie himself suggested to include the desired functionality in C 16 years ago! But using the terminology in the article my code won't compile (gcc 2.96, so somewhat old). Has it never been implemented?
Sep 1 '06 #1
Share this Question
Share on Google+
2 Replies


Banfa
Expert Mod 5K+
P: 8,916
What is suggested in that document never made it into the C standard, thank goodness. It sounds like a poor idea to me a bodge to get round a percieved problem and completely changing the scope of the sizeof operator.

As to your problem something somewhere must know how many items where filled in or needed to be filled in, post some of the code, we may be able to suggest something.
Sep 1 '06 #2

P: 7
Thank you Banfa. I'm sure there are good reasons why this functionality never got implemented, but for a fact it would be a convenience and a logical one too (which is why the issue has popped up several times in these forums). I mean, you would think a function to return the size of an object would be most useful when the object could have variable size whereas the size of fixed objects would be known. Furthermore you'd think it should be a simple task; there must be something to identify the "end" of the object in memory (the pointer identifying where it starts). Well, that's the logics of a C noob anyway. Obviously it's not that simple.

I think diving into the postgresql code to find the memory allocation part and then trying to modify it would be way too time consuming for me. Instead I'll drop the dynamic arrays and work out the memory allocation manually.
Sep 2 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.