|
you can always do a 'container':
typedef struct intArray_t
{
int *pData;
int len;
} intArray;
intArray newIntArray(int len)
{
intArray ia;
ia.pData = (int*)malloc(len * sizeof(int));
ia.len = len;
return ia;
}
intArray myData = newIntArray(10);
etc.
then you pass intArrays around
|
|
|
|
|
Oh, of course. 
|
|
|
|
|
And now I've learned that the version of MinGW GCC I have will allow typeof and the declaration of variables in for statements ( for ( int i ... ) provided I invoke -std=gnu99 .
What a fun exercise for a rainy Friday afternoon.
modified 31-Jul-15 20:24pm.
|
|
|
|
|
Wait a minute... isn't that code example missing some indirection?
|
|
|
|
|
|
Or maybe that's not C? Can you return a local variable like that in C? Even returning a pointer to a local variable is verboten isn't it?
Or is my C-fu still that rusty?
|
|
|
|
|
sure, you can return locals - even if they're structs.
a pointer to a local would be a bad idea, because the local goes out of scope. but when you return a local, it makes a copy for the caller.
|
|
|
|
|
Huh. I guess I thought C was dumber than that, one semester of C wasn't enough. That explains that small memory leak I wrote in 1994... I wonder whether or not that program is still in use... 
|
|
|
|
|
I pulled out my old VAX C book for reference, and all I see is passing structs in as parameters, but it seems to say it's limited to 1020 bytes. If that's true, it must apply to return values as well -- they're still on the stack, yes?
|
|
|
|
|
that's probably the VMS stack size.
i believe the struct returning behavior is compiler dependent (OS dependent?). some will return the struct in a register, if the struct is small enough, or on the stack. some compilers are smart enough to use the caller's struct directly so as to avoid making a copy.
that struct i showed is sizeof(int *) + sizeof(int). so, 8 or 12 bytes. nothing to worry about.
|
|
|
|
|
I believe that's implementation defined. With my linux box I can use ulimit to set the size of the stack. By default it seems to be 8MB, but I can modify that upwards more or less as needed (obviously within memory limits of the system).
I seem to recall that early C implementations were limited to returning basic types (e.g. int, double, char *, etc). gcc-5.2 still has a warning flag for aggregate returns - which suggests that other C compilers might still adhere to that.
|
|
|
|
|
Method 0 is the one I observe most of the time (and use myself more frequently). Even Windows API use it.
Sentinel method has its usages (k5054 already pointed out its drawbacks).
I won't use method 2 , I mean I won't choose a linked list instead of an array just in order to avoid the extra parameter in my function.
|
|
|
|
|
Thanks.
The sentinel method worked well in this case, but then Chris Losinger suggested method 3 -- using a container*. So I'm using that now. This technique has the added benefit that I can store the number of items allocated as well as the number of items in use -- sort of like how a List works in .net. I can add items up to the limit (I have no need to expand the array; I know how many items I need up front).
* Didn't we used to call that a Control Block? A simple way to avoid having bullions and bullions of function parameters?
|
|
|
|
|
Quote: Didn't we used to call that a Control Block? A simple way to avoid having bullions and bullions of function parameters? Of course I am aware of the general technique (pass a struct instead of tons of parameters, again Windows API docet), however it is the very first time I hear the term 'Control Block' used with such a meaning.
By the way, you are welcome.
|
|
|
|
|
PIEBALD whips out his trusty "MS-DOS Programmer's Reference" (1993, "covers through version 6"!)... and it just says "structure", e.g. RWBLOCK structure .
And VMS uses "descriptors" which are similar.
"
The Descriptor
Classic C programming uses pointers to various structures, including null-terminated strings; ASCIZ strings. These are used within the OpenVMS standard C library, though most OpenVMS interfaces use descriptors. An OpenVMS construct that will be entirely new to even experienced C programmers is the string descriptor. This is typically a small data structure, containing the data length, data type, descriptor class, and data address for a chunk of data.
" -- http://labs.hoffmanlabs.com/node/273[^]
But I'm sure we used the term "Control Block" where I worked. Maybe it's from the UNIX culture? :shrug:
|
|
|
|
|
Quote: "MS-DOS Programmer's Reference"
Well, Carlo cannot argue with The Truth. 
|
|
|
|
|
Hi All,
While running a C++ Service in Wice indows 2008 Server i got below dump for service crash.
Crash Dump
------------
00 ntdll!ZwWaitForMultipleObjects+0xa
01 ntdll!RtlReportException+0x55e
02 ntdll!RtlReportException+0xbc
03 ntdll!RtlpNtMakeTemporaryKey+0x216
04 ntdll!_C_specific_handler+0x96
05 ntdll!wcstok_s+0x34be
06 ntdll!_chkstk+0x9d
07 ntdll!RtlRaiseException+0xf67
08 ntdll!RtlRaiseException+0x18d
09 ntdll!RtlpNtMakeTemporaryKey+0x1c0
0a ntdll!RtlpNtMakeTemporaryKey+0x3402
0b ntdll!RtlpNtMakeTemporaryKey+0x4000
0c ntdll!memset+0x160da
0d msvcr110!free+0x1c
0e wincli32!ConnectDlg+0x392
0f wincli32!OsGetCOPCLIVersion+0x7fed
10 ntdll!RtlActivateActivationContextUnsafeFast+0x114
11 ntdll!LdrShutdownThread+0x116
12 ntdll!RtlExitUserThread+0x3e
13 kernel32!BaseThreadInitThunk+0x15
14 ntdll!RtlUserThreadStart+0x34
please help me out.
Thanks,
Uday
|
|
|
|
|
janaswamy uday wrote: please help me out. With what? No one here has the remotest idea what program(s) you are running or where this error may have occurred.
|
|
|
|
|
|
Here's a quick run-down of the NEC protocol. http://techdocs.altium.com/display/FPGA/NEC+Infrared+Transmission+Protocol[^]
And here's some more general stuff: https://en.wikipedia.org/wiki/Consumer_IR[^]
A good place to look for the actual data/commands sent using the various protocols is in the microcontroller community. The Arduino/8501 forums/libraries have plenty of code available.
I once setup an arduino to record the incoming pulses from a remote. I then swapped the IR receiver module for a cheap 1mw laser - I can control the IR equipment from over 100m away now.
Unfortunately, the code's on a machine I dont have access to now and I've long since forgotten where I got the info from, sorry.
"When I was 5 years old, my mother always told me that happiness was the key to life. When I went to school, they asked me what I wanted to be when I grew up. I wrote down 'happy'. They told me I didn't understand the assignment, and I told them they didn't understand life." - John Lennon
|
|
|
|
|
Thank. 
|
|
|
|
|
|
Hello there. I have this structure in Header1.h and instantiating this structure in Header2.h. When I try to initialize this instance, I get this runtime exception. Here is my skeleton of code
HEADER1.h
struct Student {
CString Name;
CString Address;
CString Phone;
};
HEADER2.h
class CHeader2Dlg {
Student student;
}
HEADER2.cpp
void CHeader2Dlg::SomeFunction()
{
student.Name = "";
student.Address = "";
student.Phone = "";
}
|
|
|
|
|
Are you perhaps using the Unicode character set but assigning an ascii string?
Maybe it should be:
student.Name = L"";
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I changed that to L"" but in vein. Does it have to do anything with project settings ?
|
|
|
|