An integer increments strangely in c - c

Ok, here is the piece of code where I found it strange:
printf("di consumerthread : mulai isi data, itmsg = %d\n",itmsg);
msgq->data[itmsg]=rec_data;
printf("di consumerthread : selesai isi data, itmsg = %d\n",itmsg);
What I found strange is the output:
di consumerthread : mulai isi data, itmsg = 42
di consumerthread : selesai isi data, itmsg = 98
How come the itmsg variable suddenly changes from 42 to 98 after this line?
msgq->data[itmsg]=rec_data;
Can anybody please tell me how to solve this problem and the cause of this strange output?

Perhaps msgq->data[itmsg] is an alias for itmsg. It could happen. If you inherited this code run a bounds checker on it.

What is the size of the data array? Are you writing off the bounds of it?
It is possible that itmsg is being overwritten by the assignment of rec_data to msgq->data[itmsg]. If they are declared near to each other, this is very possible.

Without being able to know for sure based only on the information in your question, the problem could be multiple threads accessing the same memory location (msgq->data[itmsg]) at the same time, without any kind of proper synchronization.

Do you have two or more threads that both use the itmsg variable? There's nothing in your code snippet that would change the value of that variable, so it looks like it must be another thread that's changing it.
If you're using threads, you'll need to protect any data that they share with a mutex.

Ok, I finally found what's wrong
Sorry if I dont give you enough info because my head is in a mess to explain it in detail..
So, what I did to solve it is:
changing all type of global variable, including itmsg, to static
and all type of procedure and function which use the global variable to static
and it solved all of my problem!
though I still dont know what's static function..
Thanks for all of your help! :D

Related

Cannot plot in Matlab. It says my matrix is empty. Weird?

Doing some simple plotting and I cannot seem to figure out why I can't plot my data. I troubleshooted it first by checking to see if I had any data in my arrays first. I did not!! I'm completely lost on how to fix this.
I also tried creating a script to see if maybe it solved the issue instead of using the command window. I still get the same error.
Code:
t=0:1e-6:((2e-3-1e-6)-1);
vm=sin(2*pi*1e3*t);
vc=sin(2*pi*20e3*t);
vdsb=vc.*vm;
plot(t,vdsb,'b')
You probably don't want that last -1 in your code setting up t. Try changing t to:
t=0:1e-6:((2e-3-1e-6));
Or, if you really want it negative, you need to change the step size to negative:
t=0:-1e-6:((2e-3-1e-6)-1);
Otherwise, t is defined from 0:psotiveStep:negativeNumber and so it is empty.

Array.Clear throwing out of bounds exception

I'm having trouble with a clear statement. I've got an array that gets sized dynamically, filled, and then passed to a function to be converted to some custom objects.
After that conversion I want to clear the array. I use
Array.Clear(FileData, 0, FileData.Length)
as this thread suggests (reset-an-array-to-default-in-visual-basic). However, every time I get to that point in the script the try-catch wrapping the Clear catches an out of bounds exception on the Clear.
The array is not empty (actually it's got ~34900 items) so it's not that the array has zero length. The one thing that it might be that isn't discussed in the question I referenced above is that my array is 2 dimensional.
All that said, I'm fairly well stumped. Any help would be appreciated.
UPDATE: For those experiencing this issue, I ultimately just left the step commented out, so that instead of clearing and then setting the array to Nothing, I just set it to Nothing. Doesn't really solve the underlying issue, but (should) free up memory all the same.

getting "void value not ignored as it ought to be" in C

I started learning C in class and have started the assignment but I'm writing it one piece at a time. Currently I'm stuck because I may be doing something completely wrong but I don't know what I'm doing.
The error is pretty clear... The void type isn't something you can return / store in a variable. Instead it's a special type that means "nothing". You can't store nothing, you must ignore the return or return something.
displayCard only displays the cards, it does nothing to define them, so you shouldn't be trying to use it to create your cards. Instead, think about what a "card" actually is in your program--what, for example, do you have to pass to displayCard in order for it to know which card to display?

Passing an array of cv::Mat to a function in C

I am having this problem for a few days now and I can not find a way around it.
I'm trying to store an array of cv::Mats and the way I was doing this so far was by having a a
std::vector<cv::Mat> store_frames(32); and every time I was just copying the new frames to their location with frame.copyTo(store_frames[i]).
This works fine if it's inside main but if I want to pass it to a function and do the same thing it's not good anymore.
I did not have the same problem with simple mats. I was just declaring them globally inside a header
cv::Mat frame;
And if I had a function:
void function( cv::Mat &f1 ) {
..
f1.at<float>(10,10) += 100;
..
}
Then call the function function(frame);
All the changes inside the function would be saved. So let's say I will have something like
...
for(int i=0; i<5; i++) {
function(frame);
std::cout<<frame.at<float>(10,10)<<' ';
}
The output (assuming the initial value of frame.at(10,10) would be
100 200 300 400 500
The problem now is that I can not do the same thing with std::vector store_frames . First of all if I try to declare it in the header the same way std::vector<cv::Mat> store_frames(32); I get an error on number 32 saying Error: expected a type specifier. And if I just say std::vector<cv::Mat> store_frames; then it won't know it's size. I saw somewhere people calling store_frames.resize I've tried to do that, but I get an error saying this declaration has no storage class or type specifier. I've looked up the internet for solutions, but didn't find any. I know the question itself might sound stupid, but I'm new to this and I don't know much.
So my questions is how do I declare this vector so that it would be possible to pass it to a function, modify it inside and the modifications would be saved?
Thank you!
Try
declared it std::vector<cv::Mat> store_frame; in the header and store_frame.resize(32); in the source code.
use std::vector<cv::Mat> store_frames(32); in the source code.
ps: I just added this as the comment. :)

Arrays in PowerBuilder

I have this code
n_userobject inv_userobject[]
For i = 1 to dw_1.Rowcount()
inv_userobject[i] = create n_userobject
.
.
.
NEXT
dw_1.rowcount() returns only 210 rows. Its so odd that in the range of 170 up, the application stop and crashes on inv_userobject[i] = create n_userobject.
My question, is there any limit on array or userobject declaration using arrays?
I already try destroying it after the loop so as to check if that will be a possible solution, but it is still crashing.
Or how can i be able to somehow refresh the userobject?
Or is there anyone out there encounter this?
Thanks for all your help.
First, your memory problem. You're definitely not running into an array limit. If I was to take a guess, one of the instance variables in n_userobject isn't being cleaned up properly (i.e. pointing to a class that isn't being destroyed when the parent class is destroyed) or pointing to a class that similarly doesn't clean itself up. If you've got PB Enterprise, I'd do a profiling trace with a smaller loop and see what is being garbage collected (there's a utility called CDMatch that really helps this process).
Secondly, let's face it, you're just doing this to avoid writing a reset method. Even if you get this functional, it will never be as efficient as writing your own reset method and reusing the same instance over again. Yes, it's another method you'll have to maintain whenever the instance variable list changes or the defaults change, but you'll easily gain that back in performance.
Good luck,
Terry.
I'm assuming the crash you're facing is at the PBVM level, and not a regular PB exception (which you can catch in your code). If I'm wrong, please add the exception details.
A loop of 170-210 iterations really isn't a large one. However, crashes within loops are usually the result of resource exhaustion. What we usually do in long loops is call GarbageCollect() occasionally. How often should it be called depends on what your code does - using it frequently could allow the use of less memory, but it will slow down the run. Read this for more.
If this doesn't help, make sure the error does not come from some non-PB code (imported DLL or so). You can check the stack trace during the crash to see the exception's origin.
Lastly, if you're supported by Sybase (or a local representative), you can send them a crash dump. They can analyze it, and see if it's a bug in PB, and if so, let you know when it was (or will be) fixed.
What I would normally do with a DataWindow is to create an object that processes the data in a row and call it for each row.
the only suggestion i have for this is to remove the rowcount from the for (For i = 1 to dw_1.Rowcount()) this will cause the code to recount the rows every time it uses one. get the count into a variable and then use the variable. it should run a bit better and be far more easy to debug.

Resources