473,396 Members | 1,843 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.

variable sized structure

Hi All,

Consider the following piece of code:

struct foo1
{
unsigned char tag;
unsigned char length;
unsigned char value[];
};

struct foo2
{
unsigned char tag;
unsigned char length;
unsigned char value[];
};

struct foo3
{
unsigned char tag;
unsigned char length;
unsigned char value[];
};

struct foo4
{
unsigned char tag;
unsigned char length;
unsigned char value[];
};


struct abc
{

struct foo1 foo_1;
struct foo2 foo_2;
struct foo3 foo_3;
struct foo4 foo_4;

};

In 'C' struct foo1,foo2,foo3,foo4 cannot be members of struct abc as they declared with empty index. Is there any another method which solves the above scnerio to dynamically fill data into arrays in the structures.
Apr 3 '07 #1
3 2478
JosAH
11,448 Expert 8TB
You could change those unsigned char values[] arrays to simple unsigned char* values
pointers and add a variable size array at the end of your abc struct. You do need
to initialize your pointers in the modified abc struct:
Expand|Select|Wrap|Line Numbers
  1. struct abc* p= malloc( ... );
  2. p->foo_1.value= p->data;
  3. p->foo_2.value= foo_1.value+foo_1.length;
  4. p->foo_3.value= foo_2.value+foo_2.length;
  5. p->foo_4.value= foo_3.value+foo_3,length;
I personally find it a bit 'mechanical' but that's what you get with those variable
length structs ;-)

kind regards,

Jos
Apr 3 '07 #2
hi Jos,
Thanks for ur reply.

we cannot use malloc() for our processor(AVR Series from atmel).is there any alternate way to allocate memory dynamically?.

Best Regards
Ramkrishna

You could change those unsigned char values[] arrays to simple unsigned char* values
pointers and add a variable size array at the end of your abc struct. You do need
to initialize your pointers in the modified abc struct:
Expand|Select|Wrap|Line Numbers
  1. struct abc* p= malloc( ... );
  2. p->foo_1.value= p->data;
  3. p->foo_2.value= foo_1.value+foo_1.length;
  4. p->foo_3.value= foo_2.value+foo_2.length;
  5. p->foo_4.value= foo_3.value+foo_3,length;
I personally find it a bit 'mechanical' but that's what you get with those variable
length structs ;-)

kind regards,

Jos
Apr 3 '07 #3
JosAH
11,448 Expert 8TB
hi Jos,
Thanks for ur reply.

we cannot use malloc() for our processor(AVR Series from atmel).is there any alternate way to allocate memory dynamically?.

Best Regards
Ramkrishna
If you use those little Atmels (an Atmega?) you are on your own, i.e. you can
make that pointer p (see my previous reply) point to anything you like. You
have to manage your own memory.

kind regards,

Jos
Apr 3 '07 #4

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

Similar topics

4
by: Piotr Sawuk | last post by:
Hello, I'm new in this group and new to c++ programming. And I already have my first question which wasn't answered by any text-book on c++ programming I have seen so-far: How can I define a...
13
by: HappyHippy | last post by:
Hi, I'm wondering what you think about this piece of code: #include<iostream> int main() { int size; std::cin >> size;
2
by: Michael B Allen | last post by:
My understanding is that struct members will remain in order such that the following can be used to support variable sized members: struct indexed_values { int *values; unsigned char bitset;...
5
by: frankhall36 | last post by:
Yeah, I need some help, I'm not a very good programmer but I've tried a lot of languages, and anyways, I want to start applying programming to physics, and I would like to learn how to make a...
19
by: Skybuck Flying | last post by:
Hi, I think I might have just invented the variable bit cpu :) It works simply like this: Each "data bit" has a "meta data bit". The meta data bit describes if the bit is the ending bit...
4
by: fabio | last post by:
Hello, zero-sized arrays (which I find very useful, since I do a lot of packet-style programming with circular buffers) cannot be declared if they're in a struct/class that later will be inherited...
4
by: Himanshu Singh Chauhan | last post by:
Hello All!! Can anybody tell what variable sized structures are and how can they be used? regards --Himanshu
5
by: Lyle Avery | last post by:
Hello guys, Look at this in c++ file: class T { public: char c; char ca; };
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
8
by: Andrew Smallshaw | last post by:
I'm working on a data structure that began life as a skip list derivative, but has evolved to the point that it now only has a passing resemblance to them. Each node of this structure has a few...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
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.