Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ VC10.0
Hello everyone
Visual studio adds guard bytes before and after local variables to detect buffer overruns, very usefull, but I want to disable it for some simple and short functions which are used so many times. for example, In this function
int get_first_char( char * buf, size_t len )
{
  return len > 0 ? buf[0] : -1;
}
this function is only an example, please do not discuss it's usefullness.
I want visual studio in debug mode not to add guard bytes to stack when this function is called.
Is there any way to do this?
 
EDIT: building in release mode does not add guard bytes. I want to disable guard bytes for some simple functions only in debug mode, because testing my application in debug mode slows down significantly if that functions are called so many times.
 
thanks in advance
mr.abzadeh
Posted 18-Dec-12 12:58pm
Edited 18-Dec-12 13:26pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

In VS 2010, open the project property pages, go to C/C++ -> Code Generation. There is an option called "Buffer Security Check" (/GS) which may be what you're talking about. You can disable it right there, or add "/GS-" on the command line to disable it.
 
P.S.: the help page on /GS offers additional information, among other things specific circumstances under which the compiler does not extend the buffers, e. g. when the first line of a function is inline assembly, or when it hs the keyword naked:
 
__declspec( naked ) int func( formal_parameters ) {}
Check out naked in the help pages for more info.
  Permalink  
v2
Comments
Pascal-78 at 19-Dec-12 11:34am
   
"naked" will remove a lot of thing, like
the possibility to use C++ object with potential thrown exception (error C3068)
the possibility to initialize a variable at declaration (int a=1;) (error C2489)
may be _forceinline is a better choice.
mr.abzadeh at 21-Dec-12 18:16pm
   
/GS- does not remove guard bytes.
__declspec( naked ) does remove guard bytes, and It is a solution. but the epilog and prolog code for creating and removing stack frame will be left to programmer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The only solution I could think of is to build it in Release mode. You can still debug it that way, but it will make some optimizations that Debug builds do not (for example, you may not always be able to hit break points on lines that declare variables, because the variable may be eliminated).
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I tend to believe that the option is global if activated. That is it will apply to all memory allocations.
 
Are you doing a lot of memory allocation for performance to be a problem?
 
Also, are you sure that the penality is related to guard bytes ?
  Permalink  
Comments
mr.abzadeh at 19-Dec-12 6:15am
   
Thanks for your reply.
I do not allocate a lot of memory, and the guard bytes added by malloc is not important for me. The guard bytes added to stack to protect local variable is important to me, and the performance penalti is because of it because I call that functions in deeper loops so many times. I can avoid calling that simple functions and write it's code in the calling function, but this affects readability of my code, and readablity is very important to me. In general, I think there is no need to insert guard bytes to stack if a function does not use local variables, does not call functions and does not modify it's arguments.
mr.abzadeh at 19-Dec-12 6:24am
   
Surely I do not want to disable guard bytes globally, but I wish to know how to disable them globally, If possible.
mr.abzadeh
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

May be you can use the pragma directive check_stack to turn off stack probes.
#pragma check_stack(off)
int get_first_char( char * buf, size_t len )
{
  return len > 0 ? buf[0] : -1;
}
  Permalink  
Comments
mr.abzadeh at 21-Dec-12 18:09pm
   
I examined check_stack pragma, and it does not affect guard bytes
Pascal-78 at 22-Dec-12 12:48pm
   
After a search on the net, it seems this pragma is useless in debug-mode because overridden by a compiler option.
I tried modifying some compilers options and it seems that your problem can be solved with removing the /RTCs, /RTC1 (/RTCs + /RTCu) this option is available in "C/C++ - Code Generation" properties of your projet.
mr.abzadeh at 22-Dec-12 16:14pm
   
As I understood, /RTCu and /RTCs generate extra code to capture bugs such as using uninitialized variables, detection of buffer underrun and overrund, and so on. What I need is to remove guard bytes only in a few functions (not module) in debug mode, and I Think the only solution is
declaring these few functions as __declspec(naked), which removes guard bytes completely, but need some low level programming, as creating and removing stack frame( allocating and destroying local variables ) is left to the programmer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,631
1 Sergey Alexandrovich Kryukov 6,345
2 CPallini 5,230
3 George Jonsson 3,574
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 19 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100