Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
sruct two_tables
{
int *int_table; char *char_table;
};

How would you reserve space in memory for a table of 100 structures of type struct two_tables? How much space does it take up in memory?

Help with my problem pls.

What I have tried:

this is my homework but i dont really know what does that mean cause i suck codding and english so please help me
Posted
Updated 16-Jul-22 21:52pm
v2

You can treat a struct as any other datatype (if you spell teh keyword correctly first):
sruct two_tables
{
int *int_table; char *char_table;
};
Should be
struct two_tables
{
int *int_table; char *char_table;
};
Or better:
struct two_tables
    {
    int *int_table; 
    char *char_table;
    };
Then declare an array of them just as you would any other datatype.

How much memory do they take up? That's easy: sizeof operator - cppreference.com[^]
 
Share this answer
 
This is your homework, so there's very little chance anyone's going to just give you the answer. But here's somethings to think about.

Do you know how to "reserve memory" for a "table" of simple items, say of int?
If not, you need to go back and review your course notes.
If so, then how would you change the code to allocate a "table" of type struct two_table?

Do you know that the sizeof operator tells you the size of an object? You can use that information to figure out how much space an object requires.

You'll note that I have used quotes around "table" in my answer. That's because a "table" of objects in C/C++ is normally referred to as an array.
 
Share this answer
 
It would be nice if you name the topic of the question already in the headline. If everyone chooses headings like this question it will be difficult to keep them apart or find them again. Also it could improve the chance to get answers.

I have changed the heading for you, before more people complain about it here.

The code fragment belongs under "What I have tried" and should be marked to match the code.

OriginalGriff already told you about the spelling mistake in your source code.

You asked for help for your homework without specifying what constraints or frameworks to consider. So someone answering would have to guess what the meaning of the task could be and which answers match your knowledge.

Now to the approaches to the two questions:

1. how would you allocate memory for a table with 100 structures of type struct two_tables?
Since a solution in C++ is looked for a C++ solution would fit, which initializes also equal the created data structures. Here containers from the STL library would come into question, e.g. a std::array or a std::vector.

Example:
C++
std::vector<struct two_tables> v(100);

2. how much space does it take in memory?
This question can be easily answered by determining the size of the structure with sizeof(), multiplying the determined size by n and outputting the result. For didactic reasons, it also makes sense to determine the two data types in the structure individually and then add them up.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900