Search Results

Search found 2566 results on 103 pages for 'struct'.

Page 19/103 | < Previous Page | 15 16 17 18 19 20 21 22 23 24 25 26  | Next Page >

  • C sockets, chat server and client, problem echoing back.

    - by wretrOvian
    Hi This is my chat server : #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <string.h> #define LISTEN_Q 20 #define MSG_SIZE 1024 struct userlist { int sockfd; struct sockaddr addr; struct userlist *next; }; int main(int argc, char *argv[]) { // declare. int listFD, newFD, fdmax, i, j, bytesrecvd; char msg[MSG_SIZE], ipv4[INET_ADDRSTRLEN]; struct addrinfo hints, *srvrAI; struct sockaddr_storage newAddr; struct userlist *users, *uptr, *utemp; socklen_t newAddrLen; fd_set master_set, read_set; // clear sets FD_ZERO(&master_set); FD_ZERO(&read_set); // create a user list users = (struct userlist *)malloc(sizeof(struct userlist)); users->sockfd = -1; //users->addr = NULL; users->next = NULL; // clear hints memset(&hints, 0, sizeof hints); // prep hints hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; // get srver info if(getaddrinfo("localhost", argv[1], &hints, &srvrAI) != 0) { perror("* ERROR | getaddrinfo()\n"); exit(1); } // get a socket if((listFD = socket(srvrAI->ai_family, srvrAI->ai_socktype, srvrAI->ai_protocol)) == -1) { perror("* ERROR | socket()\n"); exit(1); } // bind socket bind(listFD, srvrAI->ai_addr, srvrAI->ai_addrlen); // listen on socket if(listen(listFD, LISTEN_Q) == -1) { perror("* ERROR | listen()\n"); exit(1); } // add listfd to master_set FD_SET(listFD, &master_set); // initialize fdmax fdmax = listFD; while(1) { // equate read_set = master_set; // run select if(select(fdmax+1, &read_set, NULL, NULL, NULL) == -1) { perror("* ERROR | select()\n"); exit(1); } // query all sockets for(i = 0; i <= fdmax; i++) { if(FD_ISSET(i, &read_set)) { // found active sockfd if(i == listFD) { // new connection // accept newAddrLen = sizeof newAddr; if((newFD = accept(listFD, (struct sockaddr *)&newAddr, &newAddrLen)) == -1) { perror("* ERROR | select()\n"); exit(1); } // resolve ip if(inet_ntop(AF_INET, &(((struct sockaddr_in *)&newAddr)->sin_addr), ipv4, INET_ADDRSTRLEN) == -1) { perror("* ERROR | inet_ntop()"); exit(1); } fprintf(stdout, "* Client Connected | %s\n", ipv4); // add to master list FD_SET(newFD, &master_set); // create new userlist component utemp = (struct userlist*)malloc(sizeof(struct userlist)); utemp->next = NULL; utemp->sockfd = newFD; utemp->addr = *((struct sockaddr *)&newAddr); // iterate to last node for(uptr = users; uptr->next != NULL; uptr = uptr->next) { } // add uptr->next = utemp; // update fdmax if(newFD > fdmax) fdmax = newFD; } else { // existing sockfd transmitting data // read if((bytesrecvd = recv(i, msg, MSG_SIZE, 0)) == -1) { perror("* ERROR | recv()\n"); exit(1); } msg[bytesrecvd] = '\0'; // find out who sent? for(uptr = users; uptr->next != NULL; uptr = uptr->next) { if(i == uptr->sockfd) break; } // resolve ip if(inet_ntop(AF_INET, &(((struct sockaddr_in *)&(uptr->addr))->sin_addr), ipv4, INET_ADDRSTRLEN) == -1) { perror("* ERROR | inet_ntop()"); exit(1); } // print fprintf(stdout, "%s\n", msg); // send to all for(j = 0; j <= fdmax; j++) { if(FD_ISSET(j, &master_set)) { if(send(j, msg, strlen(msg), 0) == -1) perror("* ERROR | send()"); } } } // handle read from client } // end select result handle } // end looping fds } // end while return 0; } This is my client: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <string.h> #define MSG_SIZE 1024 int main(int argc, char *argv[]) { // declare. int newFD, bytesrecvd, fdmax; char msg[MSG_SIZE]; fd_set master_set, read_set; struct addrinfo hints, *srvrAI; // clear sets FD_ZERO(&master_set); FD_ZERO(&read_set); // clear hints memset(&hints, 0, sizeof hints); // prep hints hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; // get srver info if(getaddrinfo(argv[1], argv[2], &hints, &srvrAI) != 0) { perror("* ERROR | getaddrinfo()\n"); exit(1); } // get a socket if((newFD = socket(srvrAI->ai_family, srvrAI->ai_socktype, srvrAI->ai_protocol)) == -1) { perror("* ERROR | socket()\n"); exit(1); } // connect to server if(connect(newFD, srvrAI->ai_addr, srvrAI->ai_addrlen) == -1) { perror("* ERROR | connect()\n"); exit(1); } // add to master, and add keyboard FD_SET(newFD, &master_set); FD_SET(STDIN_FILENO, &master_set); // initialize fdmax if(newFD > STDIN_FILENO) fdmax = newFD; else fdmax = STDIN_FILENO; while(1) { // equate read_set = master_set; if(select(fdmax+1, &read_set, NULL, NULL, NULL) == -1) { perror("* ERROR | select()"); exit(1); } // check server if(FD_ISSET(newFD, &read_set)) { // read data if((bytesrecvd = recv(newFD, msg, MSG_SIZE, 0)) < 0 ) { perror("* ERROR | recv()"); exit(1); } msg[bytesrecvd] = '\0'; // print fprintf(stdout, "%s\n", msg); } // check keyboard if(FD_ISSET(STDIN_FILENO, &read_set)) { // read data from stdin if((bytesrecvd = read(STDIN_FILENO, msg, MSG_SIZE)) < 0) { perror("* ERROR | read()"); exit(1); } msg[bytesrecvd] = '\0'; // send if((send(newFD, msg, bytesrecvd, 0)) == -1) { perror("* ERROR | send()"); exit(1); } } } return 0; } The problem is with the part where the server recv()s data from an FD, then tries echoing back to all [send() ]; it just dies, w/o errors, and my client is left looping :(

    Read the article

  • C - How to use both aio_read() and aio_write().

    - by Slav
    I implement game server where I need to both read and write. So I accept incoming connection and start reading from it using aio_read() but when I need to send something, I stop reading using aio_cancel() and then use aio_write(). Within write's callback I resume reading. So, I do read all the time but when I need to send something - I pause reading. It works for ~20% of time - in other case call to aio_cancel() fails with "Operation now in progress" - and I cannot cancel it (even within permanent while cycle). So, my added write operation never happens. How to use these functions well? What did I missed? EDIT: Used under Linux 2.6.35. Ubuntu 10 - 32 bit. Example code: void handle_read(union sigval sigev_value) { /* handle data or disconnection */ } void handle_write(union sigval sigev_value) { /* free writing buffer memory */ } void start() { const int acceptorSocket = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in addr; memset(&addr, 0, sizeof(struct sockaddr_in)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = htons(port); bind(acceptorSocket, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)); listen(acceptorSocket, SOMAXCONN); struct sockaddr_in address; socklen_t addressLen = sizeof(struct sockaddr_in); for(;;) { const int incomingSocket = accept(acceptorSocket, (struct sockaddr*)&address, &addressLen); if(incomingSocket == -1) { /* handle error ... */} else { //say socket to append outcoming messages at writing: const int currentFlags = fcntl(incomingSocket, F_GETFL, 0); if(currentFlags < 0) { /* handle error ... */ } if(fcntl(incomingSocket, F_SETFL, currentFlags | O_APPEND) == -1) { /* handle another error ... */ } //start reading: struct aiocb* readingAiocb = new struct aiocb; memset(readingAiocb, 0, sizeof(struct aiocb)); readingAiocb->aio_nbytes = MY_SOME_BUFFER_SIZE; readingAiocb->aio_fildes = socketDesc; readingAiocb->aio_buf = mySomeReadBuffer; readingAiocb->aio_sigevent.sigev_notify = SIGEV_THREAD; readingAiocb->aio_sigevent.sigev_value.sival_ptr = (void*)mySomeData; readingAiocb->aio_sigevent.sigev_notify_function = handle_read; if(aio_read(readingAiocb) != 0) { /* handle error ... */ } } } } //called at any time from server side: send(void* data, const size_t dataLength) { //... some thread-safety precautions not needed here ... const int cancellingResult = aio_cancel(socketDesc, readingAiocb); if(cancellingResult != AIO_CANCELED) { //this one happens ~80% of the time - embracing previous call to permanent while cycle does not help: if(cancellingResult == AIO_NOTCANCELED) { puts(strerror(aio_return(readingAiocb))); // "Operation now in progress" /* don't know what to do... */ } } //otherwise it's okay to send: else { aio_write(...); } }

    Read the article

  • Implications of trying to double free memory space in C

    - by SidNoob
    Here' my piece of code: #include <stdio.h> #include<stdlib.h> struct student{ char *name; }; int main() { struct student s; s.name = malloc(sizeof(char *)); // I hope this is the right way... printf("Name: "); scanf("%[^\n]", s.name); printf("You Entered: \n\n"); printf("%s\n", s.name); free(s.name); // This will cause my code to break } All I know is that dynamic allocation on the 'heap' needs to be freed. My question is, when I run the program, sometimes the code runs successfully. i.e. ./struct Name: Thisis Myname You Entered: Thisis Myname I tried reading this I've concluded that I'm trying to double-free a piece of memory i.e. I'm trying to free a piece of memory that is already free? (hope I'm correct here. If Yes, what could be the Security Implications of a double-free?) While it fails sometimes as its supposed to: ./struct Name: CrazyFishMotorhead Rider You Entered: CrazyFishMotorhead Rider *** glibc detected *** ./struct: free(): invalid next size (fast): 0x08adb008 *** ======= Backtrace: ========= /lib/tls/i686/cmov/libc.so.6(+0x6b161)[0xb7612161] /lib/tls/i686/cmov/libc.so.6(+0x6c9b8)[0xb76139b8] /lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0xb7616a9d] ./struct[0x8048533] /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xb75bdbd6] ./struct[0x8048441] ======= Memory map: ======== 08048000-08049000 r-xp 00000000 08:01 288098 /root/struct 08049000-0804a000 r--p 00000000 08:01 288098 /root/struct 0804a000-0804b000 rw-p 00001000 08:01 288098 /root/struct 08adb000-08afc000 rw-p 00000000 00:00 0 [heap] b7400000-b7421000 rw-p 00000000 00:00 0 b7421000-b7500000 ---p 00000000 00:00 0 b7575000-b7592000 r-xp 00000000 08:01 788956 /lib/libgcc_s.so.1 b7592000-b7593000 r--p 0001c000 08:01 788956 /lib/libgcc_s.so.1 b7593000-b7594000 rw-p 0001d000 08:01 788956 /lib/libgcc_s.so.1 b75a6000-b75a7000 rw-p 00000000 00:00 0 b75a7000-b76fa000 r-xp 00000000 08:01 920678 /lib/tls/i686/cmov/libc-2.11.1.so b76fa000-b76fc000 r--p 00153000 08:01 920678 /lib/tls/i686/cmov/libc-2.11.1.so b76fc000-b76fd000 rw-p 00155000 08:01 920678 /lib/tls/i686/cmov/libc-2.11.1.so b76fd000-b7700000 rw-p 00000000 00:00 0 b7710000-b7714000 rw-p 00000000 00:00 0 b7714000-b7715000 r-xp 00000000 00:00 0 [vdso] b7715000-b7730000 r-xp 00000000 08:01 788898 /lib/ld-2.11.1.so b7730000-b7731000 r--p 0001a000 08:01 788898 /lib/ld-2.11.1.so b7731000-b7732000 rw-p 0001b000 08:01 788898 /lib/ld-2.11.1.so bffd5000-bfff6000 rw-p 00000000 00:00 0 [stack] Aborted So why is it that my code does work sometimes? i.e. the compiler is not able to detect at times that I'm trying to free an already freed memory. Has it got to do something with my stack/heap size?

    Read the article

  • hash table with chaining method program freezing

    - by Justin Carrey
    I am implementing hash table in C using linked list chaining method. The program compiles but when inserting a string in hash table, the program freezes and gets stuck. The program is below: struct llist{ char *s; struct llist *next; }; struct llist *a[100]; void hinsert(char *str){ int strint, hashinp; strint = 0; hashinp = 0; while(*str){ strint = strint+(*str); } hashinp = (strint%100); if(a[hashinp] == NULL){ struct llist *node; node = (struct llist *)malloc(sizeof(struct llist)); node->s = str; node->next = NULL; a[hashinp] = node; } else{ struct llist *node, *ptr; node = (struct llist *)malloc(sizeof(struct llist)); node->s = str; node->next = NULL; ptr = a[hashinp]; while(ptr->next != NULL){ ptr = ptr->next; } ptr->next = node; } } void hsearch(char *strsrch){ int strint1, hashinp1; strint1 = 0; hashinp1 = 0; while(*strsrch){ strint1 = strint1+(*strsrch); } hashinp1 = (strint1%100); struct llist *ptr1; ptr1 = a[hashinp1]; while(ptr1 != NULL){ if(ptr1->s == strsrch){ cout << "Element Found\n"; break; } else{ ptr1 = ptr1->next; } } if(ptr1 == NULL){ cout << "Element Not Found\n"; } } hinsert() is to insert elements into hash and hsearch is to search an element in the hash. Hash function is written inside hinsert() itself. In the main(), what i am initializing all the elements in a[] to be NULL like this: for(int i = 0;i < 100; i++){ a[i] = NULL; } Help is very much appreciated. Thanks !

    Read the article

  • C++/boost generator module, feedback/critic please

    - by aaa
    hello. I wrote this generator, and I think to submit to boost people. Can you give me some feedback about it it basically allows to collapse multidimensional loops to flat multi-index queue. Loop can be boost lambda expressions. Main reason for doing this is to make parallel loops easier and separate algorithm from controlling structure (my fieldwork is computational chemistry where deep loops are common) 1 #ifndef _GENERATOR_HPP_ 2 #define _GENERATOR_HPP_ 3 4 #include <boost/array.hpp> 5 #include <boost/lambda/lambda.hpp> 6 #include <boost/noncopyable.hpp> 7 8 #include <boost/mpl/bool.hpp> 9 #include <boost/mpl/int.hpp> 10 #include <boost/mpl/for_each.hpp> 11 #include <boost/mpl/range_c.hpp> 12 #include <boost/mpl/vector.hpp> 13 #include <boost/mpl/transform.hpp> 14 #include <boost/mpl/erase.hpp> 15 16 #include <boost/fusion/include/vector.hpp> 17 #include <boost/fusion/include/for_each.hpp> 18 #include <boost/fusion/include/at_c.hpp> 19 #include <boost/fusion/mpl.hpp> 20 #include <boost/fusion/include/as_vector.hpp> 21 22 #include <memory> 23 24 /** 25 for loop generator which can use lambda expressions. 26 27 For example: 28 @code 29 using namespace generator; 30 using namespace boost::lambda; 31 make_for(N, N, range(bind(std::max<int>, _1, _2), N), range(_2, _3+1)); 32 // equivalent to pseudocode 33 // for l=0,N: for k=0,N: for j=max(l,k),N: for i=k,j 34 @endcode 35 36 If range is given as upper bound only, 37 lower bound is assumed to be default constructed 38 Lambda placeholders may only reference first three indices. 39 */ 40 41 namespace generator { 42 namespace detail { 43 44 using boost::lambda::constant_type; 45 using boost::lambda::constant; 46 47 /// lambda expression identity 48 template<class E, class enable = void> 49 struct lambda { 50 typedef E type; 51 }; 52 53 /// transform/construct constant lambda expression from non-lambda 54 template<class E> 55 struct lambda<E, typename boost::disable_if< 56 boost::lambda::is_lambda_functor<E> >::type> 57 { 58 struct constant : boost::lambda::constant_type<E>::type { 59 typedef typename boost::lambda::constant_type<E>::type base_type; 60 constant() : base_type(boost::lambda::constant(E())) {} 61 constant(const E &e) : base_type(boost::lambda::constant(e)) {} 62 }; 63 typedef constant type; 64 }; 65 66 /// range functor 67 template<class L, class U> 68 struct range_ { 69 typedef boost::array<int,4> index_type; 70 range_(U upper) : bounds_(typename lambda<L>::type(), upper) {} 71 range_(L lower, U upper) : bounds_(lower, upper) {} 72 73 template< typename T, size_t N> 74 T lower(const boost::array<T,N> &index) { 75 return bound<0>(index); 76 } 77 78 template< typename T, size_t N> 79 T upper(const boost::array<T,N> &index) { 80 return bound<1>(index); 81 } 82 83 private: 84 template<bool b, typename T> 85 T bound(const boost::array<T,1> &index) { 86 return (boost::fusion::at_c<b>(bounds_))(index[0]); 87 } 88 89 template<bool b, typename T> 90 T bound(const boost::array<T,2> &index) { 91 return (boost::fusion::at_c<b>(bounds_))(index[0], index[1]); 92 } 93 94 template<bool b, typename T, size_t N> 95 T bound(const boost::array<T,N> &index) { 96 using boost::fusion::at_c; 97 return (at_c<b>(bounds_))(index[0], index[1], index[2]); 98 } 99 100 boost::fusion::vector<typename lambda<L>::type, 101 typename lambda<U>::type> bounds_; 102 }; 103 104 template<typename T, size_t N> 105 struct for_base { 106 typedef boost::array<T,N> value_type; 107 virtual ~for_base() {} 108 virtual value_type next() = 0; 109 }; 110 111 /// N-index generator 112 template<typename T, size_t N, class R, class I> 113 struct for_ : for_base<T,N> { 114 typedef typename for_base<T,N>::value_type value_type; 115 typedef R range_tuple; 116 for_(const range_tuple &r) : r_(r), state_(true) { 117 boost::fusion::for_each(r_, initialize(index)); 118 } 119 /// @return new generator 120 for_* new_() { return new for_(r_); } 121 /// @return next index value and increment 122 value_type next() { 123 value_type next; 124 using namespace boost::lambda; 125 typename value_type::iterator n = next.begin(); 126 typename value_type::iterator i = index.begin(); 127 boost::mpl::for_each<I>(*(var(n))++ = var(i)[_1]); 128 129 state_ = advance<N>(r_, index); 130 return next; 131 } 132 /// @return false if out of bounds, true otherwise 133 operator bool() { return state_; } 134 135 private: 136 /// initialize indices 137 struct initialize { 138 value_type &index_; 139 mutable size_t i_; 140 initialize(value_type &index) : index_(index), i_(0) {} 141 template<class R_> void operator()(R_& r) const { 142 index_[i_++] = r.lower(index_); 143 } 144 }; 145 146 /// advance index[0:M) 147 template<size_t M> 148 struct advance { 149 /// stop recursion 150 struct stop { 151 stop(R r, value_type &index) {} 152 }; 153 /// advance index 154 /// @param r range tuple 155 /// @param index index array 156 advance(R &r, value_type &index) : index_(index), i_(0) { 157 namespace fusion = boost::fusion; 158 index[M-1] += 1; // increment index 159 fusion::for_each(r, *this); // update indices 160 state_ = index[M-1] >= fusion::at_c<M-1>(r).upper(index); 161 if (state_) { // out of bounds 162 typename boost::mpl::if_c<(M > 1), 163 advance<M-1>, stop>::type(r, index); 164 } 165 } 166 /// apply lower bound of range to index 167 template<typename R_> void operator()(R_& r) const { 168 if (i_ >= M) index_[i_] = r.lower(index_); 169 ++i_; 170 } 171 /// @return false if out of bounds, true otherwise 172 operator bool() { return state_; } 173 private: 174 value_type &index_; ///< index array reference 175 mutable size_t i_; ///< running index 176 bool state_; ///< out of bounds state 177 }; 178 179 value_type index; 180 range_tuple r_; 181 bool state_; 182 }; 183 184 185 /// polymorphic generator template base 186 template<typename T,size_t N> 187 struct For : boost::noncopyable { 188 typedef boost::array<T,N> value_type; 189 /// @return next index value and increment 190 value_type next() { return for_->next(); } 191 /// @return false if out of bounds, true otherwise 192 operator bool() const { return for_; } 193 protected: 194 /// reset smart pointer 195 void reset(for_base<T,N> *f) { for_.reset(f); } 196 std::auto_ptr<for_base<T,N> > for_; 197 }; 198 199 /// range [T,R) type 200 template<typename T, typename R> 201 struct range_type { 202 typedef range_<T,R> type; 203 }; 204 205 /// range identity specialization 206 template<typename T, class L, class U> 207 struct range_type<T, range_<L,U> > { 208 typedef range_<L,U> type; 209 }; 210 211 namespace fusion = boost::fusion; 212 namespace mpl = boost::mpl; 213 214 template<typename T, size_t N, class R1, class R2, class R3, class R4> 215 struct range_tuple { 216 // full range vector 217 typedef typename mpl::vector<R1,R2,R3,R4> v; 218 typedef typename mpl::end<v>::type end; 219 typedef typename mpl::advance_c<typename mpl::begin<v>::type, N>::type pos; 220 // [0:N) range vector 221 typedef typename mpl::erase<v, pos, end>::type t; 222 // transform into proper range fusion::vector 223 typedef typename fusion::result_of::as_vector< 224 typename mpl::transform<t,range_type<T, mpl::_1> >::type 225 >::type type; 226 }; 227 228 229 template<typename T, size_t N, 230 class R1, class R2, class R3, class R4, 231 class O> 232 struct for_type { 233 typedef typename range_tuple<T,N,R1,R2,R3,R4>::type range_tuple; 234 typedef for_<T, N, range_tuple, O> type; 235 }; 236 237 } // namespace detail 238 239 240 /// default index order, [0:N) 241 template<size_t N> 242 struct order { 243 typedef boost::mpl::range_c<size_t,0, N> type; 244 }; 245 246 /// N-loop generator, 0 < N <= 5 247 /// @tparam T index type 248 /// @tparam N number of indices/loops 249 /// @tparam R1,... range types 250 /// @tparam O index order 251 template<typename T, size_t N, 252 class R1, class R2 = void, class R3 = void, class R4 = void, 253 class O = typename order<N>::type> 254 struct for_ : detail::for_type<T, N, R1, R2, R3, R4, O>::type { 255 typedef typename detail::for_type<T, N, R1, R2, R3, R4, O>::type base_type; 256 typedef typename base_type::range_tuple range_tuple; 257 for_(const range_tuple &range) : base_type(range) {} 258 }; 259 260 /// loop range [L:U) 261 /// @tparam L lower bound type 262 /// @tparam U upper bound type 263 /// @return range 264 template<class L, class U> 265 detail::range_<L,U> range(L lower, U upper) { 266 return detail::range_<L,U>(lower, upper); 267 } 268 269 /// make 4-loop generator with specified index ordering 270 template<typename T, class R1, class R2, class R3, class R4, class O> 271 for_<T, 4, R1, R2, R3, R4, O> 272 make_for(R1 r1, R2 r2, R3 r3, R4 r4, const O&) { 273 typedef for_<T, 4, R1, R2, R3, R4, O> F; 274 return F(F::range_tuple(r1, r2, r3, r4)); 275 } 276 277 /// polymorphic generator template forward declaration 278 template<typename T,size_t N> 279 struct For; 280 281 /// polymorphic 4-loop generator 282 template<typename T> 283 struct For<T,4> : detail::For<T,4> { 284 /// generator with default index ordering 285 template<class R1, class R2, class R3, class R4> 286 For(R1 r1, R2 r2, R3 r3, R4 r4) { 287 this->reset(make_for<T>(r1, r2, r3, r4).new_()); 288 } 289 /// generator with specified index ordering 290 template<class R1, class R2, class R3, class R4, class O> 291 For(R1 r1, R2 r2, R3 r3, R4 r4, O o) { 292 this->reset(make_for<T>(r1, r2, r3, r4, o).new_()); 293 } 294 }; 295 296 } 297 298 299 #endif /* _GENERATOR_HPP_ */

    Read the article

  • No audio with headphones, but audio works with integrated speakers

    - by Pedro
    My speakers work correctly, but when I plug in my headphones, they don't work. I am running Ubuntu 10.04. My audio card is Realtek ALC259 My laptop model is a HP G62t a10em In another thread someone fixed a similar issue (headphones work, speakers not) folowing this: sudo vi /etc/modprobe.d/alsa-base.conf (or some other editor instead of Vi) Append the following at the end of the file: alias snd-card-0 snd-hda-intel options snd-hda-intel model=auto Reboot but it doesnt work for me. Before making and changes to alsa, this was the output: alsamixer gives me this: Things I did: followed this HowTo but now no hardware seems to be present (before, there were 2 items listed): Now, alsamixer gives me this: alsamixer: relocation error: alsamixer: symbol snd_mixer_get_hctl, version ALSA_0.9 not defined in file libasound.so.2 with link time reference I guess there was and error in the alsa-driver install so I began reinstalling it. cd alsa-driver* //this works fine// sudo ./configure --with-cards=hda-intel --with-kernel=/usr/src/linux-headers-$(uname -r) //this works fine// sudo make //this doesn't work. see ouput error below// sudo make install Final lines of sudo make: hpetimer.c: In function ‘snd_hpet_open’: hpetimer.c:41: warning: implicit declaration of function ‘hpet_register’ hpetimer.c:44: warning: implicit declaration of function ‘hpet_control’ hpetimer.c:44: error: expected expression before ‘unsigned’ hpetimer.c: In function ‘snd_hpet_close’: hpetimer.c:51: warning: implicit declaration of function ‘hpet_unregister’ hpetimer.c:52: error: invalid use of undefined type ‘struct hpet_task’ hpetimer.c: In function ‘hpetimer_init’: hpetimer.c:88: error: ‘EINVAL’ undeclared (first use in this function) hpetimer.c:99: error: invalid use of undefined type ‘struct hpet_task’ hpetimer.c:100: error: invalid use of undefined type ‘struct hpet_task’ hpetimer.c: At top level: hpetimer.c:121: warning: excess elements in struct initializer hpetimer.c:121: warning: (near initialization for ‘__param_frequency’) hpetimer.c:121: warning: excess elements in struct initializer hpetimer.c:121: warning: (near initialization for ‘__param_frequency’) hpetimer.c:121: warning: excess elements in struct initializer hpetimer.c:121: warning: (near initialization for ‘__param_frequency’) hpetimer.c:121: warning: excess elements in struct initializer hpetimer.c:121: warning: (near initialization for ‘__param_frequency’) hpetimer.c:121: error: extra brace group at end of initializer hpetimer.c:121: error: (near initialization for ‘__param_frequency’) hpetimer.c:121: warning: excess elements in struct initializer hpetimer.c:121: warning: (near initialization for ‘__param_frequency’) make[1]: *** [hpetimer.o] Error 1 make[1]: Leaving directory `/usr/src/alsa/alsa-driver-1.0.9/acore' make: *** [compile] Error 1 And then sudo make install gives me: rm -f /lib/modules/0.0.0/misc/snd*.*o /lib/modules/0.0.0/misc/persist.o /lib/modules/0.0.0/misc/isapnp.o make[1]: Entering directory `/usr/src/alsa/alsa-driver-1.0.9/acore' mkdir -p /lib/modules/0.0.0/misc cp snd-hpet.o snd-page-alloc.o snd-pcm.o snd-timer.o snd.o /lib/modules/0.0.0/misc cp: cannot stat `snd-hpet.o': No such file or directory cp: cannot stat `snd-page-alloc.o': No such file or directory cp: cannot stat `snd-pcm.o': No such file or directory cp: cannot stat `snd-timer.o': No such file or directory cp: cannot stat `snd.o': No such file or directory make[1]: *** [_modinst__] Error 1 make[1]: Leaving directory `/usr/src/alsa/alsa-driver-1.0.9/acore' make: *** [install-modules] Error 1 [SOLUTION] After screwing it all up, someone mentioned why not trying using the packages in Synaptic - so I did. I have reinstalled the following packages and rebooter: -alsa-hda-realtek-ignore-sku-dkms -alsa-modules-2.6.32-25-generic -alsa-source -alsa-utils -linux-backports-modules-alsa-lucid-generic -linux-backports-modules-alsa-lucid-generic-pae -linux-sound-base -(i think i listed them all) After rebooting, the audio worked, both in speakers and headphones. I have no idea which is the package that made my audio work, but it certainly was one of them. [/SOLUTION]

    Read the article

  • Errors when trying to compile the driver for the rtl8192su wireless adapter

    - by Tom Brito
    I have a wireless to usb adapter, and I'm having some trouble to install the drivers on Ubuntu. First of all, the readme says to use the make command, and I already got errors: $ make make[1]: Entering directory `/usr/src/linux-headers-2.6.35-22-generic' CC [M] /home/wellington/Desktop/rtl8192su_linux_2.4_2.6.0003.0301.2010/HAL/rtl8192u/r8192U_core.o /home/wellington/Desktop/rtl8192su_linux_2.4_2.6.0003.0301.2010/HAL/rtl8192u/r8192U_core.c: In function ‘rtl8192_usb_probe’: /home/wellington/Desktop/rtl8192su_linux_2.4_2.6.0003.0301.2010/HAL/rtl8192u/r8192U_core.c:12325: error: ‘struct net_device’ has no member named ‘open’ /home/wellington/Desktop/rtl8192su_linux_2.4_2.6.0003.0301.2010/HAL/rtl8192u/r8192U_core.c:12326: error: ‘struct net_device’ has no member named ‘stop’ /home/wellington/Desktop/rtl8192su_linux_2.4_2.6.0003.0301.2010/HAL/rtl8192u/r8192U_core.c:12327: error: ‘struct net_device’ has no member named ‘tx_timeout’ /home/wellington/Desktop/rtl8192su_linux_2.4_2.6.0003.0301.2010/HAL/rtl8192u/r8192U_core.c:12328: error: ‘struct net_device’ has no member named ‘do_ioctl’ /home/wellington/Desktop/rtl8192su_linux_2.4_2.6.0003.0301.2010/HAL/rtl8192u/r8192U_core.c:12329: error: ‘struct net_device’ has no member named ‘set_multicast_list’ /home/wellington/Desktop/rtl8192su_linux_2.4_2.6.0003.0301.2010/HAL/rtl8192u/r8192U_core.c:12330: error: ‘struct net_device’ has no member named ‘set_mac_address’ /home/wellington/Desktop/rtl8192su_linux_2.4_2.6.0003.0301.2010/HAL/rtl8192u/r8192U_core.c:12331: error: ‘struct net_device’ has no member named ‘get_stats’ /home/wellington/Desktop/rtl8192su_linux_2.4_2.6.0003.0301.2010/HAL/rtl8192u/r8192U_core.c:12332: error: ‘struct net_device’ has no member named ‘hard_start_xmit’ make[2]: *** [/home/wellington/Desktop/rtl8192su_linux_2.4_2.6.0003.0301.2010/HAL/rtl8192u/r8192U_core.o] Error 1 make[1]: *** [_module_/home/wellington/Desktop/rtl8192su_linux_2.4_2.6.0003.0301.2010/HAL/rtl8192u] Error 2 make[1]: Leaving directory `/usr/src/linux-headers-2.6.35-22-generic' make: *** [all] Error 2 /home/wellington/Desktop/rtl8192su_linux_2.4_2.6.0003.0301.2010/ is the path where I copied the drivers on my computer. Any idea how to solve this? (I don't even know what the error is...) update: sudo lshw -class network *-network description: Ethernet interface product: RTL8111/8168B PCI Express Gigabit Ethernet controller vendor: Realtek Semiconductor Co., Ltd. physical id: 0 bus info: pci@0000:01:00.0 logical name: eth0 version: 03 serial: 78:e3:b5:e7:5f:6e size: 10MB/s capacity: 1GB/s width: 64 bits clock: 33MHz capabilities: pm msi pciexpress msix vpd bus_master cap_list rom ethernet physical tp mii 10bt 10bt-fd 100bt 100bt-fd 1000bt 1000bt-fd autonegotiation configuration: autonegotiation=on broadcast=yes driver=r8169 driverversion=2.3LK-NAPI duplex=half latency=0 link=no multicast=yes port=MII speed=10MB/s resources: irq:42 ioport:d800(size=256) memory:fbeff000-fbefffff memory:faffc000-faffffff memory:fbec0000-fbedffff *-network DISABLED description: Wireless interface physical id: 2 logical name: wlan0 serial: 00:26:18:a1:ae:64 capabilities: ethernet physical wireless configuration: broadcast=yes multicast=yes wireless=802.11b/g sudo lspci 00:00.0 Host bridge: Intel Corporation Core Processor DRAM Controller (rev 18) 00:02.0 VGA compatible controller: Intel Corporation Core Processor Integrated Graphics Controller (rev 18) 00:16.0 Communication controller: Intel Corporation 5 Series/3400 Series Chipset HECI Controller (rev 06) 00:1a.0 USB Controller: Intel Corporation 5 Series/3400 Series Chipset USB2 Enhanced Host Controller (rev 06) 00:1b.0 Audio device: Intel Corporation 5 Series/3400 Series Chipset High Definition Audio (rev 06) 00:1c.0 PCI bridge: Intel Corporation 5 Series/3400 Series Chipset PCI Express Root Port 1 (rev 06) 00:1c.2 PCI bridge: Intel Corporation 5 Series/3400 Series Chipset PCI Express Root Port 3 (rev 06) 00:1d.0 USB Controller: Intel Corporation 5 Series/3400 Series Chipset USB2 Enhanced Host Controller (rev 06) 00:1e.0 PCI bridge: Intel Corporation 82801 PCI Bridge (rev a6) 00:1f.0 ISA bridge: Intel Corporation 5 Series Chipset LPC Interface Controller (rev 06) 00:1f.2 SATA controller: Intel Corporation 5 Series/3400 Series Chipset 6 port SATA AHCI Controller (rev 06) 00:1f.3 SMBus: Intel Corporation 5 Series/3400 Series Chipset SMBus Controller (rev 06) 01:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168B PCI Express Gigabit Ethernet controller (rev 03) 02:00.0 FireWire (IEEE 1394): VIA Technologies, Inc. VT6315 Series Firewire Controller (rev 01) sudo lsusb Bus 002 Device 003: ID 0bda:0158 Realtek Semiconductor Corp. USB 2.0 multicard reader Bus 002 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 001 Device 004: ID 045e:00f9 Microsoft Corp. Wireless Desktop Receiver 3.1 Bus 001 Device 003: ID 0b05:1786 ASUSTek Computer, Inc. Bus 001 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

    Read the article

  • How do you make an in-place construction of a struct casted to array compile in Visual C++ 2008?

    - by Irwin1138
    I'm working with quite a big codebase which compiles fine in linux but vc++ 2008 spits errors. The problem code goes like this: Declaration: typedef float vec_t; typedef vec_t vec2_t[2]; The codebase is littered with in-place construction like this one: (vec2_t){0, divs} Or more complex: (vec2_t){ 1/(float)Vid_GetScreenW(), 1/(float)Vid_GetScreenH()} As far as I know, this code constructs a struct, then converts it to an array and passes the address to the function. I personally never used in-place construction like this so I have no clue how to make this one work. I don't maintain the linux build, only the windows one. And I can't get it to compile. Is there some switch, some macro to make vc++ compile it? Maybe there is a similar nifty way to construct those arrays and pass them to the functions in-place that compiles just fine in vc++?

    Read the article

  • Asp .Net MVC Viewmodel should be class or struct?

    - by Jonas Everest
    Hey guys, I have just been thinking about the concept of view model object we create in asp.net MVC. Our purpose is to instantiate it and pass it from controller to view and view read it and display the data. Those view model are usually instantiated through constructor. We won't need to initialize the members, we may not need to redefine/override parameterless constructor and we don't need inheritance feature there. So, why don't we use struct type for our view model instead of class. It will enhance the performance.

    Read the article

  • C errors - Cannot combine with previous 'struct' declaration specifier && Redefinition of 'MyMIDINotifyProc' as different kind of symbol

    - by user1905634
    I'm still new to C but trying to understand it better by working my way through a small MIDI audio unit (in Xcode 4.3.3). I've been searching for an answer to this all day and still don't even understand exactly what the problem is. Here's the code in question: //MyMIDINotifyProc.h #ifndef MIDIInstrumentUnit_CallbackProcs_h #define MIDIInstrumentUnit_CallbackProcs_h void MyMIDINotifyProc (const MIDINotification *message, void *refCon); #endif //MyMIDINotifyProc.c #include <CoreMIDI/CoreMIDI.h> #include "MyMIDINotifyProc.h" void MyMIDINotifyProc (const MIDINotification *message, void *refCon) { //manage notification } In the header definition I get this: ! Cannot combine with previous 'struct' declaration specifier I've made sure the definitions match and tried renaming them and I still get this in my .c file: ! Redefinition of 'MyMIDINotifyProc' as different kind of symbol Which points to the .h definition as the 'Previous definition'. I know that MIDIServices.h in the CoreMIDI framework defines: typedef void (*MIDINotifyProc)(const MIDINotification *message, void *refCon); But I don't understand if/why that would cause an error. I would be grateful if anyone could offer some help.

    Read the article

  • How do I pass a struct (or class) message back and forth between a C# service and a separate VB 6 ap

    - by grantjumper
    I need to pass data between a c# service and a running vb 6 application, considering using Windows Messaging. How do I pass the data back and forth between a C# service and a running vb 6 application? Shortened sample of the data I am passing back and forth is below: namespace MainSection { public struct Info { private int _Id; private string _TypeCode; private float _Calc; private DateTime _ProcessDate; private bool _ProcessFlag; public int Id { get { return _Id; } set { _Id = value; } } public string TypeCode { get { return _TypeCode; } set { _TypeCode = value; } } public float Calc { get { return _Calc; } set { _Calc = value; } } public DateTime ProcessDate { get { return _ProcessDate} set { _ProcessDate = value; } } public bool ProcessFlag { get { return _ProcessFlag} set { _ProcessFlag = value; } } } }

    Read the article

  • Segmentation fault with queue in C

    - by Trevor
    I am getting a segmentation fault with the following code after adding structs to my queue. The segmentation fault occurs when the MAX_QUEUE is set high but when I set it low (100 or 200), the error doesn't occur. It has been a while since I last programmed in C, so any help is appreciated. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_QUEUE 1000 struct myInfo { char data[20]; }; struct myInfo* queue; void push(struct myInfo); int queue_head = 0; int queue_size = 0; int main(int argc, char *argv[]) { queue = (struct myInfo*) malloc(sizeof(struct myInfo) * MAX_QUEUE); struct myInfo info; char buf[10]; strcpy(buf, "hello"); while (1) { strcpy(info.data, buf); push(info); } } void push(struct myInfo info) { int next_index = sizeof(struct myInfo) * ((queue_size + queue_head) % MAX_QUEUE); printf("Pushing %s to %d\n", info.data, next_index); *(queue + (next_index)) = info; queue_size++; } Output: Pushing hello to 0 Pushing hello to 20 ... Pushing hello to 7540 Pushing hello to 7560 Pushing hello to 7580 Segmentation fault

    Read the article

  • Reversing a circular deque without a sentinel

    - by SDLFunTimes
    Hey Stackoverflow I'm working on my homework and I'm trying to reverse a circular-linked deque without a sentinel. Here are my data structures: struct DLink { TYPE value; struct DLink * next; struct DLink * prev; }; struct cirListDeque { int size; struct DLink *back; }; Here's my approach to reversing the deque: void reverseCirListDeque(struct cirListDeque* q) { struct DLink* current; struct DLink* temp; temp = q->back->next; q->back->next = q->back->prev; q->back->prev = temp; current = q->back->next; while(current != q->back->next) { temp = current->next; current->next = current->prev; current->prev = temp; current = current->next; } } However when I run it and put values 1, 2 and 3 on it (TYPE is just a alias for int in this case) and reverse it I get 2, 3, null. Does anyone have any ideas as to what I may be doing wrong? Thanks in advance.

    Read the article

  • Hi I am facing a fragmentation error while executing this code? Can someone explain why?

    - by aks
    #include<stdio.h> struct table { char *ipAddress; char *domainName; struct table *next; }; struct table *head = NULL; void add_rec(); void show_rec(); int main() { add_rec(); show_rec(); return 0; } void add_rec() { struct table * temp = head; struct table * temp1 = (struct table *)malloc(sizeof(struct table)); if(!temp1) printf("\n Unable to allocate memory \n"); printf("Enter the ip address you want \n"); scanf("%s",temp1->ipAddress); printf("\nEnter the domain name you want \n"); scanf("%s",temp1->domainName); if(!temp) { head = temp; } else { while(temp->next!=NULL) temp = temp->next; temp->next = temp1; } } void show_rec() { struct table * temp = head; if(!temp) printf("\n No entry exists \n"); while(temp!=NULL) { printf("ipAddress = %s\t domainName = %s\n",temp->ipAddress,temp->domainName); temp = temp->next; } } When i execute this code and enters the IP address for the first node, i am facing fragmentation error. The code crashed. Can someone enlighten?

    Read the article

  • C two functions in one with casts

    - by Favolas
    I have two functions that do the exact same thing but in two different types of struct and this two types of struct are very similar. Imagine I have this two structs. typedef struct nodeOne{ Date *date; struct nodeOne *next; struct nodeOne *prev; }NodeOne; typedef struct nodeTwo{ Date *date; struct nodeTwo *next; struct nodeTwo *prev; }NodeTwo; Since my function to destroy each of the list is almost the same (Just the type of the arguments are different) I would like to make just one function to make the two thins. I have this two functions void destroyListOne(NodeOne **head, NodeOne **tail){ NodeOne *aux; while (*head != NULL){ aux = *head; *head = (*head)->next; free(aux); } *tail = NULL; } and this one: void destroyListTwo(NodeTwo **head, NodeTwo **tail){ NodeTwo *aux; while (*head != NULL){ aux = *head; *head = (*head)->next; free(aux); } *tail = NULL; } Since they are very similar I thought making something like this: void destroyList(void **ini, void **end, int listType){ if (listType == 0) { NodeOne *aux; NodeOne head = (NodeOne) ini; NodeOne tail = (NodeOne) ed; } else { NodeTwo *aux; NodeTwo head = (NodeTwo) ini; NodeTwo tail = (NodeTwo) ed; } while (*head != NULL){ aux = *head; *head = (*head)->next; free(aux); } *tail = NULL; } As you may now this is not working but I want to know if this is possible to achieve. I must maintain both of the structs as they are.

    Read the article

  • A Question about dereferencing pointer to incomplete type In C programming

    - by user552279
    Hi, can you explain this error for me? Blockquote /////////////////////////////// In my A.h file: struct TreeNode; struct TreeHead; typedef struct TreeNode * Node; typedef struct TreeHead * Head; /////////////////////////////// In my A.c file: struct TreeNode { char* theData; Node Left; Node Right; } ; struct TreeHead{ int counter; char type; Node Root; }; Head Initialisation() { Head treeHead; treeHead = malloc(sizeof (struct TreeHead)); treeHead-Root = malloc(sizeof (struct TreeNode)); return treeHead; } /////////////////////////////// In my Main.c file: Head head; Node tree; int choose =5; head = Initialisation(); (head-Root) = tree; //When compiling, this line has an error: error: dereferencing pointer to incomplete type Blockquote haed-Root will return a Node pointer, tree is also a Node pointer. So why error is dereferencing pointer to "incomplete" type?

    Read the article

  • How do I improve my performance with this singly linked list struct within my program?

    - by Jesus
    Hey guys, I have a program that does operations of sets of strings. We have to implement functions such as addition and subtraction of two sets of strings. We are suppose to get it down to the point where performance if of O(N+M), where N,M are sets of strings. Right now, I believe my performance is at O(N*M), since I for each element of N, I go through every element of M. I'm particularly focused on getting the subtraction to the proper performance, as if I can get that down to proper performance, I believe I can carry that knowledge over to the rest of things I have to implement. The '-' operator is suppose to work like this, for example. Declare set1 to be an empty set. Declare set2 to be a set with { a b c } elements Declare set3 to be a set with ( b c d } elements set1 = set2 - set3 And now set1 is suppose to equal { a }. So basically, just remove any element from set3, that is also in set2. For the addition implementation (overloaded '+' operator), I also do the sorting of the strings (since we have to). All the functions work right now btw. So I was wondering if anyone could a) Confirm that currently I'm doing O(N*M) performance b) Give me some ideas/implementations on how to improve the performance to O(N+M) Note: I cannot add any member variables or functions to the class strSet or to the node structure. The implementation of the main program isn't very important, but I will post the code for my class definition and the implementation of the member functions: strSet2.h (Implementation of my class and struct) // Class to implement sets of strings // Implements operators for union, intersection, subtraction, // etc. for sets of strings // V1.1 15 Feb 2011 Added guard (#ifndef), deleted using namespace RCH #ifndef _STRSET_ #define _STRSET_ #include <iostream> #include <vector> #include <string> // Deleted: using namespace std; 15 Feb 2011 RCH struct node { std::string s1; node * next; }; class strSet { private: node * first; public: strSet (); // Create empty set strSet (std::string s); // Create singleton set strSet (const strSet &copy); // Copy constructor ~strSet (); // Destructor int SIZE() const; bool isMember (std::string s) const; strSet operator + (const strSet& rtSide); // Union strSet operator - (const strSet& rtSide); // Set subtraction strSet& operator = (const strSet& rtSide); // Assignment }; // End of strSet class #endif // _STRSET_ strSet2.cpp (implementation of member functions) #include <iostream> #include <vector> #include <string> #include "strset2.h" using namespace std; strSet::strSet() { first = NULL; } strSet::strSet(string s) { node *temp; temp = new node; temp->s1 = s; temp->next = NULL; first = temp; } strSet::strSet(const strSet& copy) { if(copy.first == NULL) { first = NULL; } else { node *n = copy.first; node *prev = NULL; while (n) { node *newNode = new node; newNode->s1 = n->s1; newNode->next = NULL; if (prev) { prev->next = newNode; } else { first = newNode; } prev = newNode; n = n->next; } } } strSet::~strSet() { if(first != NULL) { while(first->next != NULL) { node *nextNode = first->next; first->next = nextNode->next; delete nextNode; } } } int strSet::SIZE() const { int size = 0; node *temp = first; while(temp!=NULL) { size++; temp=temp->next; } return size; } bool strSet::isMember(string s) const { node *temp = first; while(temp != NULL) { if(temp->s1 == s) { return true; } temp = temp->next; } return false; } strSet strSet::operator + (const strSet& rtSide) { strSet newSet; newSet = *this; node *temp = rtSide.first; while(temp != NULL) { string newEle = temp->s1; if(!isMember(newEle)) { if(newSet.first==NULL) { node *newNode; newNode = new node; newNode->s1 = newEle; newNode->next = NULL; newSet.first = newNode; } else if(newSet.SIZE() == 1) { if(newEle < newSet.first->s1) { node *tempNext = newSet.first; node *newNode; newNode = new node; newNode->s1 = newEle; newNode->next = tempNext; newSet.first = newNode; } else { node *newNode; newNode = new node; newNode->s1 = newEle; newNode->next = NULL; newSet.first->next = newNode; } } else { node *prev = NULL; node *curr = newSet.first; while(curr != NULL) { if(newEle < curr->s1) { if(prev == NULL) { node *newNode; newNode = new node; newNode->s1 = newEle; newNode->next = curr; newSet.first = newNode; break; } else { node *newNode; newNode = new node; newNode->s1 = newEle; newNode->next = curr; prev->next = newNode; break; } } if(curr->next == NULL) { node *newNode; newNode = new node; newNode->s1 = newEle; newNode->next = NULL; curr->next = newNode; break; } prev = curr; curr = curr->next; } } } temp = temp->next; } return newSet; } strSet strSet::operator - (const strSet& rtSide) { strSet newSet; newSet = *this; node *temp = rtSide.first; while(temp != NULL) { string element = temp->s1; node *prev = NULL; node *curr = newSet.first; while(curr != NULL) { if( element < curr->s1 ) break; if( curr->s1 == element ) { if( prev == NULL) { node *duplicate = curr; newSet.first = newSet.first->next; delete duplicate; break; } else { node *duplicate = curr; prev->next = curr->next; delete duplicate; break; } } prev = curr; curr = curr->next; } temp = temp->next; } return newSet; } strSet& strSet::operator = (const strSet& rtSide) { if(this != &rtSide) { if(first != NULL) { while(first->next != NULL) { node *nextNode = first->next; first->next = nextNode->next; delete nextNode; } } if(rtSide.first == NULL) { first = NULL; } else { node *n = rtSide.first; node *prev = NULL; while (n) { node *newNode = new node; newNode->s1 = n->s1; newNode->next = NULL; if (prev) { prev->next = newNode; } else { first = newNode; } prev = newNode; n = n->next; } } } return *this; }

    Read the article

  • C# XML parsing with LINQ storing directly to a struct?

    - by Luke
    Say I have the following XML schema: <root> <version>2.0</version> <type>fiction</type> <chapters> <chapter>1</chapter> <title>blah blah</title> </chapter> <chapters> <chapter>2</chapter> <title>blah blah</title> </chapters> </root> Would it be possibly to parse the elements which I know will not be repeated in the XML and store them directly into the struct using LINQ? For example, could I do something like this for "version" and "type" //setup structs Book book = new Book(); book.chapter = new Chapter(); //use LINQ to parse the xml var bookData = from b in xmlDoc.Decendants("root") select new { book.version = b.Element("version").Value, book.type = b.Element("type").Value }; //then for "chapters" since I know there are multiple I can do: var chapterData = from c in xmlDoc.Decendants("root" select new { chapter = c.Element("chapters") }; foreach (var ch in chapterData) { book.chapter.Add(getChapterData(ch.chapter)); }

    Read the article

  • What is the fastest (to access) struct-like object in Python?

    - by DNS
    I'm optimizing some code whose main bottleneck is running through and accessing a very large list of struct-like objects. Currently I'm using namedtuples, for readability. But some quick benchmarking using 'timeit' shows that this is really the wrong way to go where performance is a factor: Named tuple with a, b, c: >>> timeit("z = a.c", "from __main__ import a") 0.38655471766332994 Class using __slots__, with a, b, c: >>> timeit("z = b.c", "from __main__ import b") 0.14527461047146062 Dictionary with keys a, b, c: >>> timeit("z = c['c']", "from __main__ import c") 0.11588272541098377 Tuple with three values, using a constant key: >>> timeit("z = d[2]", "from __main__ import d") 0.11106188992948773 List with three values, using a constant key: >>> timeit("z = e[2]", "from __main__ import e") 0.086038238242508669 Tuple with three values, using a local key: >>> timeit("z = d[key]", "from __main__ import d, key") 0.11187358437882722 List with three values, using a local key: >>> timeit("z = e[key]", "from __main__ import e, key") 0.088604143037173344 First of all, is there anything about these little timeit tests that would render them invalid? I ran each several times, to make sure no random system event had thrown them off, and the results were almost identical. It would appear that dictionaries offer the best balance between performance and readability, with classes coming in second. This is unfortunate, since, for my purposes, I also need the object to be sequence-like; hence my choice of namedtuple. Lists are substantially faster, but constant keys are unmaintainable; I'd have to create a bunch of index-constants, i.e. KEY_1 = 1, KEY_2 = 2, etc. which is also not ideal. Am I stuck with these choices, or is there an alternative that I've missed?

    Read the article

  • Constructing radiotap header and ieee80211 header structures for packet injection

    - by hektor
    I am trying to communicate between two laptop machines using Wifi. The structure of the radiotap header and ieee80211 header I am using is: struct ieee80211_radiotap_header { unsigned char it_version; uint16_t it_len; uint32_t it_present; }; /* Structure for 80211 header */ struct ieee80211_hdr_3addr { uint16_t frame_ctl[2]; uint16_t duration_id; unsigned char addr1[ETH_ALEN]; unsigned char addr2[ETH_ALEN]; unsigned char addr3[ETH_ALEN]; uint16_t seq_ctl; }; struct packet { struct ieee80211_radiotap_header rtap_header; struct ieee80211_hdr_3addr iee802_header; unsigned char payload[30]; }; /* In main program */ struct packet mypacket; struct ieee80211_radiotap_header ratap_header; struct ieee80211_hdr_3addr iee802_header; unsigned char addr1[ETH_ALEN] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; /* broadcast address */ unsigned char addr2[ETH_ALEN] = {0x28,0xcf,0xda,0xde,0xd3,0xcc}; /* mac address of network card */ unsigned char addr3[ETH_ALEN] = {0xd8,0xc7,0xc8,0xd7,0x9f,0x21}; /* mac address of access point i am trying to connect to */ /* Radio tap header data */ ratap_header.it_version = 0x00; ratap_header.it_len = 0x07; ratap_header.it_present = (1 << IEEE80211_RADIOTAP_RATE); mypacket.rtap_header = ratap_header; /* ieee80211 header data */ iee802_header.frame_ctl[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_BEACON; iee802_header.frame_ctl[1] =IEEE80211_FC1_DIR_NODS; strcpy(iee802_header.addr1,addr1); strcpy(iee802_header.addr2,addr2); strcpy(iee802_header.addr3,addr3); iee802_header.seq_ctl = 0x1086; mypacket.iee802_header=iee802_header; /* Payload */ unsigned char payload[PACKET_LENGTH]="temp"; strcpy(mypacket.payload , payload); I am able to receive the packets when I test the transmission and reception on the same laptop. However I am not able to receive the packet transmitted on a different laptop. Wireshark does not show the packet as well. Can anyone point out the mistake I am making?

    Read the article

  • Why does forward declaration not work with classes?

    - by eSKay
    int main() { B bb; //does not compile (neither does class B bb;) C cc; //does not compile struct t tt; //compiles class B {}; struct s { struct t * pt; }; //compiles struct t { struct s * ps; }; return 0; } class C {}; I just modified the example given here. Why is that the struct forward declarations work but not the class forward declarations? Does it have something to do with the namespaces - tag namespace and typedef namespace? I know that the structure definitions without typedefs go to tag namespace. Structures are just classes with all public members. So, I expect them to behave similarly.

    Read the article

< Previous Page | 15 16 17 18 19 20 21 22 23 24 25 26  | Next Page >