sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Allen's Avatar

Why does C++ program run differently on VxWorks?


Question posted by: Allen (Guest) on July 12th, 2006 06:05 AM
I wrote many classes. In a class, there is a member variable which is
declared as char szMTreeBuf[4096]. On both Windows XP and VxWorks, it
cannot work. Then I try to declare the member variable as static char
szMTreeBuf[4096], it can work again. But when add some other function
codes, it cannot run normally. I modify the class delearation, and
change szMTreeBuf member variable to be local variable. Again it can
run now.

Why does C++ program run differently on VxWorks?

OS: VxWorks 5.5.1
Memory: 16M

5 Answers Posted
werasm's Avatar
Guest - n/a Posts
#2: Re: Why does C++ program run differently on VxWorks?


Allen wrote:
Quote:
Originally Posted by
I wrote many classes. In a class, there is a member variable which is
declared as char szMTreeBuf[4096].


This would imply that each instance of the class has a szMTreeBuff
member (part of relationship). This is valid c++.
Quote:
Originally Posted by
On both Windows XP and VxWorks, it
cannot work. Then I try to declare the member variable as static char
szMTreeBuf[4096], it can work again.


Big difference - this would imply that the szMTreeBuff member is shared
between all instances of the class of which the member is a part. Also
valid c++.
Quote:
Originally Posted by
But when add some other function
codes, it cannot run normally. I modify the class delearation, and
change szMTreeBuf member variable to be local variable.


Local? (as in not a member anymore...)
Quote:
Originally Posted by
Again it can run now.
>
Why does C++ program run differently on VxWorks?


There are many reasons for this. Do you use the same platform? You
certainly use a different compiler (I'm assuming). I've found that
Windows are more leniant than VxWorks wrt. enforcing hardware traps
when one makes serious mistakes. I must mention that changing where the
buffer is stored in memory has nothing to do with your problem (I
think). This only highlights (in all probability) another problem, as
the memory layout of the program may differ drastically considering the
changes that you've made. I suggest you to induce the problem by
allocating the applicable buffer in a way that induces the problem (as
I understand you, make it part of a class instance - non-static
member): BTW. you may consider getting rid of the horrible sz prefix...
What does is stand for, size? ;-)

class x_c
{
private:
char buff_[1000]; //part of :-)
}

If this now induces the problem, good - leave it that way as it is not
the problem itself. Note that if buff is used as a string, it may
become a problem (as it is not NULL terminated). Also note that when
you copy into buff, to not over-index. You may also use a vector - but
before you do that - stick to one thing - induce the problem and try to
find it - good luck. You seem in over your head.

Werner
Quote:
Originally Posted by
>
OS: VxWorks 5.5.1
Memory: 16M


Forest's Avatar
Guest - n/a Posts
#3: Re: Why does C++ program run differently on VxWorks?

I think it depends on:
1) Where is the instance of the class created? stack or free storage?
2) If the instance is created on stack, is there enough stack size for
a process on target OS platform?
3) Is there enough memory for *static char szMTreeBuf[4096]* ? Because
this declaration leads to allocate memory on text section of a process,
after you add some function codes, these codes take some memery, in
result there maybe not enough memory to allocate 4096 chars.

Allen wrote:
Quote:
Originally Posted by
I wrote many classes. In a class, there is a member variable which is
declared as char szMTreeBuf[4096]. On both Windows XP and VxWorks, it
cannot work. Then I try to declare the member variable as static char
szMTreeBuf[4096], it can work again. But when add some other function
codes, it cannot run normally. I modify the class delearation, and
change szMTreeBuf member variable to be local variable. Again it can
run now.
>
Why does C++ program run differently on VxWorks?
>
OS: VxWorks 5.5.1
Memory: 16M


werasm's Avatar
Guest - n/a Posts
#4: Re: Why does C++ program run differently on VxWorks?


Forest wrote:
Quote:
Originally Posted by
I think it depends on:
1) Where is the instance of the class created? stack or free storage?


Good point.
Quote:
Originally Posted by
2) If the instance is created on stack, is there enough stack size for
a process on target OS platform?


Especially in vxWks...
Quote:
Originally Posted by
3) Is there enough memory for *static char szMTreeBuf[4096]* ? Because
this declaration leads to allocate memory on text section of a process,
after you add some function codes, these codes take some memery, in
result there maybe not enough memory to allocate 4096 chars.


Now that you mention - the program stops working when he allocates in
as part of an instance (and works if he creates it as part of class -
static). If he creates enough instances, he may run out of either stack
or heap, depending on where he creates the class or sequence of classes
(16M memory does seem skimpy compared to Windowzzz).

W

Jim Langston's Avatar
Guest - n/a Posts
#5: Re: Why does C++ program run differently on VxWorks?

"Allen" <chenal@naritech.cnwrote in message
news:1152680944.969884.243670@b28g2000cwb.googlegr oups.com...
Quote:
Originally Posted by
>I wrote many classes. In a class, there is a member variable which is
declared as char szMTreeBuf[4096]. On both Windows XP and VxWorks, it
cannot work. Then I try to declare the member variable as static char
szMTreeBuf[4096], it can work again. But when add some other function
codes, it cannot run normally. I modify the class delearation, and
change szMTreeBuf member variable to be local variable. Again it can
run now.
>
Why does C++ program run differently on VxWorks?
>
OS: VxWorks 5.5.1
Memory: 16M


You say it cannot run normally. What type of error do you get? Stack
overflow? Memory allocation?

One thing you might try is forcing it to use dynamic memory.

char* szMTreeBuf;

and in the constructor or initializaer
szMTreeBuf = new char[4096];

I suspect that will probably work.

It may also work the way you have it if you increase the stack size, you'll
have to look for settings for that in your compiler(s).


Allen's Avatar
Guest - n/a Posts
#6: Re: Why does C++ program run differently on VxWorks?

Forest wrote:
Quote:
Originally Posted by
I think it depends on:
1) Where is the instance of the class created? stack or free storage?


There is an only single global instance of the class.
Quote:
Originally Posted by
2) If the instance is created on stack, is there enough stack size for
a process on target OS platform?


Yes, I also think so. But Why cannot work on Windows XP? I will try to
change the
stack size in VC++ compiler.
Quote:
Originally Posted by
3) Is there enough memory for *static char szMTreeBuf[4096]* ? Because
this declaration leads to allocate memory on text section of a process,
after you add some function codes, these codes take some memery, in
result there maybe not enough memory to allocate 4096 chars.


How to adjust text section size? If I change the declaration to be
static int buffer[1024].
Will this declaration allocate memory on data section?

Jim Langston wrote:
Quote:
Originally Posted by
One thing you might try is forcing it to use dynamic memory.
>
char* szMTreeBuf;
>
and in the constructor or initializaer
szMTreeBuf = new char[4096];
>
I suspect that will probably work.


Our HW has not MMU, therefore we do not allow dynamic memory
allocation.

 
Not the answer you were looking for? Post your question . . .
196,994 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,994 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors