Search Results

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

Page 22/103 | < Previous Page | 18 19 20 21 22 23 24 25 26 27 28 29  | Next Page >

  • What software design pattern is best for the following scenario (C#)

    - by askjdh
    I have a gps device that records data e.g. datetime, latitude, longitude I have an sdk that reads the data from the device. The way the data is read: A command packet (basically a combination of int values in a struct) is sent to the device. The device responds with the data in fixed size chunks e.g. 64bytes Depending on the command issued I will get back differect data structs e.g. sending command 1 to the device returns a struct like struct x { id int, name char[20] } command 2 returns a collection of the following structs (basically it boils down to an array of the structs - y[12]) struct y { date datetime, lat decimal, lon decimal } I would then want to convert the struct to a class and save the data to a database. What would be the best way to encapsulate the entire process, preferably using some established design pattern? Many thanks M

    Read the article

  • Setting pointers to structs

    - by Bobby
    I have the following struct: struct Datastore_T { Partition_Datastores_T cmtDatastores; // bytes 0 to 499 Partition_Datastores_T cdhDatastores; // bytes 500 to 999 Partition_Datastores_T gncDatastores; // bytes 1000 to 1499 Partition_Datastores_T inpDatastores; // bytes 1500 1999 Partition_Datastores_T outDatastores; // bytes 2000 to 2499 Partition_Datastores_T tmlDatastores; // bytes 2500 to 2999 Partition_Datastores_T sm_Datastores; // bytes 3000 to 3499 }; I want to set a char* to struct of this type like so: struct Datastore_T datastores; // Elided: datastores is initialized with data here char* DatastoreStartAddr = (char*)&datastores; memset(DatastoreStartAddr, 0, 3500); The problem I have is that DatastoreStartAddr always has a value of zero when it should point to the struct that has been initialized with data. What am I doing wrong?

    Read the article

  • compare function for dates

    - by Chris
    I have struct as: struct stored { char *dates; // 12/May/2010, 10/Jun/2010 etc.. int ctr; }; // const struct stored structs[] = {{"12/May/2010", 1}, {"12/May/2011", 1}, {"21/May/2009", 4}, {"12/May/2011", 3}, {"10/May/2011", 8}, {"12/May/2011", 4 }}; What I want to do is to sort struct 'stored' by stored.dates. qsort(structs, 9, sizeof(struct stored*), sortdates); // sortdates function I'm not quite sure what would be a good way to sort those days? Compare them as c-strings?

    Read the article

  • Marshalling a C structure to C#

    - by Hilbert
    Hi, I don't know how to marshall this structure in Mono. typedef struct rib_struct { rib_used_t used; rib_status_t status; rib_role_t role; uint8_t conf; rib_dc_t *pending; pthread_mutex_t mutex; pthread_cond_t cond; rib_f_t *props; } rib_t; And for example, rib_dc_t is like: typedef struct rib_dc_struct { uint16_t id; uint8_t min_id; uint8_t conf; struct rib_dc_struct *next; } rib_dc_t; I don't know how to marshall the pthread structures. And the pointers... should I use IntPtr or a managed structures? How to mashall the pointer in the last struct to the struct itself? Thanks in adanvaced

    Read the article

  • Why we used double pointer in objective-C or C language?

    - by Rajendra Bhole
    Hi, I confused when i want to take single pointer and when should i take double pointer? In following structure what exactly did? struct objc_class { Class isa; Class super_class; const char *name; long version; long info; long instance_size; struct objc_ivar_list *ivars; struct objc_method_list **methodLists; struct objc_cache *cache; struct objc_protocol_list *protocols; }; Why we use the "**methodLists" double pointer?

    Read the article

  • NULL pointer dereference in C

    - by user554125
    hey ive got this piece of code. It dereferences a null pointer here. But then there is an and with unsigned int. I really dont understand the whole part. Can someone explain the output.?? struct hi { long a; int b; long c; }; int main() { struct hi ob={3,4,5}; struct hi *ptr=&ob; int num= (unsigned int) & (((struct hi *)0)->b); printf("%d",num); printf("%d",*(int *)((char *)ptr + (unsigned int) & (((struct hi *)0)->b))); } The o/p i get is 44 .But how does it work?

    Read the article

  • Adding nodes to a global linked-list

    - by Zack
    I am attempting to construct my first linked list, and having read a basic introduction, have done the following. Firstly, declare a linked list node as: struct errorNode { uint8 error; struct errorNode* next; }; Secondly, define the first node globally as: struct errorNode errorList = {0, NULL}; This has been done to allow each of the libraries that make up my current project to insert errors into a common list. The function to do this is: void errorListWrite(uint8 error) { struct errorNode* newNode = malloc(sizeof(struct errorNode)); newNode->error = error; newNode->next = &errorList; errorList = *newNode; } Whilst this compiles without error, it does not function as expected. I thnk the problem is with the last two statements of the list write function, but I am unsure. A hint as to what I am doing wrong would be most appreciated.

    Read the article

  • What .NET UnmanagedType is Unicode (UTF-16)?

    - by Pat
    I am packing bytes into a struct, and some of them correspond to a Unicode string. The following works fine for an ASCII string: [StructLayout(LayoutKind.Sequential)] private struct PacketBytes { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)] public string MyString; } I assumed that I could do [StructLayout(LayoutKind.Sequential)] private struct PacketBytes { [MarshalAs(UnmanagedType.LPWStr, SizeConst = 32)] public string MyString; } to make it Unicode, but that didn't work. (Since this field is part of a struct with other fields, which I've omitted for clarity, I can't simply change the CharSet of the containing struct.) Any idea what I'm doing wrong?

    Read the article

  • How can I declare a pointer structure using {}?

    - by Y_Y
    This probably is one of the easiest question ever in C programming language... I have the following code: typedef struct node { int data; struct node * after; struct node * before; }node; struct node head = {10,&head,&head}; Is there a way I can make head to be *head [make it a pointer] and still have the availability to use '{ }' [{10,&head,&head}] to declare an instance of head and still leave it out in the global scope? For example: //not legal!!! struct node *head = {10,&head,&head};

    Read the article

  • Is apparent NULL pointer dereference in C actually pointer arithmetic?

    - by karthik A
    hey ive got this piece of code. It dereferences a null pointer here. But then there is an and with unsigned int. I really dont understand the whole part. Can someone explain the output.?? struct hi { long a; int b; long c; }; int main() { struct hi ob={3,4,5}; struct hi *ptr=&ob; int num= (unsigned int) & (((struct hi *)0)->b); printf("%d",num); printf("%d",*(int *)((char *)ptr + (unsigned int) & (((struct hi *)0)->b))); } The output I get is 44. But how does it work?

    Read the article

  • bool - 255 value

    - by user994165
    I have this function defined: struct heap_validation { size_t num_alloc; size_t num_alloc_sz; struct memory *mem; }; ... bool get_isallocated(struct metadata_record *); When I call the heap_validation function from hashtable_traverse and print the result, I've gotten the following values: 0,255 ,246 void hashtable_traverse(struct metadata_record *metarec, struct heap_validation *heap_val) { printf("get_isallocated(metarec): %d\n",get_isallocated(metarec)); bool retrieved = false; bool allocated = get_isallocated(metarec); if (allocated) { heap_val->num_alloc += 1; I also tried with %d and I get the same result.

    Read the article

  • Processing incorrect mac addresses from 802.11 frames with pcap

    - by Quentin Swain
    I'm working throurgh a project with pcap and wireless. Following an example posted in response to oe of my earlier questions I am trying to extract the mac addresses from wireless frames. I have created structures for the radiotap header and a basic management frame. For some reason when it comes to trying to output the mac addresses I am printing out the wrong data. When I compare to wireshark I don't see why the radio tap data is printing out correctly but the mac addresses are not. I don't see any additional padding in the hex dump that wireshark displays when i look at the packets and compare the packets that I have captured. I am somewhat famialar with c but not an expert so maybe I am not using the pointers and structures properly could someone help show me what I am doing wrong? Thanks, Quentin // main.c // MacSniffer // #include <pcap.h> #include <string.h> #include <stdlib.h> #define MAXBYTES2CAPTURE 65535 #ifdef WORDS_BIGENDIAN typedef struct frame_control { unsigned int subtype:4; /*frame subtype field*/ unsigned int protoVer:2; /*frame type field*/ unsigned int version:2; /*protocol version*/ unsigned int order:1; unsigned int protected:1; unsigned int moreDate:1; unsigned int power_management:1; unsigned int retry:1; unsigned int moreFrag:1; unsigned int fromDS:1; unsigned int toDS:1; }frame_control; struct ieee80211_radiotap_header{ u_int8_t it_version; u_int8_t it_pad; u_int16_t it_len; u_int32_t it_present; u_int64_t MAC_timestamp; u_int8_t flags; u_int8_t dataRate; u_int16_t channelfrequency; u_int16_t channFreq_pad; u_int16_t channelType; u_int16_t channType_pad; u_int8_t ssiSignal; u_int8_t ssiNoise; u_int8_t antenna; }; #else typedef struct frame_control { unsigned int protoVer:2; /* protocol version*/ unsigned int type:2; /*frame type field (Management,Control,Data)*/ unsigned int subtype:4; /* frame subtype*/ unsigned int toDS:1; /* frame coming from Distribution system */ unsigned int fromDS:1; /*frame coming from Distribution system */ unsigned int moreFrag:1; /* More fragments?*/ unsigned int retry:1; /*was this frame retransmitted*/ unsigned int powMgt:1; /*Power Management*/ unsigned int moreDate:1; /*More Date*/ unsigned int protectedData:1; /*Protected Data*/ unsigned int order:1; /*Order*/ }frame_control; struct ieee80211_radiotap_header{ u_int8_t it_version; u_int8_t it_pad; u_int16_t it_len; u_int32_t it_present; u_int64_t MAC_timestamp; u_int8_t flags; u_int8_t dataRate; u_int16_t channelfrequency; u_int16_t channelType; int ssiSignal:8; int ssiNoise:8; }; #endif struct wi_frame { u_int16_t fc; u_int16_t wi_duration; u_int8_t wi_add1[6]; u_int8_t wi_add2[6]; u_int8_t wi_add3[6]; u_int16_t wi_sequenceControl; // u_int8_t wi_add4[6]; //unsigned int qosControl:2; //unsigned int frameBody[23124]; }; void processPacket(u_char *arg, const struct pcap_pkthdr* pkthdr, const u_char* packet) { int i= 0, *counter = (int *) arg; struct ieee80211_radiotap_header *rh =(struct ieee80211_radiotap_header *)packet; struct wi_frame *fr= (struct wi_frame *)(packet + rh->it_len); u_char *ptr; //printf("Frame Type: %d",fr->wi_fC->type); printf("Packet count: %d\n", ++(*counter)); printf("Received Packet Size: %d\n", pkthdr->len); if(rh->it_version != NULL) { printf("Radiotap Version: %d\n",rh->it_version); } if(rh->it_pad!=NULL) { printf("Radiotap Pad: %d\n",rh->it_pad); } if(rh->it_len != NULL) { printf("Radiotap Length: %d\n",rh->it_len); } if(rh->it_present != NULL) { printf("Radiotap Present: %c\n",rh->it_present); } if(rh->MAC_timestamp != NULL) { printf("Radiotap Timestamp: %u\n",rh->MAC_timestamp); } if(rh->dataRate != NULL) { printf("Radiotap Data Rate: %u\n",rh->dataRate); } if(rh->channelfrequency != NULL) { printf("Radiotap Channel Freq: %u\n",rh->channelfrequency); } if(rh->channelType != NULL) { printf("Radiotap Channel Type: %06x\n",rh->channelType); } if(rh->ssiSignal != NULL) { printf("Radiotap SSI signal: %d\n",rh->ssiSignal); } if(rh->ssiNoise != NULL) { printf("Radiotap SSI Noise: %d\n",rh->ssiNoise); } ptr = fr->wi_add1; int k= 6; printf("Destination Address:"); do{ printf("%s%X",(k==6)?" ":":",*ptr++); } while(--k>0); printf("\n"); ptr = fr->wi_add2; k=0; printf("Source Address:"); do{ printf("%s%X",(k==6)?" ":":",*ptr++); }while(--k>0); printf("\n"); ptr = fr->wi_add3; k=0; do{ printf("%s%X",(k==6)?" ":":",*ptr++); } while(--k>0); printf("\n"); /* for(int j = 0; j < 23124;j++) { if(fr->frameBody[j]!= NULL) { printf("%x",fr->frameBody[j]); } } */ for (i = 0;i<pkthdr->len;i++) { if(isprint(packet[i +rh->it_len])) { printf("%c",packet[i + rh->it_len]); } else{printf(".");} //print newline after each section of the packet if((i%16 ==0 && i!=0) ||(i==pkthdr->len-1)) { printf("\n"); } } return; } int main(int argc, char** argv) { int count = 0; pcap_t* descr = NULL; char errbuf[PCAP_ERRBUF_SIZE], *device = NULL; struct bpf_program fp; char filter[]="wlan broadcast"; const u_char* packet; memset(errbuf,0,PCAP_ERRBUF_SIZE); device = argv[1]; if(device == NULL) { fprintf(stdout,"Supply a device name "); } descr = pcap_create(device,errbuf); pcap_set_rfmon(descr,1); pcap_set_promisc(descr,1); pcap_set_snaplen(descr,30); pcap_set_timeout(descr,10000); pcap_activate(descr); int dl =pcap_datalink(descr); printf("The Data Link type is %s",pcap_datalink_val_to_name(dl)); //pcap_dispatch(descr,MAXBYTES2CAPTURE,1,512,errbuf); //Open device in promiscuous mode //descr = pcap_open_live(device,MAXBYTES2CAPTURE,1,512,errbuf); /* if(pcap_compile(descr,&fp,filter,0,PCAP_NETMASK_UNKNOWN)==-1) { fprintf(stderr,"Error compiling filter\n"); exit(1); } if(pcap_setfilter(descr,&fp)==-1) { fprintf(stderr,"Error setting filter\n"); exit(1); } */ pcap_loop(descr,0, processPacket, (u_char *) &count); return 0; }

    Read the article

  • Static linking in Qt --> link errors in VS 2008

    - by ChruS
    Today I dediced to make static linking in Qt. I used Qt4 with Visual Studio and static C runtime article. The 3rd step took quite a long time. When it was finished I opened my project in VS 2008, made Build->Clean Solution and try to Release. Unfortunately I got link errors: warning LNK4098: defaultlib 'libcmt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library error LNK2019: unresolved external symbol "private: static struct QString::Data QString::shared_null" (?shared_null@QString@@0UData@1@A) referenced in function "public: __thiscall QString::QString(void)" (??0QString@@QAE@XZ) error LNK2019: unresolved external symbol "private: static struct QByteArray::Data QByteArray::shared_null" (?shared_null@QByteArray@@0UData@1@A) referenced in function "public: __thiscall QByteArray::QByteArray(void)" (??0QByteArray@@QAE@XZ) error LNK2001: unresolved external symbol "private: static struct QByteArray::Data QByteArray::shared_null" (?shared_null@QByteArray@@0UData@1@A) error LNK2001: unresolved external symbol "public: static struct QMetaObject const QDialog::staticMetaObject" (?staticMetaObject@QDialog@@2UQMetaObject@@B) error LNK2001: unresolved external symbol "public: static struct QMetaObject const QDialog::staticMetaObject" (?staticMetaObject@QDialog@@2UQMetaObject@@B) error LNK2001: unresolved external symbol "public: static struct QMetaObject const QDialog::staticMetaObject" (?staticMetaObject@QDialog@@2UQMetaObject@@B) error LNK2019: unresolved external symbol "public: static enum QSysInfo::WinVersion const QSysInfo::WindowsVersion" (?WindowsVersion@QSysInfo@@2W4WinVersion@1@B) referenced in function _WinMain@16 What i did wrong? Help to fix this pls.

    Read the article

  • Linux Kernel - Red/Black Trees

    - by CodeRanger
    I'm trying to implement a red/black tree in Linux per task_struct using code from linux/rbtree.h. I can get a red/black tree inserting properly in a standalone space in the kernel such as a module but when I try to get the same code to function with the rb_root declared in either task_struct or task_struct-files_struct, I get a SEGFAULT everytime I try an insert. Here's some code: In task_struct I create a rb_root struct for my tree (not a pointer). In init_task.h, macro INIT_TASK(tsk), I set this equal to RB_ROOT. To do an insert, I use this code: rb_insert(&(current-fd_tree), &rbnode); This is where the issue occurs. My insert command is the standard insert that is documented in all RBTree documentation for the kernel: int my_insert(struct rb_root *root, struct mytype *data) { struct rb_node **new = &(root->rb_node), *parent = NULL; /* Figure out where to put new node */ while (*new) { struct mytype *this = container_of(*new, struct mytype, node); int result = strcmp(data->keystring, this->keystring); parent = *new; if (result < 0) new = &((*new)->rb_left); else if (result > 0) new = &((*new)->rb_right); else return FALSE; } /* Add new node and rebalance tree. */ rb_link_node(&data->node, parent, new); rb_insert_color(&data->node, root); return TRUE; } Is there something I'm missing? Some reason this would work fine if I made a tree root outside of task_struct? If I make rb_root inside of a module this insert works fine. But once I put the actual tree root in the task_struct or even in the task_struct-files_struct, I get a SEGFAULT. Can a root node not be added in these structs? Any tips are greatly appreciated. I've tried nearly everything I can think of.

    Read the article

  • sendto: Invalid Argument

    - by Sylvain
    Hi, I have a list<struct sockaddr_in> _peers I'd like to use for sendto() the list is filled this way struct hostent *hp; hp = gethostbyname(hostname); sockaddr_in sin; bzero(&sin, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_port = htons(port); sin.sin_addr.s_addr = *(in_addr_t *)hp->h_addr; _peers.push_front(sin); and here's how I try to send: for (list<struct sockaddr_in>::iterator it = _peers.begin(); it != _peers.end(); ++it) { if (sendto(_s, "PING", 5, 0, (struct sockaddr *)&(*it), sizeof(struct sockaddr_in)) < 0) perror("sendto"); } outputs: sendto: Invalid argument If I create the struct sockaddr_in right before sendto(), everything works fine, so I guess I fail at using the list properly ... I also tested using &_peers.front() directly and still get the same error ... what am I doing wrong? Thanks in advance,

    Read the article

  • Static linking in Qt --> link errors in VS 2008

    - by ChruS
    Today I dediced to make static linking in Qt. I used Qt4 with Visual Studio and static C runtime article. The 3rd step took quite a long time. When it was finished I opened my project in VS 2008, made Build->Clean Solution and try to Release. Unfortunately I got link errors: warning LNK4098: defaultlib 'libcmt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library error LNK2019: unresolved external symbol "private: static struct QString::Data QString::shared_null" (?shared_null@QString@@0UData@1@A) referenced in function "public: __thiscall QString::QString(void)" (??0QString@@QAE@XZ) error LNK2019: unresolved external symbol "private: static struct QByteArray::Data QByteArray::shared_null" (?shared_null@QByteArray@@0UData@1@A) referenced in function "public: __thiscall QByteArray::QByteArray(void)" (??0QByteArray@@QAE@XZ) error LNK2001: unresolved external symbol "private: static struct QByteArray::Data QByteArray::shared_null" (?shared_null@QByteArray@@0UData@1@A) error LNK2001: unresolved external symbol "public: static struct QMetaObject const QDialog::staticMetaObject" (?staticMetaObject@QDialog@@2UQMetaObject@@B) error LNK2001: unresolved external symbol "public: static struct QMetaObject const QDialog::staticMetaObject" (?staticMetaObject@QDialog@@2UQMetaObject@@B) error LNK2001: unresolved external symbol "public: static struct QMetaObject const QDialog::staticMetaObject" (?staticMetaObject@QDialog@@2UQMetaObject@@B) error LNK2019: unresolved external symbol "public: static enum QSysInfo::WinVersion const QSysInfo::WindowsVersion" (?WindowsVersion@QSysInfo@@2W4WinVersion@1@B) referenced in function _WinMain@16 What i did wrong? Help to fix this pls.

    Read the article

  • Boost.Python wrapping hierarchies avoiding diamond inheritance

    - by stbuton
    I'm having some trouble seeing what the best way to wrap a series of classes with Boost.Python while avoiding messy inheritance problems. Say I have the classes A, B, and C with the following structure: struct A { virtual void foo(); virtual void bar(); virtual void baz(); }; struct B : public A { virtual void quux(); }; struct C : public A { virtual void foobar(); }; I want to wrap all classes A, B, and C such that they are extendable from Python. The normal method for accomplishing this would be along the lines of: struct A_Wrapper : public A, boost::python::wrapper<A> { //dispatch logic for virtual functions }; Now for classes B and C which extend from A I would like to be able to inherit and share the wrapping implementation for A. So I'd like to be able to do something along the lines of: struct B_Wrapper : public B, public A_Wrapper, public boost::python::wrapper<B> { //dispatch logic specific for B }; struct C_Wrapper : public C, public A_Wrapper, public boost::python::wrapper<C> { //dispatch logic specific for C } However, it seems like that would introduce all manner of nastiness with the double inheritance of the boost wrapper base and the double inheritance of A in the B_Wrapper and C_Wrapper objects. Is there a common way that this instance is solved that I'm missing? thanks.

    Read the article

  • c - strncpy issue

    - by Joe
    Hi there, I am getting segmentation fault when using strncpy and (pointer-to-struct)-(member) notation: I have simplified my code. I initialise a struct and set all of it's tokens to an empty string. Then a declare a pointer to a struct and assign the address of the struct to it. I pass the pointer to a function. I can print out the contents of the struct at the beginning of the function, but if I try to use the tp - mnemonic in a strncpy function, I get seg fault. Can anyone tell me what I am doing wrong? typedef struct tok { char* label; char* mnem; char* operand; }Tokens; Tokens* tokenise(Tokens* tp, char* line) { // This prints fine printf("Print this - %s\n", tp -> mnem); // This function gives me segmentation fault strncpy(tp -> mnem, line, 4); return tp; } int main() { char* line = "This is a line"; Tokens tokens; tokens.label = ""; tokens.mnem = "load"; tokens.operand = ""; Tokens* tp = &tokens; tp = tokenise(tp, line); return 0; } I have used printf statements to confirm that the code definitely stops executing at the strncpy function. Can anyone tell me where I am going wrong? Many thanks Joe

    Read the article

  • C question: error: expected ')' before '*' token

    - by lhw
    ===EDIT I apologize for not putting the pcb struct into the code snippet. There is a struct called pcb defined in above the two structs I originally posted. Namely, typedef struct{ UINT32 proc; struct pcb *link; }pcb; Hi, I asked a question regarding structs in C a few minutes ago and got an answer blazing fast. But now I'm facing another problem, namely the error in the title of this question. I'm trying to implement a simple priority queue in C using arrays of queues. However, when I try to declare a function on pcb_pQ structure, I get the above error. I have the structs clearly defined in the heard file. In the header file: typedef struct{ pcb *head; pcb *tail; SINT32 size; } pcb_Q; typedef struct pcb_pQ { pcb_Q queues[5]; SINT32 size; } pcb_pQ; Function prototype in header file: /*priority queue operations*/ VOID pcb_pq_enqueue(pcb_pQ*, pcb*); Function impelmentation in .c file: VOID pcb_pq_enqueue(pcb_pQ* pcb_pQ, pcb* pcb) { pcb_Q* pcb_Q_p; int priority; priority = pcb->proc_priority; pcb_Q_p = &pcb_pQ->queues[priority]; pcb_enqueue(pcb_Q_p, pcb); } When I try to compile the above code, I get an "error: expected ')' before '*' token". This error is pointing to the function signature in the .c file, namely VOID pcb_pq_enqueue(pcb_pQ* pcb_pQ, pcb* pcb) { But I am not sure why I am getting this error, could someone give me a hand? Thanks a lot.

    Read the article

  • How to cast sockaddr_storage and avoid breaking strict-aliasing rules

    - by sinoth
    I'm using Beej's Guide to Networking and came across an aliasing issue. He proposes a function to return either the IPv4 or IPv6 address of a particular struct: 1 void *get_in_addr( struct sockaddr *sa ) 2 { 3 if (sa->sa_family == AF_INET) 4 return &(((struct sockaddr_in*)sa)->sin_addr); 5 else 6 return &(((struct sockaddr_in6*)sa)->sin6_addr); 7 } This causes GCC to spit out a strict-aliasing error for sa on line 3. As I understand it, it is because I call this function like so: struct sockaddr_storage their_addr; ... inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), connection_name, sizeof connection_name); I'm guessing the aliasing has to do with the fact that the their_addr variable is of type sockaddr_storage and another pointer of a differing type points to the same memory. Is the best way to get around this sticking sockaddr_storage, sockaddr_in, and sockaddr_in6 into a union? It seems like this should be well worn territory in networking, I just can't find any good examples with best practices. Also, if anyone can explain exactly where the aliasing issue takes place, I'd much appreciate it.

    Read the article

  • memory alignment within gcc structs

    - by Mumbles
    I am porting an application to an ARM platform in C, the application also runs on an x86 processor, and must be backward compatible. I am now having some issues with variable alignment. I have read the gcc manual for __attribute__((aligned(4),packed)) I interpret what is being said as the start of the struct is aligned to the 4 byte boundry and the inside remains untouched because of the packed statement. originally I had this but occasionally it gets placed unaligned with the 4 byte boundary. typedef struct { unsigned int code; unsigned int length; unsigned int seq; unsigned int request; unsigned char nonce[16]; unsigned short crc; } __attribute__((packed)) CHALLENGE; so I change it to this. typedef struct { unsigned int code; unsigned int length; unsigned int seq; unsigned int request; unsigned char nonce[16]; unsigned short crc; } __attribute__((aligned(4),packed)) CHALLENGE; The understand I stated earlier seems to be incorrect as both the struct is now aligned to a 4 byte boundary, and and the inside data is now aligned to a four byte boundary, but because of the endianess, the size of the struct has increased in size from 42 to 44 bytes. This size is critical as we have other applications that depend on the struct being 42 bytes. Could some describe to me how to perform the operation that I require. Any help is much appreciated.

    Read the article

  • sizeof abuse : get the size of a const table

    - by shodanex
    When declaring a const table, it is possible to get the size of the table using sizeof. However, once you stop using the symbol name, it does not work anymore. is there a way to have the following program output the correct size for table A, instead of 0 ? #include <stdio.h> struct mystruct { int a; short b; }; const struct mystruct tableA[] ={ { .a = 1, .b = 2, }, { .a = 2, .b = 2, }, { .a = 3, .b = 2, }, }; const struct mystruct tableB[] ={ { .a = 1, .b = 2, }, { .a = 2, .b = 2, }, }; int main(int argc, char * argv[]) { int tbl_sz; const struct mystruct * table; table = tableA; tbl_sz = sizeof(table)/sizeof(struct mystruct); printf("size of table A : %d\n", tbl_sz); table = tableB; tbl_sz = sizeof(tableB)/sizeof(struct mystruct); printf("size of table B : %d\n", tbl_sz); return 0; } Output is : size of table A : 0 size of table B : 2

    Read the article

  • Speedup writing C programs using a subset of the Python syntax

    - by psihodelia
    I am constantly trying to optimize my time. Writing a C code takes a lot of time and requires much more keyboard touches than say writing a Python program. However, in order to speed up the time required to create a C program, one can automatize many things. I'd like to write my programs using smth. like Python but with C semantics. It means, all keywords are C keywords, but syntax is optimized. For example, this C code: #include "dsplib.h" #include "coeffs.h" #define MODULENAME "dsplib" #define NUM_SAMPLES 320 typedef float t_Vec; typedef struct s_Inter { char *pc_Name; struct s_Inter *px_Next; }t_Inter; typedef struct s_DspLibControl { t_Vec f_Y; }t_DspLibControl; void v_DspLibName(void) { printf("Module: %s", MODULENAME); printf("\n"); } int v_DspLibInitInterControl(t_DspLibControl *px_Con) { int y; px_Con->f_Y = 0.0; for(int i=0;i<10;i++) { y += i * i; } return y; } in optimized pythonized version can look like: include dsplib, coeffs define MODULENAME="dsplib", NUM_SAMPLES=320 typedef float t_Vec typedef struct s_Inter: char *pc_Name struct s_Inter *px_Next t_Inter typedef struct s_DspLibControl: t_Vec f_Y t_DspLibControl v_DspLibName(): printf("Module: %s", MODULENAME); printf("\n") int v_DspLibInitInterControl(t_DspLibControl *px_Con): int y px_Con->f_Y = 0.0 for int i=0;i<10;i++: y += i * i return y My question is: Do you know any VIM script, which allows to translate an original pythonized C code into a standard C code? For example, one is writing a C code but uses pythonized syntax, once she decides to translate pythonized blocks into standard C, she selects such blocks and press some key. And she doesn't save such pythonized code of course, VIM translates it into standard C.

    Read the article

  • flood fill algorithm

    - by user335593
    i want to implement the flood fill algorthm...so that when i get the x and y co-od of a point...it should start flooding from that point and fill till it finds a boundary but it is not filling the entire region...say a pentagon this is the code i am using void setpixel(struct fill fillcolor,int x,int y) { glColor3f(fillcolor.r,fillcolor.g,fillcolor.b); glBegin(GL_POINTS); glVertex2i(x,y); glEnd(); glFlush(); } struct fill getpixcol(int x,int y) { struct fill gotpixel; glReadPixels(x,y,1,1,GL_RGB,GL_UNSIGNED_BYTE,pick_col); gotpixel.r =(float) pick_col[0]/255.0; gotpixel.g =(float) pick_col[1]/255.0; gotpixel.b =(float) pick_col[2]/255.0; return(gotpixel); } void floodFill(int x, int y,struct fill fillcolor,struct fill boundarycolor) { struct fill tmp; // if ((x < 0) || (x >= 500)) return; // if ((y < 0) || (y >= 500)) return; tmp=getpixcol(x,y); while (tmp.r!=boundarycolor.r && tmp.g!=boundarycolor.g && tmp.b!=boundarycolor.b) { setpixel(fillcolor,x,y); setpixel(fillcolor,x+1,y); setpixel(fillcolor,x,y+1); setpixel(fillcolor,x,y-1); setpixel(fillcolor,x-1,y); floodFill(x-1,y+1,fillcolor,boundarycolor); floodFill(x-1,y,fillcolor,boundarycolor); floodFill(x-1,y-1,fillcolor,boundarycolor); floodFill(x,y+1,fillcolor,boundarycolor); floodFill(x,y-1,fillcolor,boundarycolor); floodFill(x+1,y+1,fillcolor,boundarycolor); floodFill(x+1,y,fillcolor,boundarycolor); floodFill(x+1,y-1,fillcolor,boundarycolor); } }

    Read the article

  • Pointers, am I doing them correctly? Objective-c/cocoa

    - by Chris
    I have this in my @interface struct track currentTrack; struct track previousTrack; int anInt; Since these are not objects, I do not have to have them like int* anInt right? And if setting non-object values like ints, boolean, etc, I do not have to release the old value right (assuming non-GC environment)? The struct contains objects: typedef struct track { NSString* theId; NSString* title; } *track; Am I doing that correctly? Lastly, I access the struct like this: [currentTrack.title ...]; currentTrack.theId = @"asdf"; //LINE 1 I'm also manually managing the memory (from a setter) for the struct like this: [currentTrack.title autorelease]; currentTrack.title = [newTitle retain]; If I'm understanding the garbage collection correctly, I should be able to ditch that and just set it like LINE 1 (above)? Also with garbage collection, I don't need a dealloc method right? If I use garbage collection does this mean it only runs on OS 10.5+? And any other thing I should know before I switch to garbage collected code? Sorry there are so many questions. Very new to objective-c and desktop programming. Thanks

    Read the article

< Previous Page | 18 19 20 21 22 23 24 25 26 27 28 29  | Next Page >