forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Access Violation in C++, address variable changes

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> 64-bit
View previous topic :: View next topic  
Author Message
Ryan



Joined: 25 Feb 2016
Posts: 95

PostPosted: Tue Sep 29, 2020 9:14 pm    Post subject: Access Violation in C++, address variable changes Reply with quote

An odd bug where a guard clause around a loop causes the code work. Without it we get an access violation. I did not report this when I first found it as I managed to work around it using the below but it has now cropped up somewhere else in the same codebase and the same workaround can be used.

I did output (to a message box) the address and value of the variable to verify this. This will be a terrible one to isolate as the codebase is large and not well formed. I mention it in case it rings any bells as it would be good to get to the bottom of.

Code:

  /* Don't remove the following conditional, it works around an issue in 64 bit.
  ** If you check the address of the_number before this loop and then after
  ** this loop, it changes. It gets restored on exit of the routine though.
  ** Placing this conditional here gets the compiler to not do whatever it was
  ** doing and the address stays the same, meaning no crashes.
  */
  if (the_number > 0)
  {
    for(int i = 0; i <the_number; i++) {
      the_elements_backup[i] = the_elements[i];
    }
  }


Happy to share more info if you are interested.
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 6554
Location: Salford, UK

PostPosted: Wed Sep 30, 2020 10:11 am    Post subject: Reply with quote

Ryan

Thank you for the feedback. I can't see anything wrong with the assembly listing for my test program. Is the_number a simple local variable?

Here is my test code...
Code:
  #include <stdio.h>
  int main()
  {
     int the_number = 0;
     int the_elements_backup[4], the_elements[4];
     int* p = &the_number;
     for(int i = 0; i < the_number; i++)
     {
        the_elements_backup[i] = the_elements[i];
     }
     printf("%p %p\n", p, &the_number);
     return 0;
  }
Back to top
View user's profile Send private message AIM Address
Ryan



Joined: 25 Feb 2016
Posts: 95

PostPosted: Wed Sep 30, 2020 10:55 am    Post subject: Reply with quote

Nothing so simple, it's a Template (below, reduced for simplicity).

Code:

template <class Type>
class database {
public:
  database(int);
  int the_number;
private:
  Type *the_elements;
  Type *the_elements_backup;
};
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 6554
Location: Salford, UK

PostPosted: Wed Sep 30, 2020 11:55 am    Post subject: Reply with quote

Ryan

My understanding is that no attempt has yet been made to get C++ templates to work for 64 bits.
Back to top
View user's profile Send private message AIM Address
Ryan



Joined: 25 Feb 2016
Posts: 95

PostPosted: Wed Sep 30, 2020 12:14 pm    Post subject: Reply with quote

Understood. They work, but old code so I'm loathe to touch them. I'll see if there is an alternate approach to templates.

Thanks Paul
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> 64-bit All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group