vc++ 6.0 extern values in global.c/global.h file gets corrupted after use

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • greenknt
    New Member
    • Apr 2009
    • 2

    vc++ 6.0 extern values in global.c/global.h file gets corrupted after use

    Hi All,

    Ok here we go;

    I have a global.h file with a extern char glb_holder[50];
    + global.c with char glb_holder[50];

    now I have ;
    (one.c)
    Code:
    global.h
    
    void doWork (void) {
              unsigned char data[30];
                
                 //   data gets value
                
               memcpy(glb_holder, data, 30);  
    }
    then
    (two.c)
    Code:
    global.h
    
    void showWork (void) {
              
      //shows or prints glb_holder and all is ok
     
    }
    but now when I try to use it again...
    it would 3/5 times get corrupted/ mem re-allocated

    Should I make holder a pointer? Its almost like this problem ..

    <url removed see posting guidelines>

    Also in my app I can't use ANY classes, so all my .c files have functions in them.

    Really any feedback would be appreciated, plus if I left out some vital stuff feel free to ask me for it.

    Thanks in advance

    John
    Last edited by Banfa; Apr 3 '09, 12:42 PM. Reason: Removed URL
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:
    Code:
    extern char glb_holder[50];
    defines glb_holder as a pointer to and array of 50 char. You usually have this when the array is two dimensional. That is, you have an array of several 50 char arrays. Like this:
    Code:
    char array[5][50];
    If you have a one dimensional array, then glb_holder should be a char*.

    This is due to the name of an array being the address of element 0.

    Next, I see you copy 30 bytes to a 50 byte array. How does that work? That is, howe do you know your global array only has 30 initialized bytes instead of 50 initilized bytes?

    You need to a) copy 50 bytes bytes every time so you know the array is completely initialized or you need to keep a second variable that tells you how many elements are currently considered active.

    Lastly, you should not be using global variables in your code. Read this: http://bytes.com/topic/c/insights/73...obal-variables

    Comment

    • greenknt
      New Member
      • Apr 2009
      • 2

      #3
      Hi, thanks for the reply.

      I did use pointers in the end.

      Also the reason I am using global values is that as I am programming hardware....
      There are some values that I would need to use over and over again.

      Thanks

      green.

      Comment

      Working...