473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

sizeof and dynamically allocated arrays

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
2 3282
Banfa
9,065 Expert Mod 8TB
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
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

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

Similar topics

13
by: Lokicer | last post by:
hi I am a newbie in c++, help me! thanks in much lokicer
21
by: sugaray | last post by:
hi, it just came up my mind that since we can get the length of any given string literal S with 'sizeof S-1', so, what's the merit of library function strlen()'s existence ? thanx in advance for...
11
by: D | last post by:
hi, i would like to know how to calculate the size of a dynamic array created using a dereference declaration like int *numbers and allocating via malloc or calloc: numbers=(int...
15
by: stand__sure | last post by:
Having recently had a need to use ZeroMemory from the kernel32.dll, I "discovered" that there is apparently no .NET equivalent to the c/c++ sizeof operator -- one does exist in the Marshall...
37
by: yogpjosh | last post by:
Hello All, I was asked a question in an interview.. Its related to dynamically allocated and deallocated memory. eg. //start char * p = new char; ...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
3
by: lisp9000 | last post by:
I was wondering the best way to define and loop through a character array. Most lines of the file I am processing are 80 characters long but when an error occurs in the client which created the log...
10
by: Slain | last post by:
I am using sizeof to doa memcpy but run into probs because of the following problem. static char arr1 = {0x01,0x00,0x03,0x04}; static char arr2 = {0x00,0x03,0x04,0x06,0x07}; const char*...
7
by: alternative451 | last post by:
Hi, I have just one question : how to know the size of an array, i have un little program, i use first static, ant i can use sizeof() to know the size, but when i put it as paremeter in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.