473,379 Members | 1,337 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,379 software developers and data experts.

C structure rest

Hi,

I am hoping to get some help from you. Basically, I have a structure in
my c program and the structure is used to store the values of a table
row by row and does some processing. At the end of one row processing,
I would like to clear the contents of each members of the structure
which has different data typebefore processing the next row. Can this
be done? Please let me know.

struct table_1
{
char col1[4] ;
char col2[7] ;
char col3[7] ;
char col4[4] ;
long dt_dt ;
char temp1[3] ;
char col5_i[2] ;
char col6_i[2] ;
char col7[2] ;
char col5[2] ;
} ;

Thanks a lot..

Dev-

Oct 7 '06 #1
11 2281
de******************@gmail.com wrote:
Hi,

I am hoping to get some help from you. Basically, I have a structure in
my c program and the structure is used to store the values of a table
row by row and does some processing. At the end of one row processing,
I would like to clear the contents of each members of the structure
which has different data typebefore processing the next row. Can this
be done? Please let me know.

struct table_1
{
char col1[4] ;
char col2[7] ;
char col3[7] ;
char col4[4] ;
long dt_dt ;
char temp1[3] ;
char col5_i[2] ;
char col6_i[2] ;
char col7[2] ;
char col5[2] ;
} ;
Sure, you can use:

#include <string>
#include <cstddef>

template < typename CharT, std::size_t N >
void clear ( CharT (&arg) [N] ) {
std::char_traits< CharT >::assign( arg, N, CharT() );
}

and then do

void clear ( table_1 & arg ) {
clear( arg.col1 );
// ...
arg.dt_dt = 0;
// ...
}
Best

Kai-Uwe Bux

Oct 7 '06 #2
Kai-Uwe Bux wrote:
devaraj.takhellambam wrote:
>my c program...
template < typename CharT, std::size_t N >
You are kind'a cruel, Kai-Uwe. Don't tempt devaraj with high-level C++
things when low-level C is the issue.

devaraj, you should post this question to news:comp.lang.c , where they will
probably recommend how to use memset() safely. However, you also asked how
to "clear the contents of each members of the structure which has different
data typebefore processing the next row." That's between you and your
database, so there might not be a generic C solution.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Oct 7 '06 #3
Kai-Uwe Bux wrote:
de******************@gmail.com wrote:
>Hi,

I am hoping to get some help from you. Basically, I have a structure in
my c program and the structure is used to store the values of a table
row by row and does some processing. At the end of one row processing,
I would like to clear the contents of each members of the structure
which has different data typebefore processing the next row. Can this
be done? Please let me know.

struct table_1
{
char col1[4] ;
char col2[7] ;
char col3[7] ;
char col4[4] ;
long dt_dt ;
char temp1[3] ;
char col5_i[2] ;
char col6_i[2] ;
char col7[2] ;
char col5[2] ;
} ;
[snip some incredibly convoluted stuff]

Well, here is another try:

t = table_1();

as in:
struct table_1 {
char col1[4] ;
char col2[7] ;
char col3[7] ;
char col4[4] ;
long dt_dt ;
char temp1[3] ;
char col5_i[2] ;
char col6_i[2] ;
char col7[2] ;
char col5[2] ;
};
#include<iostream>

int main ( void ) {
table_1 t;
t.dt_dt = 1;
t.col1[0] = 'a';
std::cout << t.col1[0] << " " << t.dt_dt << '\n';
t = table_1();
std::cout << t.col1[0] << " " << t.dt_dt << '\n';
}
Best

Kai-Uwe Bux
Oct 7 '06 #4
Phlip wrote:
Kai-Uwe Bux wrote:
>devaraj.takhellambam wrote:
>>my c program...
> template < typename CharT, std::size_t N >

You are kind'a cruel, Kai-Uwe. Don't tempt devaraj with high-level C++
things when low-level C is the issue.
I wasn't trying to be mean -- I wasn't thinking properly (see my
self-reply). However, posting to this news group, any poster should be
prepared to receive C++ suggestions.

devaraj, you should post this question to news:comp.lang.c , where they
will probably recommend how to use memset() safely.
That is exactly how I started: using memset. Then, of course, you look up
the signature and you find this void* and the length parameter and you
think: that should be needed in C++. This is what led me to look up
char_traits, because I had this recollection that there one finds the C++
versions of memset() and its friends. The template then is actually needed
to avoid keeping track of the lengths manually.

However, you also
asked how to "clear the contents of each members of the structure which
has different data typebefore processing the next row." That's between you
and your database, so there might not be a generic C solution.
Oops, I missed out on that issue entirely: only selected fields are to be
cleared. Well, then what about the generic clear() template:

template < typename T >
T & clear ( T & t ) {
t = T();
return ( t );
}

Of course, now one still has to write some non-generic code to figure out
which fields to clear.
Best

Kai-Uwe Bux
Oct 7 '06 #5
Kai-Uwe Bux posted:
t = table_1();
Only thing though is that that doesn't work for arrays. I think the only
good portable way of "clearing" any kind of object is:

#include<new>

template<class T>
void Clear(T &obj)
{
obj.~T();

::new(&obj) T();
}

/* Now some types to test it out on */

#include <string>

struct MyPOD { int i; char b; void *p; };

int main()
{
std::string obj1;
MyPOD obj2;
int obj3;
double obj4[5];
MyPOD obj5[7];
std::string obj6[2];

Clear(obj1); Clear(obj2); Clear(obj3); Clear(obj4);
Clear(obj5); Clear(obj6);
}

At first, I was tempted to do:

memcpy(&obj,
&reinterpret_cast<T const &>(T()),
sizeof(T));

, but then I realised it would be just as handy to destruct and re-
construct the original object in-place (also, we have the problem of not
being allowed to move around objects willy-nilly, as their internal
workings may depend on the object's address).

--

Frederick Gotham
Oct 7 '06 #6
Frederick Gotham wrote:
Kai-Uwe Bux posted:
> t = table_1();

Only thing though is that that doesn't work for arrays. I think the only
good portable way of "clearing" any kind of object is:

#include<new>

template<class T>
void Clear(T &obj)
{
obj.~T();

::new(&obj) T();
}
[snip]

Right, I thought about that later. The version, I added to my library is
this:

template < typename T >
T & reinitialize ( T & t ) {
/*
| The natural idea
|
| t = T();
|
| requires assignability and default constructability.
| The following looks scary, but is a little less demanding:
| it requires just default constructability and destructability.
*/
// destroy:
address_of( t )->~T();
// reconstruct:
new ( ( void*) address_of( t ) ) T ();
return ( t );
}

where address_of used to deal with classes that think they need to tamper
with the built-in address operator. It is effectively stolen from Boost:

template < typename T >
T * address_of (T & t) {
return (
reinterpret_cast<T*>(
& const_cast<char&>(
reinterpret_cast<const volatile char &>( t ) ) ) );
}
Best

Kai-Uwe Bux
Oct 7 '06 #7
Kai-Uwe Bux posted:
template < typename T >
T & reinitialize ( T & t ) {
/*
| The natural idea
|
| t = T();
|
| requires assignability and default constructability.
| The following looks scary, but is a little less demanding:
| it requires just default constructability and destructability.
*/
// destroy:
address_of( t )->~T();

Good one, I hadn't thought of that.

// reconstruct:
new ( ( void*) address_of( t ) ) T ();

Is the void* cast necessary? Could we not simply have:

::new(address_of(t)) T();

--

Frederick Gotham
Oct 7 '06 #8
Frederick Gotham wrote:
Kai-Uwe Bux posted:
> template < typename T >
T & reinitialize ( T & t ) {
/*
| The natural idea
|
| t = T();
|
| requires assignability and default constructability.
| The following looks scary, but is a little less demanding:
| it requires just default constructability and destructability.
*/
// destroy:
address_of( t )->~T();


Good one, I hadn't thought of that.

> // reconstruct:
new ( ( void*) address_of( t ) ) T ();


Is the void* cast necessary? Could we not simply have:

::new(address_of(t)) T();
Good question. What led me to stick in the (void*) cast is the following
remark from the standard about the semantics of

std::allocator<T>::construct()

The standard [20.4.1.1/12] says:

void construct(pointer p, const_reference val);
Returns: new((void *)p) T(val)

I just copied that, hoping the authors of the standard had thought carefully
about how the standard allocator should construct an object. I think, the
(void*) makes sure that overloaded versions of new do not match. It is
quite possible that using ::new does that too.
Best

Kai-Uwe Bux
Oct 7 '06 #9
As my program is in C and not c++, is there any easier meathod to do
that like using memset or something. Please let me know.
Kai-Uwe Bux wrote:
Frederick Gotham wrote:
Kai-Uwe Bux posted:
template < typename T >
T & reinitialize ( T & t ) {
/*
| The natural idea
|
| t = T();
|
| requires assignability and default constructability.
| The following looks scary, but is a little less demanding:
| it requires just default constructability and destructability.
*/
// destroy:
address_of( t )->~T();

Good one, I hadn't thought of that.

// reconstruct:
new ( ( void*) address_of( t ) ) T ();

Is the void* cast necessary? Could we not simply have:

::new(address_of(t)) T();

Good question. What led me to stick in the (void*) cast is the following
remark from the standard about the semantics of

std::allocator<T>::construct()

The standard [20.4.1.1/12] says:

void construct(pointer p, const_reference val);
Returns: new((void *)p) T(val)

I just copied that, hoping the authors of the standard had thought carefully
about how the standard allocator should construct an object. I think, the
(void*) makes sure that overloaded versions of new do not match. It is
quite possible that using ::new does that too.
Best

Kai-Uwe Bux
Oct 7 '06 #10
de******************@gmail.com wrote:
As my program is in C and not c++, is there any easier meathod to do
that like using memset or something. Please let me know.
(a) please don't top post in this group. It is frowned upon and considered
poor form around these parts.

(b) please don't post in this group if you have a C question. The folks at
comp.lang.c will be happy to take your question. Hint: as far as I know,
(a) applies in that forum, as well.

(c) Other than that: memset() is worth a shot. Beware, however, that using
memset requires you to manually pass the length of the string that you want
to clear: that makes it actually less easy than using the C++ template
function. Thus, you probably want to write a few helper functions like so:

clear_field_1 ( table_1 * t_ptr ) {
memset( &(t_ptr->col1), 0, 4 );
}

clear_field_2 ( table_1 * t_ptr ) {
...

(1) warning: untested code. might be totally bogus
(2) in C++, you would use a reference instead of a pointer.
Best

Kai-Uwe Bux
Oct 7 '06 #11
Devaraj posted:
As my program is in C and not c++, is there any easier meathod to do
that like using memset or something. Please let me know.

You won't get much more help here. I left you a reply over on comp.lang.c.

--

Frederick Gotham
Oct 7 '06 #12

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

Similar topics

1
by: googleo | last post by:
Hi, in my application I want to handle and store data in a hierarchic data structure. For example: persons who manage houses; houses have various numbers of floors; floors have various numbers...
1
by: irfi | last post by:
Hi All, I am working on a cars investment tracking database and need some help with structure You have investors, who pass the money to company the money in turn purchase cars, sell, and...
1
by: irfi | last post by:
Hi All, I am working on a cars investment tracking database and need some help with structure You have investors, who pass the money to company the money in turn purchase cars, sell, and...
5
by: Bob Weiner | last post by:
I'm new to C#. What is the best (in terms of programmability and use) data structure to use to maintain an array of information with containing 3 fields of differing types. For instance:...
2
by: Eric Lindsay | last post by:
Most of the web pages whose source I look at have a very elaborate structure (even apart from any massive use of Javascript), often with div in div in div layered up to six deep, lots of classes...
1
by: pmm | last post by:
hi I am repeating my post here plz excuse i am trying out a UDP packet transfer between a windows machine and a linux I created a structure on both sides (ie on linux and on windows) and I sent...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
6
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
7
by: edsunder | last post by:
I'm making a "wrapper" for an unmanaged DLL (written, I'm fairly certain in C). I have a c++ "wrapper" that I'm trying to convert to VB.net. I've got most of the program working, but I've hit a brick...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...

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.