473,769 Members | 8,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof and dynamically allocated arrays

7 New Member
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 3312
Banfa
9,065 Recognized Expert Moderator Expert
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
flyvholm
7 New Member
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
2260
by: Lokicer | last post by:
hi I am a newbie in c++, help me! thanks in much lokicer
21
8489
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 your instruction.
11
12037
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 *)malloc(sizeof(int)*10); using sizeof(numbers) will return the pointers size... is there a possibility?
15
4520
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 namespace, but it fails when passed a managed type. I poked around the Reflection namespace, but found nothing that fits the bill. Is there a runtime equivalent to sizeof in ..NET?
37
2785
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
4770
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 that I found inside of a string. Any ideas?
3
1691
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 sometimes they can be much longer so what's the best way to determine the array and define it? char s; vs
10
339
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* string_CDR= {arr1,arr2}; cout<<"The size of is " <<sizeof(arr1)<<" and "<<sizeof(arr2) <<endl;
7
3118
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 function, size of return "1". ex int tab; printf("%d",sizeof(tab)/sizeof(int)); // print 10 int length(int *tab)
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.