473,407 Members | 2,312 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,407 software developers and data experts.

Banging head against a wall

I took over some code for someone here and I have a real irritating
situation.

There are 20 or so structs that are initialized ad I need to change
some of the values of these structs based on a user defined option.

So, 20 struct arrays like this:
<code>
typedef struct{
int a;
} MY_STRUCT;

MY_STRUCT stuff_1[]={{1},{2}};
MY_STRUCT stuff_2[]={{1},{2},{3},{4}};
// etc
MY_STRUCT stuff_20[]={{1},{2},{3}};
</code>

In a perfect world, I could initialize these structs with the variable
I want, but I can't. I can't use a pre processor #define as the value
is being read from FLASH (it's an embedded device) at startup.
I can store pointers to each of the struct arrays, but then I can't
tell the sizeof the array. In other words stuff_2 is actually a
MY_STRUCT array with a length of 4.

Given:
<code>
MY_STRUCT* pStuff = (Y_STRUCT*)&my_stuff_2;
</code>

Is there any way to determine the depth of pStuff?

sizeof(*pStuff)/sizeof(MY_STUFF) doesn't work cause sizeof(*pStuff)
returns sizeof(MY_STUFF)
Anyway, hopefully you see my situation and might have a creative idea.

Thanks for any help,
Steve

Sep 9 '05 #1
4 1356
* St********@gmail.com:
I took over some code for someone here and I have a real irritating
situation.

There are 20 or so structs that are initialized ad I need to change
some of the values of these structs based on a user defined option.

So, 20 struct arrays like this:
<code>
typedef struct{
int a;
} MY_STRUCT;
In C++ style,

struct MyStruct{ int a; };

You don't need a 'typedef'. And (it's a good idea to) don't use all uppercase
names for anything but macros.

MY_STRUCT stuff_1[]={{1},{2}};
MY_STRUCT stuff_2[]={{1},{2},{3},{4}};
// etc
MY_STRUCT stuff_20[]={{1},{2},{3}};
</code>

In a perfect world, I could initialize these structs with the variable
I want, but I can't. I can't use a pre processor #define as the value
is being read from FLASH (it's an embedded device) at startup.
If you said, the values you want are not static, then it would make sense.

But if you could otherwise use a #define then the values must be static and
then I don't see why you couldn't have them in flash memory.
I can store pointers to each of the struct arrays, but then I can't
tell the sizeof the array.
First, this seems to be unrelated to the above, and second, why on earth not?

In other words stuff_2 is actually a MY_STRUCT array with a length of 4.
Is that supposed to clarify the previous sentence?

Given:
<code>
MY_STRUCT* pStuff = (Y_STRUCT*)&my_stuff_2;
</code>

Is there any way to determine the depth of pStuff?
Supposing you mean the array size, yes:

static std::size_t const stuffSize = sizeof(my_stuff_2)/sizeof(*my_stuff_2);

sizeof(*pStuff)/sizeof(MY_STUFF) doesn't work cause sizeof(*pStuff)
returns sizeof(MY_STUFF)
Anyway, hopefully you see my situation
Nope.

and might have a creative idea.


Yes, try to figure out what 1 (one) problem is, and give an example of that.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 10 '05 #2
use vectors (?)
<St********@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
I took over some code for someone here and I have a real irritating
situation.

There are 20 or so structs that are initialized ad I need to change
some of the values of these structs based on a user defined option.

So, 20 struct arrays like this:
<code>
typedef struct{
int a;
} MY_STRUCT;

MY_STRUCT stuff_1[]={{1},{2}};
MY_STRUCT stuff_2[]={{1},{2},{3},{4}};
// etc
MY_STRUCT stuff_20[]={{1},{2},{3}};
</code>

In a perfect world, I could initialize these structs with the variable
I want, but I can't. I can't use a pre processor #define as the value
is being read from FLASH (it's an embedded device) at startup.
I can store pointers to each of the struct arrays, but then I can't
tell the sizeof the array. In other words stuff_2 is actually a
MY_STRUCT array with a length of 4.

Given:
<code>
MY_STRUCT* pStuff = (Y_STRUCT*)&my_stuff_2;
</code>

Is there any way to determine the depth of pStuff?

sizeof(*pStuff)/sizeof(MY_STUFF) doesn't work cause sizeof(*pStuff)
returns sizeof(MY_STUFF)
Anyway, hopefully you see my situation and might have a creative idea.

Thanks for any help,
Steve

Sep 10 '05 #3
Someonekicked wrote:
use vectors (?)


Yes absolutely, use vectors is the C++ solution. But since the code
looks like C to me, maybe the OP really wants a C solution.

Maybe this

void change_values(MY_STRUCT* array, size_t array_size)
{
...
}

change_values(stuff_2, sizeof stuff_2 / sizeof stuff_2[0]);

I.e. pass the size of the array that is being changed to the function
that does the work.

As the OP seems to be aware using sizeof like that is a bit dangerous,
which is why using vectors not arrays is the recommended solution.

john
Sep 10 '05 #4
Hello John,

I was hoping to avoid the use of vectors for size reasons, we have a
very small footprint to work with and space is precious ;)

I ended up implementing a solution very similar to what you suggested.
I can easily find the size of an array when I'm working with the array
variable, but a dereferenced pointer to the same variable will only
give me the size of that dereferenced type. I find this odd.

But, solution solved for now, and plans to redesign this whole section
of code are well under way.

Thanks again for the post.
Thank you also to someonekicked.
Alf,
The problem was unclear, I just re-read my post and saw that I jumped
to my proposed solution's problem without explaining that solution.
Due to the fact that I can't initialize my structs with the variable, I
had tried to "set" the value after initialization with a separate
function. My original attempt was to generate an array of ptrs to the
structs, then dereferenced each pointer to further determine it's array
depth and then set the values for each item within (array of arrays)
but that was not working for me as I couldn't get the size of the
nested arrays.
<snip>
sizeof(my_stuff_2)/sizeof(*my_stuff_2)
</snip>

sizeof() passed a dereferenced pointer to an array will not return the
sum of the size of all items in the array, it will return the size of
the first item. Hence my problem.

I'd thank you for your post, but your attitude annoyed me, lighten up.

Sep 12 '05 #5

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

Similar topics

13
by: showme | last post by:
PHP and ASP.NET Go Head-to-Head By Sean Hull http://otn.oracle.com/pub/articles/hull_asp.html SUMMARY at the BOTTOM Speed and efficiency. As I mentioned earlier, ASP.NET is a framework...
87
by: ziliath | last post by:
I recently tried out the Google "top coder" contest, as a C++ coder. I noticed immediately that they expected me to know STL. To which I say, what the fuck?! I may be missing something, but at...
25
by: Michael Schuerig | last post by:
I'm trying to do something seemingly very simple, but it's brought me close to crushing my head on the keyboard. All I want is a table where the head row is fixed and the body columns below are...
4
by: Laura | last post by:
Here's the situation: I'm trying to use an update query to copy data from one row to another. Here is the situation: I have 5 companies that are linked to each other. I need to show all 5...
10
by: Brian W | last post by:
Hi All, I have a web user control that, among other things, provides Print this page, and Email this page functionality I have this script that is to execute on the click of the asp:hyperlinks ...
1
by: Martin Z | last post by:
I'm getting acquainted with the whole XML/XSD thing, and I've run into a wall. I have a tree of objects that I deserialize from XML for configuration reasons. I have generated XSD from 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: 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...
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,...
0
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...

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.