Have a memset issue in C++/CLI - c++-cli

I'm a newbie currently using c++/cli to wrap a few classes that i have used in my .lib file. And I am in dire need of using "memset" in my c++/cli. Anyone here knows how to use memset in c++/cli code?
The c++ code I'm trying to use in my c++/cli code:
memset(&DeviceInfo, 0, sizeof(FS_DEVICE_INFO));
Here's my c++/cli Code where I get the error when I try to use the same memset line from my c++ code:
bool newIFSWDevice::GetDeviceInfo(PFS_DEVICE_INFO pDevInfo)
{
IFSDevice* pDeviceWheel = nullptr;
FS_DEVICE_INFO DeviceInfo;
int x = 0;
while (nullptr != (pDeviceWheel = newFSDeviceEnumerator::EnumerateInstance(x++)))
{
memset(&DeviceInfo, 0, sizeof(FS_DEVICE_INFO)); //error line
pDeviceWheel->GetDeviceInfo(&DeviceInfo);
if (0 == wcscmp(DeviceInfo.Name, FS_DEVICE_WHEEL_PORSCHE_NAME)
break;
I tried using a for loop instead...
for (int i = 0; i <= sizeof(FS_DEVICE_INFO); i++)
FS_DEVICE_INFO[i] = 0;
But it still gives me an error "expression must have a constant value". Help would be much appreciated! :)

As noted in the comments, you're missing the header file #include <string.h>. See the documentation.
It's also worth noting that your for loop to do the clearing has several problems:
for (int i = 0; i <= sizeof(FS_DEVICE_INFO); i++)
FS_DEVICE_INFO[i] = 0;
sizeof(FS_DEVICE_INFO) gives you the size of that struct in bytes, but FS_DEVICE_INFO[i] indexes into an array of structs: [1] would be the second struct in the array, not the second byte! You would need to cast the pointer to char or something similar.
i <= sizeof(FS_DEVICE_INFO): The <= is incorrect. If the struct is 10 bytes large, you'd end up operating on bytes 0 through 10, which is 11 bytes total, stomping on whatever happened to be after the struct.
FS_DEVICE_INFO[i]: FS_DEVICE_INFO is the name of the class, your local variable is DeviceInfo, so this should be DeviceInfo[i]. This is why you're getting the expression must have a constant value error.

Related

Something weird in for loop speed

here is a part of my program code:
int test;
for(uint i = 0; i < 1700; i++) {
test++;
}
the whole program takes 0.5 seconds to finish, but when I change it to:
int test[1];
for(uint i = 0; i < 1700; i++) {
test[0]++;
}
it will takes 3.5 seconds! and when I change the int to double, it will gets very worse:
double test;
for(uint i = 0; i < 1700; i++) {
test++;
}
it will takes about 18 seconds to finish !!!
I have to increase an int array element and a double variable in my real for loop, and it will takes about 30 seconds!
What's happening here?! Why should it takes that much time for just an increment?!
I know a floating point data type like double has different structure from a fixed point data type like int, but is it the only cause for such a big different time? and what about the second example which is also an int array element?!
Thanks
You have answered your question yourself.
float (double) operations are different from integer ones. Even if you just add 1.0f.
Your second example takes longer than the first one just because you added some pointer refernces. An array in C is -bottom down- not much different from a pointer to the first element. Accessing any element, even the first one, would cause the machine code to load the starting address of the array multiply the index (0 in this case) with the length of each member (4 or whatever bytes int has) and add that (0) to the pointer. Then it has to dereference the pointer, meaning to acutally load the value at that very address. Add one and write back the result.
A smart modern compiler should optimize this a bit. When you want to avoid this optimization, then modify the code a bit and don`t use a constant for the index.
I never tried that with a modern objective-c compiler. But I guess that this code would take much loger than 3.5s to run:
int test[2];
int index = 0;
for(uint i = 0; i < 1700; i++) {
test[index]++;
}
If that does not make much of a change then try this:
-(void)foo:(int)index {
int test[2];
for(uint i = 0; i < 1700; i++) {
test[index]++;
}
}
and then call foo:0;
Give it a try and let us know :)

objective-c I can't understand why using of sprintf lead program to crash

-(void)InitWithPwd:(char *)pPwd
{
char szResult[17];
//generate md5 checksum
CC_MD5(pPwd, strlen(pPwd),&szResult[0]);
szResult[16] = 0;
m_csPasswordHash[0]=0;
for(int i = 0;i < 16;i++)
{
char sz[3] = {'\0'};
//crash in blow row. The first pass is ok. The third pass crash.
//I can't understand.
sprintf(&sz[0],"%2.2x",szResult[i]);
strcat(m_csPasswordHash,sz);
}
m_csPasswordHash[32] = 0;
printf("pass:%s\n",m_csPasswordHash);
m_ucPacketType = 1;
}
I want to get the md5 of the password. But above code crash again and again. I can't understand why.
Your buffer (sz) is too small, causing sprintf() to generate a buffer overflow which leads to undefined behavior, in your case a crash.
Note that szResult[1] might be a negative value when viewed as an int (which happens when passing a char-type value to sprintf()), which can cause sprintf() to disregard your field width and precision directives in order to format the full value.
Here is an example showing this problem. The example code is written in C, but that shouldn't matter for this case.
This solves the problem by making sure the incoming data is considered unsigned:
sprintf(sz, "%02x", (unsigned char) szResult[i]);

EXC_BAD_ACCESS in attempt to rewrite NSString ComponentsSeparatedByString:

I'm writing an objective-C program to deal with trajectories of Biomolecules with XCODE 4.3.1 and ARC. I need to read PDB files, i.e. parse large quantities of text formatted data. I'm very disappointed by NSString inefficiency and was trying to write a C-equivalent of componentsSeparatedByString:. The algorithm works just fine with NSString and NSMutableArrays, but i'm having a hard time using char* and char**.
Unfortunately, I'm getting an EXC_BAD_ACCESS error. The strange thing is that i get the error for i=68103 and j=68049 (does these number ring a bell for you ?), which means it worked for some time before crashing. The error is "static" (always block at the same (i,j) numbers). The array seems to work just fine(NSLog on its values before crash).
As it seems, I'm not very experienced with C-code and the subtlety behind pointers, but I would definitely be glad to hear your suggestions to make it work ! Thanks !
Heres the code :
+(char**) componentsSeparedByNewLineCEQUIV:(const char*)aChar:(int*)numWord
{ // char* aChar : my file, is typically 3 millions characters
int j=-1; //Last non space character
int i; //Scanned character
int len=strlen(aChar);
char** stringArray=malloc((*numWord)*sizeof(char*));
for (i=0;i<len; i++)
{ if (aChar[i]==10)
{
if ( j!=-1)
{
char* buffer2=malloc(i-j+1);
strcpy(buffer2, strndup(aChar+j, i-j));
stringArray[i]=malloc(sizeof(char)*strlen(buffer2)+1); //EXC_BAD_ACCESS HERE
strcpy(stringArray[i], buffer2);
}
j=-1;
}
else if (j==-1)
{j=i;}
}
if (j!=-1)
{ char* buffer2=malloc(i-j+1);
strcpy(buffer2, strndup(aChar+j, i-j));
stringArray[i]=malloc(strlen(buffer2)+1);
strcpy(stringArray[i], buffer2);
}
return stringArray;
}
You're probably not the first person to have this problem :)
Why not just use strtok?
PS What analysis showed that NSString was your problem?
I don't know why the error is at the line above of where it should be.However you are copying a string that is not allocated.
stringArray[i] is not allocated when you copy on it buffer2, allocate it:
if ( j!=-1)
{
char* buffer2=malloc(i-j+1);
strcpy(buffer2, strndup(aChar+j, i-j));
stringArray[i]=malloc(sizeof(char)*strlen(buffer2)+1); //EXC_BAD_ACCESS HERE
stringArray[i]=(char*)malloc( (strlen(buffer2)+1)*sizeof(char)); // Allocate the string
strcpy(stringArray[i], buffer2);
}
First: if Im not totaly wrong, but i think you are consuming at least 4-times as much memory as you need to:
You are using malloc for creating buffer2 and also using strndup for getting the wanted chars. strndup does exactly what you want, but in one step. char* buffer2 = strndup(aChar+j, i-j) should be your first step. Even worse in the next two line you are essential doing the same again. So i think what you are really want is stringArray[i] = strndup(aChar+j, i-j). To look at memory Problems: all the functions use errno to indicate memory allocating failure.
Second: Your functions does not return the number of components, so your stringArray may contain some garbage without knowing.
Third: strlen is expensive and you do not need it, just use for(int i = 0; aChar[i] != '\0'; i++)
Update for everyone who might be interested : this is a working version, using strtok which can be useful, although i'm still interested in response on my code.
This code have been tested 5 times faster (125ms vs 581ms) than [astring componentsSeparatedByString:#"\n"] ...
+(char**)componentsSeparatedByNewLine:(const char*)aChar:(int*)numWord
{
int i;
int j=0;
int len = strlen(aChar);
*numWord=1;
for (i=0;i<len; i++)
{
if (aChar[i]==10) *numWord=*numWord+1; //change 10 for any other character (ASCII for space)
}
char** stringArray=malloc((*numWord)*sizeof(char*));
char* pch;
char* aChar2=malloc(len+1);
strcpy(aChar2,aChar);
pch = strtok(aChar2,"\n");
while (pch != NULL)
{
stringArray[j]=(char*)malloc( (strlen(pch)+1)*sizeof(char));
strcpy(stringArray[j], pch);
//NSLog(#"%s",stringArray[j]);
j=j+1;
pch = strtok (NULL, "\n");
}
return stringArray;
}

Optimizing a Bit-Wise Manipulation Kernel

I have the following code which progressively goes through a string of bits and rearrange them into blocks of 20bytes. I'm using 32*8 blocks with 40 threads per block. However the process takes something like 36ms on my GT630M. Are there any further optimization I can do? Especially with regard to removing the if-else in the inner most loop.
__global__ void test(unsigned char *data)
{
__shared__ unsigned char dataBlock[20];
__shared__ int count;
count = 0;
unsigned char temp = 0x00;
for(count=0; count<(streamSize/8); count++)
{
for(int i=0; i<8; i++)
{
if(blockIdx.y >= i)
temp |= (*(data + threadIdx.x*(blockIdx.x + gridDim.x*(i+count)))&(0x01<<blockIdx.y))>>(blockIdx.y - i);
else
temp |= (*(data + threadIdx.x*(blockIdx.x + gridDim.x*(i+count)))&(0x01<<blockIdx.y))<<(i - blockIdx.y);
}
dataBlock[threadIdx.x] = temp;
//do something
}
}
It's not clear what your code is trying to accomplish, but a couple obvious opportunities are:
1) if possible, use 32-bit words instead of unsigned char.
2) use block sizes that are multiples of 32.
3) The conditional code may not be costing you as much as you expect. You can check by compiling with --cubin --gpu-architecture sm_xx (where xx is the SM version of your target hardware), and using cuobjdump --dump-sass on the resulting cubin file to look at the generated assembly. You may have to modify the source code to loft the common subexpression into a separate variable, and/or use the ternary operator ? : to hint to the compiler to use predication.

Realloc not expanding my array

I'm having trouble implementing realloc in a very basic way.
I'm trying to expand the region of memory at **ret, which is pointing to an array of structs
with ret = realloc(ret, newsize); and based on my debug strings I know newsize is correctly increasing over the course of the loop (going from the original size of 4 to 8 to 12 etc.), but when I do sizeof(ptr) it's still returning the original size of 4, and the things I'm trying to place into the newly allocated space can't be found (I think I've narrowed it down to realloc() which is why I'm formatting the question like this)
I can post the function in it's entirety if the problem isn't immediately evident to you, I'm just trying to not "cheat" with my homework too much (the code is kind of messy right now anyway, with heavy use of printf() for debug).
[EDIT] Alright, so based on your answers I'm failing at debugging my code, so I guess I'll post the whole function so you can tell me more about what I'm doing wrong.
(You can ignore the printf()'s since most of that is debug that isn't even working)
Booking **bookingSelectPaid(Booking **booking) {
Booking **ret = malloc(sizeof(Booking*));
printf("Initial address of ret = %p\n", ret);
size_t i = 0;
int numOfPaid = 0;
while (booking[i] != NULL)
{
if (booking[i]->paid == 1)
{
printf("Paying customer! sizeof(Booking*) = %d\n", (int)sizeof(Booking*));
++numOfPaid;
size_t newsize = sizeof(Booking*) * (numOfPaid + 1);
printf("Newsize = %d\n", (int)newsize);
Booking **temp = realloc(NULL, (size_t)newsize);
if (temp != NULL)
printf("Expansion success! => %p sizeof(new pointer) = %d ret = %p\n", temp, (int)sizeof(temp), ret);
ret = realloc(ret, newsize);
ret[i] = booking[i];
ret[i+1] = NULL;
}
++i;
printf("Sizeof(ret) = %d numOfPaid = %d\n", (int)sizeof(ret), numOfPaid);
}
return ret; }
[EDIT2] --> http://pastebin.com/xjzUBmPg
[EDIT3] Just to be clear, the printf's, the temp pointer and things of that nature are debug, and not part of the intended functionality. The line that is puzzling me is either the one with realloc(ret, newsize); or ret[i] = booking[i]
Basically I know for sure that booking contains a table of structs that ends in NULL, and I'm trying to bring the ones that have a specific value set to 1 (paid) onto the new table, which is what my main() is trying to get from this function... So where am I going wrong?
I think the problem here is that your sizeof(ptr) only returns the size of the pointer, which will depend on your architecture (you say 4, so that would mean you're running a 32-bit system).
If you allocate memory dynamically, you have to keep track of its size yourself.
Because sizeof(ptr) returns the size of the pointer, not the allocated size
Yep, sizeof(ptr) is a constant. As the other answer says, depends on the architecture. On a 32 bit architecture it will be 4 and on a 64 bit architecture it will be 8. If you need more help with questions like that this homework help web site can be great for you.
Good luck.