473,396 Members | 1,809 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,396 software developers and data experts.

Dynamic size of struct

ose
If a struct declared as:

struct x {int a; int b; string c;}

Since string c's content could be changed at runtime, does this mean that
"sizeof struct x" could be dynamic and changed at runtime as well? Is this a
good, valid way of using "struct"?
Jul 27 '08 #1
3 2448
ose wrote:
If a struct declared as:

struct x {int a; int b; string c;}

Since string c's content could be changed at runtime, does this mean that
"sizeof struct x" could be dynamic and changed at runtime as well?
No.
Is this a good, valid way of using "struct"?
Your member names are a little bit on the meaningless side. But the struct
is clearly valid and sometimes a struct like that (with better named
fields) even qualifies as good style.
Best

Kai-Uwe Bux
Jul 27 '08 #2
Sam
ose writes:
If a struct declared as:

struct x {int a; int b; string c;}

Since string c's content could be changed at runtime, does this mean that
"sizeof struct x" could be dynamic and changed at runtime as well?
Of course not. Let's substitute your string with a char *:

struct x {int a; int b; char *c; }

sizeof(char *) is always the same, no matter how large the character string
the pointer points to. The pointer itself has constant size. Typically, on
32 bit machines the pointer is 4 bytes long, on 64 bit machines it's 8 bytes
long.

With your string, the concept is the same.
Is this a
good, valid way of using "struct"?
I see nothing wrong with that.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkiNISYACgkQx9p3GYHlUOJVUgCfc8bFZc8aRe ZM7YvdIDa0yMcW
WiYAnjn2RDNPlWf/myqa7iiUoWPh84i5
=4hL+
-----END PGP SIGNATURE-----

Jul 28 '08 #3
In article
<1g*********************@bgtnsc04-news.ops.worldnet.att.net>,
"ose" <os***@hotmail.comwrote:
If a struct declared as:

struct x {int a; int b; string c;}

Since string c's content could be changed at runtime, does this mean that
"sizeof struct x" could be dynamic and changed at runtime as well? Is this a
good, valid way of using "struct"?
By 'string' I presume you mean the C++ standard string class.

Internally the std::string class maintains a pointer to a string; thus,
'string c' in your declaration above is akin to:

struct x { int a; int b; struct { overhead decls; char *ptr; } c };

Because you don't know the size of std::string because the overhead it
maintains is not defined, you cannot predict the size of struct x;
however, the overall size of struct x is fixed.

Internally variable length strings are maintained by the std::string
class by the equivalent of realloc() on ptr; thus, your struct points to
another chunk of memory that may bounce around and change size in the
heap as you manipulate the string c.

Hope this helps.

--
William Edward Woody - wo***@alumni.caltech.edu
Chaos In Motion - http://www.chaosinmotion.com

Freedom is the non-negotiable demand of human dignity;
the birthright of every person‹-in every civilization.
- National Security Strategy of the United States
Jul 31 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Geetesh | last post by:
Recently i saw a code in which there was a structer defination similar as bellow: struct foo { int dummy1; int dummy2; int last }; In application the above array is always allocated at...
6
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
11
by: Marco Loskamp | last post by:
Dear list, I'm trying to dynamically generate functions; it seems that what I really want is beyond C itself, but I'd like to be confirmed here. In the minimal example below, I'd like to...
15
by: rwf_20 | last post by:
I just wanted to throw this up here in case anyone smarter than me has a suggestion/workaround: Problem: I have a classic producer/consumer system which accepts 'commands' from a socket and...
26
by: Andrew Poelstra | last post by:
I hacked this together this morning so that I could shift my out-of- space code away from the rest of my logic. I wanted to allow array syntax on my dynamic buffers, so I manually created a struct...
13
by: coosa | last post by:
Dear all, Using the conio implementation i wanted to create a dynamic string, whereby its size would be determined after each keyboard hit; in other words, i don't want to ask the user to...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
28
by: hlubenow | last post by:
Hello, I really like Perl and Python for their flexible lists like @a (Perl) and a (Python), where you can easily store lots of strings or even a whole text-file. Now I'm not a...
20
by: sirsnorklingtayo | last post by:
hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE()...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...
0
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,...

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.