473,657 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A question on C structures

I've a structure:
struct msghdr
{
//some goes here

struct iovec *msg_iov;

//some more ---
}

struct msghdr msg;
The body of struct iovec looks like this:
struct iovec
{
void *iov_base; // to point to a (char *) buffer
int iov_len;
}
I declare a struct iovec array:
struct iovec iov[10];

Then fill it with values after proper memory allocation:

for i: 0 to 9
strcpy(iov[i].iov_base, buf);
iov[i].iov_len = length;

Then i make *msg_iov to point to this array:
msg.msg_iov = iov;

my question is when i try to access iov_base by:
msg.msg_iov[i].iov_base
it works only for i=0, for the rest it shows NULL.

Now the interesting part, when i pass the
address of 'msg' to a display function (display(struct msghdr *msg))
then in that function i'm able to access the buffers without any problem
using,
msg->msg_iov[i].iov_base
Isn't this weird?
Why will a 'access method' make any difference
Any suggestion/clarifications are welcome.
Nov 14 '05 #1
11 1667
nishant a couché sur son écran :
struct msghdr
{
struct iovec *msg_iov;
}

struct msghdr msg;

struct iovec
{
void *iov_base; // to point to a (char *) buffer
int iov_len;
}

my question is when i try to access iov_base by:
msg.msg_iov[i].iov_base
it works only for i=0, for the rest it shows NULL.

Now the interesting part, when i pass the
address of 'msg' to a display function (display(struct msghdr *msg))
then in that function i'm able to access the buffers without any problem
using,
msg->msg_iov[i].iov_base

Isn't this weird?
No, it's not.
Why will a 'access method' make any difference
Any suggestion/clarifications are welcome.


Because you passed the address of the structure (Which is The Good Way)
via a pointer. The access is now 'indirect'. For clarification, I
suggest that you change your definition for

display (struct msghdr *p_msg)
{
<...>
p_msg->msg_iov[i].iov_base
}

or (probably better, at last to me)

display (struct msghdr *this)
{
<...>
this->msg_iov[i].iov_base
}

--
Emmanuel

Nov 14 '05 #2
On 19 Jul 2004 21:54:56 -0700, ni*******@gmail .com (nishant) wrote:

snip disjointed code fragments
my question is when i try to access iov_base by:
msg.msg_iov[i].iov_base
it works only for i=0, for the rest it shows NULL.

Now the interesting part, when i pass the
address of 'msg' to a display function (display(struct msghdr *msg))
then in that function i'm able to access the buffers without any problem
using,
msg->msg_iov[i].iov_base
Isn't this weird?
Why will a 'access method' make any difference
Any suggestion/clarifications are welcome.


You need to post a compilable example that illustrates the behavior
you describe.
<<Remove the del for email>>
Nov 14 '05 #3


nishant wrote:
I've a structure:
struct msghdr
{
//some goes here

struct iovec *msg_iov;

//some more ---
}

struct msghdr msg;
The body of struct iovec looks like this:
struct iovec
{
void *iov_base; // to point to a (char *) buffer
int iov_len;
}
I declare a struct iovec array:
struct iovec iov[10];

Then fill it with values after proper memory allocation:

for i: 0 to 9
strcpy(iov[i].iov_base, buf);
iov[i].iov_len = length;

Then i make *msg_iov to point to this array:
msg.msg_iov = iov;

my question is when i try to access iov_base by:
msg.msg_iov[i].iov_base
it works only for i=0, for the rest it shows NULL.
This doen't seem right. Your syntax, msg.msg_iov[i].iov_base,
looks fine. I am assuming that all 10 of the dynamic allocations
were successful since the strcpy worked in the loop. There must
be something else involved.

See the example
Now the interesting part, when i pass the
address of 'msg' to a display function (display(struct msghdr *msg))
then in that function i'm able to access the buffers without any problem
using,
msg->msg_iov[i].iov_base

Isn't this weird? Your use of the -> operator is correct. I guess your 'weird' comment
is because the . operator is not working but the -> is working
as expected. It appears to me that you are using the operators
corrected.
Why will a 'access method' make any difference
Any suggestion/clarifications are welcome.


The example:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define SZ 10

struct iovec
{
void *iov_base; // to point to a (char *) buffer
size_t iov_len;
};

struct msghdr
{
//some goes here

struct iovec *msg_iov;

//some more ---
};

void FreeMsghdr(stru ct msghdr *msg)
{
int i;

for(i = 0; i < SZ; i++)
{
free(msg->msg_iov[i].iov_base);
msg->msg_iov[i].iov_base = NULL;
msg->msg_iov[i].iov_len = 0;
}
msg->msg_iov = NULL;
return;
}

int main(void)
{
struct msghdr msg;
struct iovec iov[SZ];
int i;
char buf[32];

for(i = 0; i < SZ;i++)
{
iov[i].iov_base = malloc(32);
if(iov[i].iov_base)
{
sprintf(buf,"He lloWorld_%d",i) ;
strcpy(iov[i].iov_base,buf);
iov[i].iov_len = strlen(buf);
}
}
msg.msg_iov = iov;
for(i = 0;i < SZ;i++)
printf("msg.msg _iov[%d].iov_base = \"%s\"\n",i,
msg.msg_iov[i].iov_base?msg.m sg_iov[i].iov_base:"NULL ");
FreeMsghdr(&msg );
puts("\nAfter Calling FreeMsghdr:");
for(i = 0;i < SZ;i++)
printf("iov[%d].iov_base = %s\t\t"
"iov[%d].iov_len = %u\n",i,
iov[i].iov_base?iov[i].iov_base:"NULL ",
i,iov[i].iov_len);
return 0;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #4
In the function void FreeMsghdr(stru ct msghdr *msg), isn't it enough
by calling free()? Why would we still need

msg->msg_iov[i].iov_base = NULL;
msg->msg_iov[i].iov_len = 0;

and

msg->msg_iov = NULL;

I remember I saw this before but have no idea why we need them.
Al Bowers <xa******@rapid sys.com> wrote in message news:<2m******* *****@uni-berlin.de>...
nishant wrote:
I've a structure:
struct msghdr

[snip]

The example:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define SZ 10

struct iovec
{
void *iov_base; // to point to a (char *) buffer
size_t iov_len;
};

struct msghdr
{
//some goes here

struct iovec *msg_iov;

//some more ---
};

void FreeMsghdr(stru ct msghdr *msg)
{
int i;

for(i = 0; i < SZ; i++)
{
free(msg->msg_iov[i].iov_base);
msg->msg_iov[i].iov_base = NULL;
msg->msg_iov[i].iov_len = 0;
}
msg->msg_iov = NULL;
return;
}

int main(void)
{
struct msghdr msg;
struct iovec iov[SZ];
int i;
char buf[32];

for(i = 0; i < SZ;i++)
{
iov[i].iov_base = malloc(32);
if(iov[i].iov_base)
{
sprintf(buf,"He lloWorld_%d",i) ;
strcpy(iov[i].iov_base,buf);
iov[i].iov_len = strlen(buf);
}
}
msg.msg_iov = iov;
for(i = 0;i < SZ;i++)
printf("msg.msg _iov[%d].iov_base = \"%s\"\n",i,
msg.msg_iov[i].iov_base?msg.m sg_iov[i].iov_base:"NULL ");
FreeMsghdr(&msg );
puts("\nAfter Calling FreeMsghdr:");
for(i = 0;i < SZ;i++)
printf("iov[%d].iov_base = %s\t\t"
"iov[%d].iov_len = %u\n",i,
iov[i].iov_base?iov[i].iov_base:"NULL ",
i,iov[i].iov_len);
return 0;
}

Nov 14 '05 #5
salut Emmanuel
i'm sorry if i made u couché (i hope my french is correct :-) )
Thank you for replying. but the question still remains ---

Emmanuel Delahaye <em***@YOURBRAn oos.fr> wrote in message news:<mn******* *************** *@YOURBRAnoos.f r>...
nishant a couché sur son écran :
-----------
Now the interesting part, when i pass the
address of 'msg' to a display function (display(struct msghdr *msg))
then in that function i'm able to access the buffers without any problem
using,
msg->msg_iov[i].iov_base

Isn't this weird?
No, it's not.
Why will a 'access method' make any difference
Any suggestion/clarifications are welcome.


Because you passed the address of the structure (Which is The Good Way)
via a pointer. The access is now 'indirect'. For clarification, I
suggest that you change your definition for


i will restate my question,
A method whether 'direct' or 'indirect' shudn't make any difference
when accessing a value, right?
if i can access it thru a pointer then why not by the variable itself
??

display (struct msghdr *p_msg)
{
<...>
p_msg->msg_iov[i].iov_base
}

or (probably better, at last to me)

display (struct msghdr *this)
{
<...>
this->msg_iov[i].iov_base
}

Nov 14 '05 #6
Hello Al
Thank you for replying.
But The question still remains
Now the interesting part, when i pass the
address of 'msg' to a display function (display(struct msghdr *msg))
then in that function i'm able to access the buffers without any problem
using,
msg->msg_iov[i].iov_base

Isn't this weird?

Your use of the -> operator is correct. I guess your 'weird' comment
is because the . operator is not working but the -> is working
as expected.


You are Right, Any explanantions for that ?
I even tried cleaning up the memory first before assigning anything as
you suggested. But even that doesn't work.
Nov 14 '05 #7


nishant wrote:
Hello Al
Thank you for replying.
But The question still remains

Now the interesting part, when i pass the
address of 'msg' to a display function (display(struct msghdr *msg))
then in that function i'm able to access the buffers without any problem
using,
msg->msg_iov[i].iov_base

Isn't this weird?


Your use of the -> operator is correct. I guess your 'weird' comment
is because the . operator is not working but the -> is working
as expected.

You are Right, Any explanantions for that ?
I even tried cleaning up the memory first before assigning anything as
you suggested. But even that doesn't work.


From the info you have provided, I cannot give an explanation.

You have:
1. Provided the declarations and assignments for the structs.
They seem fine.
2. Shown that you have an understanding of the . operator and the
-> operation and the their usage.

Yet! The . operator is not working and the -> is working.

This leaves me to believe that there is a suble error in the code
that you have yet to uncover.

I would suggest that you simply the code, still showing 'weird'
behavior, to a form that would allow you to post it.
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #8


Xingbo G wrote:
In the function void FreeMsghdr(stru ct msghdr *msg), isn't it enough
by calling free()? Why would we still need

msg->msg_iov[i].iov_base = NULL;
msg->msg_iov[i].iov_len = 0;

and

msg->msg_iov = NULL;


It is not needed. It is not neccessary, especially in the trival
example. It is more like a 'religion'.

In the design of a function that frees dynamic memory in objects,
some feel that it best not leave dangling pointers that could
lead to problems in a complex, large code project. So, the pointers
and other members of the struct are given a value that can asseverate
the condition of the object elsewhere in the code.
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #9
Hi Al,
I include the code below, if this is what u want.
The program makes use of socket calls to accept a connection and then
recieve some data. The 'weird' part is with the the " /*### "
comments.
I hope this time the 'weird'ness is clear to you.
#include <stdio.h>
#include <------->

#define PORT 6000
#define BUFNUM 3
#define BUFLEN 20
#define BACKLOG 15

/* int recvmsg(int, struct msghdr, int)
Description: It is used to receive an array of buffers (from the
socket interface specified by int s)
and copy it to struct msghdr *mesghdr */
int recvmsg(int s, struct msghdr *mesghdr, int flags)
{
char *buf;
int i,j, bufsize, fromlen;
struct sockaddr_in from_addr;
struct iovec iov[BUFNUM];

fromlen = sizeof(struct sockaddr_in);
buf = (char *)malloc(BUFLEN +1);
bufsize = BUFLEN+1;

for(j=0; j<BUFNUM; j++)
{
iov[j].iov_base = (char *)malloc(BUFLEN );
iov[j].iov_len = BUFLEN;
}

for(j=0; j<BUFNUM; j++) // keep recieving BUFNUM times
{
i = recvfrom(s,buf, bufsize,flags,& from_addr,&from len); // a
socket call
strcpy(iov[j].iov_base, buf);
iov[j].iov_len = bufsize;
}

mesghdr->msg_iov = iov;

mesghdr->msg_name = (void *)&from_addr;
mesghdr->msg_namelen = fromlen;
mesghdr->msg_iovlen = BUFNUM;

return (i);

}

void display(struct msghdr *msg) // takes the address of msghdr
{
int i;
char recvdata[BUFNUM][BUFLEN];
for(i=0; i<BUFNUM && strcmp(msg->msg_iov[i].iov_base,"") != 0;
i++)
{
strcpy(recvdata[i], msg->msg_iov[i].iov_base); /* ###
'->'works fine */
printf("\nMessa ge Received %d: %s\n", i+1,
recvdata[i]);
}
}

void main()
{
int new_fd, sockfd, i;
int sockin_size;
char recvdata[BUFNUM][BUFLEN];

struct msghdr mesghdr;
struct sockaddr_in local_addr, remote_addr;

sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == -1)
{
printf("Sockfd error\n");
exit(0);
}

local_addr.sin_ family = AF_INET;
local_addr.sin_ port = htons(PORT);
local_addr.sin_ addr.s_addr = INADDR_ANY;
/*local_addr.sin _addr.s_addr = inet_addr("172. 31.41.17");
*/

memset(&(local_ addr.sin_zero), '\0', 8); /* Required */

if( bind(sockfd, (struct sockaddr *)&local_addr, sizeof(struct
sockaddr)) == -1)
{
printf("BIND ERROR\n");
exit(0);
}

if( listen(sockfd, BACKLOG) == -1)
{
printf("LISTEN ERROR\n");
exit(0);
}

sockin_size = sizeof(struct sockaddr_in);
printf("\n before accept \n");
new_fd = accept(sockfd, (struct sockaddr *) &remote_addr ,
&sockin_size );
printf("\n after accept \n");

printf("server: got connection from
%s\n",inet_ntoa (remote_addr.si n_addr));
memset(mesghdr, 0, sizeof (struct msghdr));
mesghdr.msg_nam e = (void *)malloc(sizeof (struct sockaddr));
mesghdr.msg_nam elen = sizeof(struct sockaddr);
mesghdr.msg_iov len = BUFNUM;

if(( i = recvmsg(new_fd, (struct msghdr *)&mesghdr,0)) == -1 )
{
perror("recvmsg error: ");
printf("Error No: %d\n", errno);
exit(0);
}

/* for(i=0; i<BUFNUM && strcmp(mesghdr. msg_iov[i].iov_base," ")
!= 0; i++)
{
strcpy(recvdata[i], mesghdr.msg_iov[i].iov_base); /*### '.'doesn't
work */
printf("\nMessa ge Received %d: %s\n", i+1, recvdata[i]);
} */

display((struct msghdr *)&mesghdr); // included this since the above
doesn't work

printf("\n");

close(sockfd);
}

*************** ****** structure body *************** ************
struct msghdr
{
//some goes here

struct iovec *msg_iov;

//some more ---
}msg;

struct iovec
{
void *iov_base; // to point to a (char *) buffer
int iov_len;
}iov[10];
Nov 14 '05 #10

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

Similar topics

1
2725
by: kazack | last post by:
Hi all it's me again with another question as I got further in my book. The chapter I am in covers structres, abstract data and classes. I only read through to the end of the coverage on structures. Trying to comprehend this is harder than I thought it was going to be. I should of just skipped this chapter and went right into pointers since they seem to be easier to use. But anyways here i smy question: you define a structure...
33
3856
by: Peter Seaman | last post by:
I understand that structures are value types and arrays and classes are reference types. But what about arrays as members of structures i.e. as in C struct x { int n; int a; }
6
4481
by: Ken Allen | last post by:
OK, I admit that I have been programming since before C++ was invented, and I have developed more than my share of assembly language systems, and even contributed to operating system and compiler systems over the years. I have developed code in more than 30 distinct programming languages for a wide cariety of industries. But this issue of structures in C# is beginning to annoy me. I should also make it clear that I am not a big supporter...
7
6055
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
14
15078
by: pmclinn | last post by:
I've noticed that many programmers use classes to store data about such things like: Class Customers .....Phone ....ID ....Address End Class....
2
2404
by: thomasfarrow | last post by:
At work, our development team has a development standards document that insists Structures should never be used. I'm looking to change this standard but need a suitable argument in order to make the change. I know that Structures are value types, sit on the stack, and are generally more efficient to manipulate than reference types (i.e. Classes). Structures cannot use inheritance, the finalize method or default constructors. Can anyone...
11
3759
by: efrat | last post by:
Hello, I'm planning to use Python in order to teach a DSA (data structures and algorithms) course in an academic institute. If you could help out with the following questions, I'd sure appreciate it: 1. What exactly is a Python list? If one writes a, then is the complexity Theta(n)? If this is O(1), then why was the name "list" chosen? If this is indeed Theta(n), then what alternative should be used? (array does not seem suited for...
44
5774
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
4
4715
by: cleanrabbit | last post by:
Hello! I hate having to do this, because im almost certain there is someone in the world that has come across this problem and i just havent found their solution yet, so i do appologise if this has already been covered. I have been trying to learn c# as fast as possible in the last month and in doing so i have been re-visiting old C/C++ problems and trying to overcome them in C#. What i have is a very complex serise of structures. Id...
8
1485
by: Bob Altman | last post by:
Hi all, I have a structure that includes a constructor. I want to add a bunch of these structures to an STL map (whose index is an int). If I define the map like this: map<int, MyStruct*myMap; then I can add pointers to newly allocated structures like this:
0
8421
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8325
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8844
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6177
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.