Hi,
I know some basic C and am currently trying to understand some source code.
I enclose some relevant snippets of code below.
My question is this:
The code creates a large array of chars, called t_data. The first 10
spaces are then filled with characters.
However, the line:
params = (struct param *)&t_data;
then, I think (not entirely sure), creates a variable of type param
(struct defined elsewhere), starting at the address of the start of t_data?
So, to my mind the values previously written to t_data are overwritten
so there is no point to the code filling the first 10 entries of the
array? Is this correct?
Hopefully I have provided enough context below,
Many thanks,
Stephen
(... represents cropped code)
(the struct param is defined in a .h file, structure of ints and chars)
#define T_DATA_MAX 10000000
unsigned char t_data[T_DATA_MAX];
struct param *params;
....
for(i=0; i<10; i++)
{
t_data[i] = i;
}
....
params = (struct param *)&t_data; 4 1475
Stephen Kershaw wrote: Hi,
I know some basic C and am currently trying to understand some source code. I enclose some relevant snippets of code below.
My question is this: The code creates a large array of chars, called t_data. The first 10 spaces are then filled with characters. However, the line: params = (struct param *)&t_data; then, I think (not entirely sure), creates a variable of type param (struct defined elsewhere), starting at the address of the start of t_data?
It initialises a pointer to structure of type param, with the address
of the array t_data[]. Since they are both objects of different types a
cast is needed.
BTW, params = (struct param *) t_data would also be the same.
So, to my mind the values previously written to t_data are overwritten so there is no point to the code filling the first 10 entries of the array? Is this correct?
No, the values written to t_data are still there and can still be
accessed, however their access via the pointer params might or might
not be meaningful. It depends on the implementation and the intent of
the programmer who wrote the code.
Stephen Kershaw wrote: Hi,
The code creates a large array of chars, called t_data. The first 10 spaces are then filled with characters. However, the line: params = (struct param *)&t_data; then, I think (not entirely sure), creates a variable of type param (struct defined elsewhere), starting at the address of the start of t_data?
params should have been declared as a pointer-to-(struct param):
struct param *params;
The value of the address of t-data is stored in params as a
such a pointer.
No variable is created, and params is not of type 'struct param'.
This is a dangerous trick which will be portable only in special cases.
In particular,
1) struct param may have internal padding that differs between
implementations (even between different releases of the same
implementations), so any member beyond the first may be misaligned.
2) If the first member of struct param has alignment requirements more
stringent that that of char *, the contents of t_data may be
misaligned for it.
So, to my mind the values previously written to t_data are overwritten so there is no point to the code filling the first 10 entries of the array? Is this correct?
No. Nothing about t_data has been changed or even looked at. Only its
address has been stored as a pointer-to-(struct param) in params.
Martin Ambuhl wrote: Stephen Kershaw wrote: Hi,
The code creates a large array of chars, called t_data. The first 10 spaces are then filled with characters. However, the line: params = (struct param *)&t_data; then, I think (not entirely sure), creates a variable of type param (struct defined elsewhere), starting at the address of the start of t_data?
params should have been declared as a pointer-to-(struct param): struct param *params; The value of the address of t-data is stored in params as a such a pointer.
No variable is created, and params is not of type 'struct param'. This is a dangerous trick which will be portable only in special cases. In particular, 1) struct param may have internal padding that differs between implementations (even between different releases of the same implementations), so any member beyond the first may be misaligned. 2) If the first member of struct param has alignment requirements more stringent that that of char *, the contents of t_data may be misaligned for it.
Can you give an example struct declaration for 2. above?
santosh wrote: Martin Ambuhl wrote:
Stephen Kershaw wrote:
Hi,
The code creates a large array of chars, called t_data. The first 10 spaces are then filled with characters. However, the line: params = (struct param *)&t_data; then, I think (not entirely sure), creates a variable of type param (struct defined elsewhere), starting at the address of the start of t_data?
params should have been declared as a pointer-to-(struct param): struct param *params; The value of the address of t-data is stored in params as a such a pointer.
No variable is created, and params is not of type 'struct param'. This is a dangerous trick which will be portable only in special cases. In particular, 1) struct param may have internal padding that differs between implementations (even between different releases of the same implementations), so any member beyond the first may be misaligned. 2) If the first member of struct param has alignment requirements more stringent that that of char *, the contents of t_data may be misaligned for it.
Can you give an example struct declaration for 2. above?
Many platforms require several or all of int, long int, long long int,
float, double, and long double to start at addresses that are multiples
of 2, 4, 8, or even larger values. Since the char array can begin at
any address, all of these will break. It is easier to find examples
where (2) is a problem than to find ones where it is not. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Michael Hill |
last post by:
I have a general question about how people generally tend to deal with
users data that they enter.
As an example users enter double quotes in a text field surrounding a
specific piece of text...
|
by: Bret Shortall |
last post by:
Using the SENDOBJECT action command in a Macro works fine if you hard
code in their email address.
SENDOBJECT TO bret@emailaddress.com
SUBJECT This is the Subject line...
|
by: Johann Blake |
last post by:
I have an application that runs on a web server that sends data to
clients over a TCP socket. The data comes from a class that has been
serialized. The class exists in a library (a DLL). The...
|
by: Michelle Keys |
last post by:
I am trying to call a print function to print a string from a database
using javascript.
Which is RC_DATA of Varchar2(2500).
This is a javascript is not being used. I have a thing that needs to...
|
by: MisterB |
last post by:
When I compile the following code in Visual Studio, I don't see the tabs on
the web page ... just the words Tab One Tab Two?? Can anyone tell me what's
wrong with my code??
============
<%@ Page...
|
by: Raterus |
last post by:
I've got a good question about object-oriented programming, I hope I can explain it well enough. I'm creating a UserManager, which I'm going to inherit from so different applications can share...
|
by: James Daughtry |
last post by:
char array;
scanf("%19s", &array);
I know this is wrong because it's a type mismatch, where scanf expects
a pointer to char and gets a pointer to an array of 20 char. I know
that question 6.12...
|
by: chump1708 |
last post by:
union u
{
struct st
{
int i : 4;
int j : 4;
int k : 4;
int l; // or int l:4
}st;
int i;
|
by: SM |
last post by:
Hello
I have simple question using simpleXML and PHP.
i have an xml file that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<discography version="0.01">
<CD>...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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,...
| | |