473,395 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Assigning __FILE__ to a char* gives warning

As part of a very simple memory leak detector, I am trying to store
the value of __FILE__ in a char*. Since gcc4.2 I get the following
warning...

warning: deprecated conversion from string constant to 'char*'
>From what I understand about __FILE__ it returns a const char[]
object. The macro of delete assigns this to a global char* which is
used to track where deletions were made. Something like:

#define delete delete_FILE_ = __FILE__, \
delete_LINE_ = __LINE__, \
delete

Removing the delete_FILE_ = __FILE__ line stops the error. How do I
do this properly?

Jul 8 '07 #1
4 3871
al*********@gmail.com wrote:
As part of a very simple memory leak detector, I am trying to store
the value of __FILE__ in a char*. Since gcc4.2 I get the following
warning...

warning: deprecated conversion from string constant to 'char*'

From what I understand about __FILE__ it returns a const char[]
object. The macro of delete assigns this to a global char* which is
used to track where deletions were made. How do I do this properly?
Use a const char* instead of a char*.
Jul 8 '07 #2
Thanks, that fixed the problem.

Jul 8 '07 #3
al*********@gmail.com wrote:
As part of a very simple memory leak detector, I am trying to store
the value of __FILE__ in a char*. Since gcc4.2 I get the following
warning...

warning: deprecated conversion from string constant to 'char*'
From what I understand about __FILE__ it returns a const char[]
object. The macro of delete assigns this to a global char* which is
used to track where deletions were made. Something like:

#define delete delete_FILE_ = __FILE__, \
delete_LINE_ = __LINE__, \
delete

Removing the delete_FILE_ = __FILE__ line stops the error. How do I
do this properly?
I have a question about the above... I also have code that redefines new
and delete for leak detection, but I'm not really sure if redefining
keywords like that is appropriate. Is it allowed by the standard?

My code looks something like this:

void* operator new( std::size_t size, const char* fileName, int line );
void* operator new[]( std::size_t size, const char* fileName, int line );

#define DEBUG_NEW new(__FILE__, __LINE__)
#define new DEBUG_NEW

void operator delete( void* address ) throw();
void operator delete[]( void* address ) throw();
Jul 8 '07 #4
"Alf P. Steinbach" <al***@start.nowrote:
My code looks something like this:

void* operator new( std::size_t size, const char* fileName, int line );
void* operator new[]( std::size_t size, const char* fileName, int line );

#define DEBUG_NEW new(__FILE__, __LINE__)
#define new DEBUG_NEW

void operator delete( void* address ) throw();
void operator delete[]( void* address ) throw();

With a standard-conforming compiler this will reproduce Microsoft's
problem with MFC, that is, leaks when constructors throw.
The system I use the above in, doesn't have exceptions (constructors
can't throw) so I haven't had the problem.

As I understand it, I should provide matching delete functions to avoid
the problem you are talking about:

void operator delete( void* address, const char*, int ) throw();
void operator delete[]( void* address, const char*, int ) throw();

Correct?
Bug general advice: don't do use things you don't understand, at the
architecture level where it affects all code.

Instead, use proven techniques, such as smart pointers, and tools, such
as e.g. ValGrind. I've never needed to use it myself, so I don't know
from first-hand experience how effective it is at detecting a sloppy
programmer's mess. But as I understand it, for those who prefer to use
days and weeks to fix up their mess afterwards, plus ditto extra time
for any maintainance, instead of minutes or hours Doing It Right in the
first place, ValGrind & friends are indispensable tools and work OK.
Well, in my case, it's me fixing other peoples messes, but I understand
your point.
Jul 8 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

74
by: Peter | last post by:
Hi, So many times, I have seen compile warning: "you used a char* without initilize it", probably on the code like this: ------------ char* ptr; func(..., ptr); ----------
3
by: Senthilraja | last post by:
I am able to compile the following code and get "hello" as ouput everytime I execute it:- #include <stdio.h> int main (void) { char *str; str="hello"; puts(str); return 0;
5
by: jake1138 | last post by:
I couldn't find an example of this anywhere so I post it in the hope that someone finds it useful. I believe this is compiler specific (I'm using gcc), as C99 defines __VA_ARGS__. Comments are...
10
by: Steve Pope | last post by:
The first of the following functions compiles, the second gives what I think is a spurious error: "cannot convert `const char' to `char *' in assignment". void foo(int m) { char *str; if (m...
13
by: Eugene Rice | last post by:
I'm writing C code for an Atmel AVR micro controller. Because RAM space is extremely limited (about 500 bytes) I use char variables wherever I can. For example I use chars as array indices in...
5
by: Neo | last post by:
Hie, Can I put __FILE__ and __LINE__ macros inside the class function which may not be inline. And void function() { classobject::LogInfo(...); } Internally LogInfo should log file name...
1
by: Amit_Basnak | last post by:
Dear Friend which one is the better way of assigning memory and releasing it //WF_STRUCT_WORKUNIT is a Structure WF_STRUCT_WORKUNIT *p_str_Workunit = (WF_STRUCT_WORKUNIT *) new char ; if...
9
by: Peithon | last post by:
Hi, This is a very simple question but I couldn't find it in your FAQ. I'm using VC++ and compiling a C program, using the /TC flag. I've got a function for comparing two strings int...
13
by: Andreas Eibach | last post by:
Hi, let's say I have this: #include <string.h> #define BLAH "foo" Later on, I do this:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.