473,396 Members | 2,009 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.

Understand piece of code.....

Could anyone please help me understand what is going on in the code below. A
short explanation would be nice.....

Best Regards
Terry

struct mystruct {
unsigned char testchar;
unsigned short testshort;
unsigned char anothertestchar;
};

void testfunc(unsigned char* mymemlocation) {

struct mystruct *thedata = (struct mystruct*) mymemlocation;

thedata->testchar = 0x55;
thedata->testshort = 0xFFFF;
thedata->anothertestchar = 0x11;

}
Nov 13 '05 #1
13 2934
"Terry Andersen" <te**@sea.com> wrote in message
news:bg**********@news.net.uni-c.dk...
struct mystruct {
unsigned char testchar;
unsigned short testshort;
unsigned char anothertestchar;
}; You have created a structure, which is something like a container for
arbitrary types and related variables.
void testfunc(unsigned char* mymemlocation) {

struct mystruct *thedata = (struct mystruct*) mymemlocation; This is a function that passes a pointer for a memory location to store your
struct. This is _nasty code_. Never assume that unsigned char* is equal to
anything else but unsigned char. The parameter should be void*
mymemlocation. Most likely, this function is requiring that you supply your
own allocated pointer for manipulation of the structure on the heap.
thedata->testchar = 0x55; Pointers use -> or (*struct). to access members if they are pointers.
Otherwise they use a period '.'. This line here sets testchar to the letter
'U', which is 0x55 in hexadecimal. 0x before a value indicates that it is
hexadecimal. You could have also made it equal to 85.
thedata->testshort = 0xFFFF; This sets testshort, the unsigned short int from the structure, to 65535.
thedata->anothertestchar = 0x11; This sets the other character to 17, which is an unprintable (not
alpha-numeric) digit.
}

There should be a semicolon here.
Nov 13 '05 #2
"Greg P." <no@spam.sam> wrote in message
news:oc*****************@newsread3.news.pas.earthl ink.net...
"Terry Andersen" <te**@sea.com> wrote in message
news:bg**********@news.net.uni-c.dk...
struct mystruct {
unsigned char testchar;
unsigned short testshort;
unsigned char anothertestchar;
}; You have created a structure, which is something like a container for
arbitrary types and related variables.
void testfunc(unsigned char* mymemlocation) {

struct mystruct *thedata = (struct mystruct*) mymemlocation;

This is a function that passes a pointer for a memory location to store

your struct. This is _nasty code_. Never assume that unsigned char* is equal to
anything else but unsigned char. The parameter should be void*
mymemlocation. Most likely, this function is requiring that you supply your own allocated pointer for manipulation of the structure on the heap.
thedata->testchar = 0x55; Pointers use -> or (*struct). to access members if they are pointers.
Otherwise they use a period '.'. This line here sets testchar to the

letter 'U', which is 0x55 in hexadecimal. 0x before a value indicates that it is
hexadecimal. You could have also made it equal to 85.
thedata->testshort = 0xFFFF;

This sets testshort, the unsigned short int from the structure, to 65535.
thedata->anothertestchar = 0x11;

This sets the other character to 17, which is an unprintable (not
alpha-numeric) digit.
}

There should be a semicolon here.


Thanks a lot. Would this mean that testchar, testshort and anothertestchar
are stored from memory location mymemlocation and further? If mymemlocation
would be like:

unsigned char TheBuffer[100];
//and you called the testfunc with:

testfunc(TheBuffer[50]);

/*
Would TheBuffer then look like:
TheBuffer[50] = 0x55
TheBuffer[51] = 0xFF
TheBuffer[52] = 0xFF
TheBuffer[53] = 0x11
???
*/

Best Regards
Terry
Nov 13 '05 #3
"Terry Andersen" <te**@sea.com> wrote in message
news:bg*********@news.net.uni-c.dk...
Thanks a lot. Would this mean that testchar, testshort and anothertestchar
are stored from memory location mymemlocation and further? If mymemlocation would be like:

unsigned char TheBuffer[100];
//and you called the testfunc with:

testfunc(TheBuffer[50]); No. First of all you would not pass TheBuffer to testfunc() with the
subscript on it. It would be passed as

testfunc(TheBuffer);

instead. The name of an array is a pointer to the first value, which you
could later iterate over.
Would TheBuffer then look like:
TheBuffer[50] = 0x55
TheBuffer[51] = 0xFF
TheBuffer[52] = 0xFF
TheBuffer[53] = 0x11

No. I think you mean: would the buffer[index] hold the values from the
function as array indexes. No.

With this code, you are now overwriting the boundary of your array, which
ends at 49. Remember that arrays start from 0, and go to length-1. So if I
had an array like:

char array[45];

I can only access array[0] to array[44].

If you access anything outside of your array's range, it is undetermined
behavior, but most likely you will overwrite another program's (running on
your machine) data causing a crash or something nastier.

To get back to your question about whether the members from the struct are
part of the array, no. You are attempting to pass an unsigned char to your
function as a form of memory reservation. Instead use:

struct mystruct p_yourstruct* = (struct mystruct) malloc(sizeof(mystruct));

to get the memory. For example:
---------------------------------------------
struct mystruct
{
unsigned char testchar;
unsigned short testshort;
unsigned char anothertestchar;
};

struct mystruct* mem_location = (struct mystruct) malloc(sizeof(mystruct));

void testfunc(struct mystruct* mymemlocation)
{
thedata->testchar = 0x55;
thedata->testshort = 0xFFFF;
thedata->anothertestchar = 0x11;
};
-----------------------------------------------------------------------
malloc() allocates memory for your struct. You need to call
free(mem_location) to free up the memory before your program terminates, or
else you will get memory leak.

To use the code you would pass:

testfunc(mem_location);

and then you could goof off with the variables.

Try not to mix apples and oranges. The only thing you should pass to hold a
struct is another struct of the same type. It would be like passing a car
variable to a function that uses the car to hold a mansion.

Any more questions? =)
Nov 13 '05 #4
"Greg P." <no@spam.sam> wrote in message
news:RN****************@newsread3.news.pas.earthli nk.net...
"Terry Andersen" <te**@sea.com> wrote in message
news:bg*********@news.net.uni-c.dk...
Thanks a lot. Would this mean that testchar, testshort and anothertestchar are stored from memory location mymemlocation and further? If mymemlocation
would be like:

unsigned char TheBuffer[100];
//and you called the testfunc with:

testfunc(TheBuffer[50]);

No. First of all you would not pass TheBuffer to testfunc() with the
subscript on it. It would be passed as

testfunc(TheBuffer);

instead. The name of an array is a pointer to the first value, which you
could later iterate over.
Would TheBuffer then look like:
TheBuffer[50] = 0x55
TheBuffer[51] = 0xFF
TheBuffer[52] = 0xFF
TheBuffer[53] = 0x11

No. I think you mean: would the buffer[index] hold the values from the
function as array indexes. No.

With this code, you are now overwriting the boundary of your array, which
ends at 49. Remember that arrays start from 0, and go to length-1. So if I
had an array like:

char array[45];

I can only access array[0] to array[44].

If you access anything outside of your array's range, it is undetermined
behavior, but most likely you will overwrite another program's (running on
your machine) data causing a crash or something nastier.

To get back to your question about whether the members from the struct are
part of the array, no. You are attempting to pass an unsigned char to your
function as a form of memory reservation. Instead use:

struct mystruct p_yourstruct* = (struct mystruct)

malloc(sizeof(mystruct));
to get the memory. For example:
---------------------------------------------
struct mystruct
{
unsigned char testchar;
unsigned short testshort;
unsigned char anothertestchar;
};

struct mystruct* mem_location = (struct mystruct) malloc(sizeof(mystruct));
void testfunc(struct mystruct* mymemlocation)
{
thedata->testchar = 0x55;
thedata->testshort = 0xFFFF;
thedata->anothertestchar = 0x11;
};
-----------------------------------------------------------------------
malloc() allocates memory for your struct. You need to call
free(mem_location) to free up the memory before your program terminates, or else you will get memory leak.

To use the code you would pass:

testfunc(mem_location);

and then you could goof off with the variables.

Try not to mix apples and oranges. The only thing you should pass to hold a struct is another struct of the same type. It would be like passing a car
variable to a function that uses the car to hold a mansion.

Any more questions? =)


Seems like I have much to learn when programming C.... :-)
What I would like my code to do is what I described, namely I would like to
"update" part of a global array with the struct data.......what if I called
testfunc like: testfunc(TheBuffer+50); would'nt I get the desired result
then? I Mean:

TheBuffer[50] = 0x55
TheBuffer[51] = 0xFF
TheBuffer[52] = 0xFF
TheBuffer[53] = 0x11

Or is this a silly way of doing this.....?

Best Regards
Terry
Nov 13 '05 #5
"Terry Andersen" <te**@sea.com> wrote in message
news:bg**********@news.net.uni-c.dk...
Seems like I have much to learn when programming C.... :-)
What I would like my code to do is what I described, namely I would like to "update" part of a global array with the struct data.......what if I called testfunc like: testfunc(TheBuffer+50); would'nt I get the desired result
then? I Mean:

TheBuffer[50] = 0x55
TheBuffer[51] = 0xFF
TheBuffer[52] = 0xFF
TheBuffer[53] = 0x11

Or is this a silly way of doing this.....?

Silly =)

If you want to update a global array with the values, you would have to call
your function testfunc() first, properly, then do something like.

TheBuffer[50] = your_struct->testchar;
TheBuffer[51] = your_struct->anothertextchar;

Do not put the unsigned short into the buffer, unless you are sure of its
value being within range of a byte, which is not likely. You will truncate
(round off) the value if you try to cast it as a char.

Also, you keep trying to pass an array of chars to testfunc(), which
manipulates a structure. No no no <slaps hand>. struct->testchar, testshort,
and anothertestchar have nothing to do with TheBuffer. You are assuming that
the compiler puts the value of testchar into TheBuffer[50], the value of
testshort into TheBuffer[51], and the value of anothertestchar into
TheBuffer[52]. This is not true. The compiler puts the values into the
structure, which you can then put into the array.

What books are you reading on C?
--

Regards,
Greg P.

Golden Rule of Open Source Programming:
"Don't whine about something unless you plan to implement it yourself"
Nov 13 '05 #6
I don't think I am being clear enough. What you need to do, then, is remove
the mystruct struct and just work on the array.

void testfunc(unsigned char array[])
{
array[50] = something;
array[51] = something;
array[52] = something;
};
Nov 13 '05 #7
In <bg**********@news.net.uni-c.dk> "Terry Andersen" <te**@sea.com> writes:
Could anyone please help me understand what is going on in the code below. A
short explanation would be nice.....

struct mystruct {
unsigned char testchar;
unsigned short testshort;
unsigned char anothertestchar;
};

void testfunc(unsigned char* mymemlocation) {
struct mystruct *thedata = (struct mystruct*) mymemlocation;

thedata->testchar = 0x55;
thedata->testshort = 0xFFFF;
thedata->anothertestchar = 0x11;
}


If you do not understand this code, the right thing to do is to (re)read
the chapters dealing with structures and pointers in your favourite C
book. This is very basic C code and you have a big problem if you don't
understand it.

The proper type for mymemlocation in standard C is void pointer, but this
is a minor detail.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #8
"Dan Pop" <Da*****@cern.ch> wrote in message
news:bg**********@sunnews.cern.ch...
In <bg**********@news.net.uni-c.dk> "Terry Andersen" <te**@sea.com> writes:
Could anyone please help me understand what is going on in the code below. Ashort explanation would be nice.....

struct mystruct {
unsigned char testchar;
unsigned short testshort;
unsigned char anothertestchar;
};

void testfunc(unsigned char* mymemlocation) {
struct mystruct *thedata = (struct mystruct*) mymemlocation;

thedata->testchar = 0x55;
thedata->testshort = 0xFFFF;
thedata->anothertestchar = 0x11;
}


If you do not understand this code, the right thing to do is to (re)read
the chapters dealing with structures and pointers in your favourite C
book. This is very basic C code and you have a big problem if you don't
understand it.

The proper type for mymemlocation in standard C is void pointer, but this
is a minor detail.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de


You are both quite right. Back to the books. Thank you very much for your
time (especially Greg) and effort.
Nov 13 '05 #9
"Terry Andersen" <te**@sea.com> wrote:

[ Bloody 'ell, what a lot of quoting. Out come the scissors... ]
"Greg P." <no@spam.sam> wrote in message
news:RN****************@newsread3.news.pas.earthli nk.net...
"Terry Andersen" <te**@sea.com> wrote in message
news:bg*********@news.net.uni-c.dk...
void testfunc(struct mystruct* mymemlocation)
{
thedata->testchar = 0x55;
thedata->testshort = 0xFFFF;
thedata->anothertestchar = 0x11;
};
What I would like my code to do is what I described, namely I would like to
"update" part of a global array with the struct data.......what if I called
testfunc like: testfunc(TheBuffer+50); would'nt I get the desired result
then? I Mean:

TheBuffer[50] = 0x55
TheBuffer[51] = 0xFF
TheBuffer[52] = 0xFF
TheBuffer[53] = 0x11
Not necessarily. There may well be holes in the struct, for example for
alignment purposes. E.g., it could well result in this:

TheBuffer[50] == 0x55
TheBuffer[51] == 0xA2 /* Whatever was there before. */
TheBuffer[52] == 0xFF
TheBuffer[53] == 0xFF
TheBuffer[54] == 0x11

or

TheBuffer[50] == 0x55
TheBuffer[51] == 0x55 /* Which happened to be in the register. */
TheBuffer[52] == 0xFF
TheBuffer[53] == 0xFF
TheBuffer[54] == 0x11

Moreover, if you change

thedata->testshort = 0xFFFF;

to

thedata->testshort = 0x1234;

you don't know whether it will end up as

TheBuffer[52] == 0x12
TheBuffer[53] == 0x34

TheBuffer[52] == 0x34
TheBuffer[53] == 0x12

unless you happen to know the endianness of your computer. And then you
have to consider that shorts need not be two bytes long... or, indeed,
that bytes may be 16 or 32 bits long.

And as a final thought, this could break depending on what you pass to
the function, because of alignment errors. For example, suppose that
shorts need to be aligned on even addresses. If you, your struct will
indeed have a hole in it, as above. Calling testfunc(TheBuffer+50) will
work, because the short will, as it should, end up on an even address.
Now consider where that short will be positioned if you call
testfunc(TheBuffer+51)...
Or is this a silly way of doing this.....?


Not necessarily, but it's a very system-specific one, and details may
depend on, e.g., the optimisation flags you used to compile your
program. IOW, it's risky, and it probably isn't quite as useful as you
think it is.

Richard
Nov 13 '05 #10
bd
On Tue, 29 Jul 2003 08:25:56 +0000, Greg P. wrote:
"Terry Andersen" <te**@sea.com> wrote in message
news:bg**********@news.net.uni-c.dk...
struct mystruct {
unsigned char testchar;
unsigned short testshort;
unsigned char anothertestchar;
}; You have created a structure, which is something like a container for
arbitrary types and related variables.
void testfunc(unsigned char* mymemlocation) {

[snip]
}

There should be a semicolon here.


You don't need a semicolon after a function body.

--
Freenet distribution not available
A holding company is a thing where you hand an accomplice the goods while
the policeman searches you.

Nov 13 '05 #11
bd
On Tue, 29 Jul 2003 11:42:49 +0200, Terry Andersen wrote:
"Greg P." <no@spam.sam> wrote in message
news:RN****************@newsread3.news.pas.earthli nk.net...
"Terry Andersen" <te**@sea.com> wrote in message
news:bg*********@news.net.uni-c.dk...
> Thanks a lot. Would this mean that testchar, testshort and anothertestchar > are stored from memory location mymemlocation and further? If

mymemlocation
> would be like:
>
> unsigned char TheBuffer[100];
> //and you called the testfunc with:
>
> testfunc(TheBuffer[50]);

No. First of all you would not pass TheBuffer to testfunc() with the
subscript on it. It would be passed as

testfunc(TheBuffer);

instead. The name of an array is a pointer to the first value, which you
could later iterate over.
> Would TheBuffer then look like:
> TheBuffer[50] = 0x55
> TheBuffer[51] = 0xFF
> TheBuffer[52] = 0xFF
> TheBuffer[53] = 0x11

No. I think you mean: would the buffer[index] hold the values from the
function as array indexes. No.

With this code, you are now overwriting the boundary of your array, which
ends at 49. Remember that arrays start from 0, and go to length-1. So if I
had an array like:

char array[45];

I can only access array[0] to array[44].

If you access anything outside of your array's range, it is undetermined
behavior, but most likely you will overwrite another program's (running on
your machine) data causing a crash or something nastier.

To get back to your question about whether the members from the struct are
part of the array, no. You are attempting to pass an unsigned char to your
function as a form of memory reservation. Instead use:

struct mystruct p_yourstruct* = (struct mystruct)

malloc(sizeof(mystruct));

to get the memory. For example:
---------------------------------------------
struct mystruct
{
unsigned char testchar;
unsigned short testshort;
unsigned char anothertestchar;
};

struct mystruct* mem_location = (struct mystruct)

malloc(sizeof(mystruct));

void testfunc(struct mystruct* mymemlocation)
{
thedata->testchar = 0x55;
thedata->testshort = 0xFFFF;
thedata->anothertestchar = 0x11;
};
-----------------------------------------------------------------------
malloc() allocates memory for your struct. You need to call
free(mem_location) to free up the memory before your program terminates,

or
else you will get memory leak.

To use the code you would pass:

testfunc(mem_location);

and then you could goof off with the variables.

Try not to mix apples and oranges. The only thing you should pass to hold

a
struct is another struct of the same type. It would be like passing a car
variable to a function that uses the car to hold a mansion.

Any more questions? =)


Seems like I have much to learn when programming C.... :-)
What I would like my code to do is what I described, namely I would like to
"update" part of a global array with the struct data.......what if I called
testfunc like: testfunc(TheBuffer+50); would'nt I get the desired result
then? I Mean:

TheBuffer[50] = 0x55
TheBuffer[51] = 0xFF
TheBuffer[52] = 0xFF
TheBuffer[53] = 0x11

Or is this a silly way of doing this.....?


A struct may have padding - it could have any number of spaces in between
valid members that have no defined value (that is, could contain
anything).
--
Freenet distribution not available
My face is new, my license is expired, and I'm under a doctor's care!!!!

Nov 13 '05 #12

"Terry Andersen" <te**@sea.com> wrote in message
news:bg*********@news.net.uni-c.dk...
"Greg P." <no@spam.sam> wrote in message
news:oc*****************@newsread3.news.pas.earthl ink.net...
"Terry Andersen" <te**@sea.com> wrote in message
news:bg**********@news.net.uni-c.dk...
struct mystruct {
unsigned char testchar;
unsigned short testshort;
unsigned char anothertestchar;
}; You have created a structure, which is something like a container for
arbitrary types and related variables.


(big snip)
Thanks a lot. Would this mean that testchar, testshort and anothertestchar
are stored from memory location mymemlocation and further? If mymemlocation would be like:

unsigned char TheBuffer[100];
file://and you called the testfunc with:

testfunc(TheBuffer[50]);

/*
Would TheBuffer then look like:
TheBuffer[50] = 0x55
TheBuffer[51] = 0xFF
TheBuffer[52] = 0xFF
TheBuffer[53] = 0x11
???
*/


Well, it could, but on many machines that I know about it would be more like

TheBuffer[50] = 0x55;
TheBuffer[52] = 0xFF;
TheBuffer[53] = 0xFF;
TheBuffer[54] = 0x11;

as many machines require a short to have an even address.

Also, the two bytes of the short may be stored in different order on
different machines.

-- glen


Nov 13 '05 #13
"bd" <bd*****@bd-home-comp.no-ip.org> wrote in message
news:pa****************************@bd-home-comp.no-ip.org...
You don't need a semicolon after a function body.


Right! I was tired, sorry terry.
Nov 13 '05 #14

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

Similar topics

1
by: Vincent Jones | last post by:
I have this piece of script and I'm trying to understand what the 'SET t.Z_ASSOC_ROW = (t.rowid - m.MinRowID) + 1' and the 'INNER JOIN' line is doing UPDATE t SET t.Z_ASSOC_ROW = (t.rowid -...
10
by: forgotten field | last post by:
Hi,how are you? I have been studying C++ by myself, but recently I am having a real problem. I am learning about the basic usage of a doubly linked list using polymorphism. However, I have got the...
9
by: TCMA | last post by:
I am looking for some tools to help me understand source code of a program written in C++ by someone else. Are there any non-commercial, open source C or C++ tools to reverse engineer C or C++...
51
by: Richard Hengeveld | last post by:
Hi all, I'm trying to understand how pointers for function parameters work. As I understand it, if you got a function like: void f(int *i) { *i = 0; }
2
by: DC | last post by:
Hi, I need to asynchronously read from a network (TCP) stream, and I am having trouble with retrieving whole blocks; I get a break in the data block every 1460 bytes which relates to network...
6
by: Alan Silver | last post by:
Hello, I'm just taking my first steps at doing layout with CSS, and I'm having a few problems. This could be because I don't really understand what I'm doing yet!! I would really appreciate any...
2
by: badboybrown | last post by:
Hello folks, I found this piece of code on this group by Dorman Blackman: > Function BusinessDays(dDate1, dDate2) As Long > BusinessDays = (DateDiff("d", dDate1, dDate2) - _ > ...
1
by: asp coding | last post by:
what does following piece of code do?? With rdsDC .Server = Srv .Connect =Cnstr .ExecuteOptions = adcExecSync End With
7
by: kigoobe | last post by:
Hi guys I was trying to understand how the ffmpeg works, but even after a lot of googling, couldn't advance a bit. Ok, in case of normal file uploading, we have something like - ...
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
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
Oralloy
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,...
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
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
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.