Below is the code which was written VC++ 6.0 under windows environment.
Executing the same throws:
------------------
Debug Error!
Program: ccheck.exe
DAMAGE: after normal block (#41) at 0x00300160
(Press Retry to debug the application)
------------------
While the free () statement is commented the program does not report the error.
Please do let me know your views ...
Thanks in advance.
# include <string.h>
# include <stdio.h>
# include <stdlib.h>
# include <errno.h>
# define _DEBUG_ 1
void SetProgramName (char *);
char * GetProgramName ();
void PrintUsage ();
void PrintErrMsg (size_t);
void ClearMemory ();
char *ProgramName = NULL;
extern errno;
int
main (int argc, char *argv[])
{
SetProgramName (argv[0]);
if (argc < 2)
{
PrintUsage ();
ClearMemory ();
}
return (EXIT_SUCCESS);
}
void
SetProgramName (char *PrgName)
{
ProgramName = (char *) malloc (strlen (PrgName));
if (ProgramName == NULL)
{
PrintErrMsg (errno);
}
else
{
(void) strcpy (ProgramName, PrgName);
#if defined (_DEBUG_)
(void) fprintf (stderr, "\n Debug: ProgramName %s \n", ProgramName);
#endif
}
}
char *
GetProgramName ()
{
return (ProgramName == NULL ? NULL : ProgramName);
}
void
ClearMemory ()
{
if (strlen (ProgramName) > 0)
{
free (ProgramName);
#if 0
ProgramName = NULL;
#endif
PrintErrMsg (errno);
}
}
void
PrintErrMsg (size_t ErrNumber)
{
(void) fprintf ( stderr, \
"\n Err Number [%ld] \n Err Msg [%s] \n", \
ErrNumber, strerror (ErrNumber)
);
}
void
PrintUsage ()
{
(void) fprintf (stderr, "\n %s <> <>", GetProgramName( ));
}
---
Thanks,
Anu 6 5540
Anuradha wrote: Below is the code which was written VC++ 6.0 under windows environment.
Executing the same throws: ------------------ Debug Error! Program: ccheck.exe DAMAGE: after normal block (#41) at 0x00300160
(Press Retry to debug the application) ------------------
While the free () statement is commented the program does not report the error.
Please do let me know your views ...
[snip]
void SetProgramName (char *PrgName) {
ProgramName = (char *) malloc (strlen (PrgName));
ProgramName = malloc(strlen(P rgName) + 1);
boa
[snip]
> ProgramName = (char *) malloc (strlen (PrgName));
You are not allocating enough memory here. if (ProgramName == NULL) { PrintErrMsg (errno); } else { (void) strcpy (ProgramName, PrgName);
And here you run over memory beyond what you allocated.
Gordon L. Burditt
Anuradha wrote: # include <string.h> # include <stdio.h> # include <stdlib.h> # include <errno.h>
# define _DEBUG_ 1
void SetProgramName (char *); char * GetProgramName (); void PrintUsage ();
Better:
char * GetProgramName (void);
void PrintUsage (void);
void PrintErrMsg (size_t); void ClearMemory ();
Same here.
char *ProgramName = NULL;
extern errno;
It would be a good idea to tell your program about the type of errno.
int main (int argc, char *argv[]) { SetProgramName (argv[0]);
if (argc < 2) { PrintUsage (); ClearMemory (); }
return (EXIT_SUCCESS); }
void SetProgramName (char *PrgName) {
ProgramName = (char *) malloc (strlen (PrgName));
You need
ProgramName = malloc (strlen (PrgName) + 1);
because the '\0' character is not counted by strlen(). if (ProgramName == NULL) { PrintErrMsg (errno);
This is kind of silly, since errno is a global variable anyway.
} else { (void) strcpy (ProgramName, PrgName);
#if defined (_DEBUG_) (void) fprintf (stderr, "\n Debug: ProgramName %s \n", ProgramName); #endif
} }
char * GetProgramName () { return (ProgramName == NULL ? NULL : ProgramName);
This is equivalent to
return ProgramName;
}
void ClearMemory () { if (strlen (ProgramName) > 0)
This is not the way to test whether the string allocation was successful.
You want
if (ProgramName != NULL)
{ free (ProgramName); #if 0 ProgramName = NULL; #endif PrintErrMsg (errno); } }
By the way: You don't need to copy argv[0] at all, since it exists until the
end of the program.
Christian
On Thu, 26 Aug 2004 08:53:01 +0200
Christian Kandeler <ch************ ****@hob.de> wrote: Anuradha wrote:
# include <string.h> # include <stdio.h> # include <stdlib.h> # include <errno.h>
<snip>
extern errno;
It would be a good idea to tell your program about the type of errno.
No it isn't, it's better to delete the "extern errno;" entirely. errno
is declared appropriately by errno.h
<snip> if (ProgramName == NULL) { PrintErrMsg (errno);
This is kind of silly, since errno is a global variable anyway.
It is silly.
However, errno might not be a global variable, it could be a macro that
expands to a modifiable lvalue :-)
--
Flash Gordon
Pedantic to the last, except I sometimes get it wrong.
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
>> if (ProgramName == NULL) { PrintErrMsg (errno);
This is kind of silly, since errno is a global variable anyway.
No, it's *NOT* silly. It's perfectly possible and reasonable to
pass something else (for example, a saved version of errno from
before you tried opening the log file to put the error message in).
Being forced to save and restore errno is a pain, and so many things
MIGHT mess it up (such as opening log files, and even printf()),
it's often necessary. With a function taking an argument, you're
still sometimes stuck with saving errno, but usually not with
restoring it.
It's not that unusual to have a function return an error code or 0
to indicate success. I see that a lot inside kernels. There, you
want to deal with what the kernel returned, not errno.
Why does strerror() take an argument rather than use errno? Same
issue.
Gordon L. Burditt
Anuradha wrote: return (ProgramName == NULL ? NULL : ProgramName);
return ProgramName;
Not an error, though... See the other replys for that.
-- Thomas This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alfonso Morra |
last post by:
Hi,
I am at the end of my tether now - after spending several days trying to
figure how to do this. I have finally written a simple "proof of
concept" program to test serializing a structure containing pointers
into a "flattened" bit stream.
Here is my code (it dosen't work - compiles fine, pack appears to work,
but unpack retrieves jibberish and causes program to crash).
|
by: Hongzheng Wang |
last post by:
Hi, everyone
I have a problem about malloc/free function.
Does malloc add size information to program? And, when free function is
called, how this function get the size information?
That is, if I request a memory block of size 10, where the size
information 10 is stored?
To be clear, I have such codes below:
int *p = (int *) malloc(10*sizeof(int));
/* ...... */
|
by: Suyog_Linux |
last post by:
I wish to know how the free()function knows how much memory to be
freed as we only give pointer to allocated memory as an argument to
free(). Does system use an internal variable to store allocated memory
when we use malloc().
Plz help......
|
by: Joris Adriaenssens |
last post by:
This is my first posting, please excuse me if it is off-topic.
I'm learning to program in C. It's been almost ten years I've been
programming and a lot of things have changed apparently.
I understand from other postings that casting a result from malloc
isn't good. In the past I have always been casting the malloc. I
think it was even necessary. (But that's a long time ago, I hadn't
heard of a standard for C these days). Was it...
|
by: Zheng Da |
last post by:
Program received signal SIGSEGV, Segmentation fault.
0x40093343 in _int_malloc () from /lib/tls/libc.so.6
(gdb) bt
#0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6
#1 0x40094c54 in malloc () from /lib/tls/libc.so.6
It's really strange; I just call malloc() like "tmp=malloc(size);"
the system gives me Segmentation fault
I want to write a code to do like a dynamic array, and the code is as
| |
by: jacob navia |
last post by:
In the C tutorial for lcc-win32, I have a small chapter
about a debugging implementation of malloc.
Here is the code, and the explanations that go with it.
I would appreciate your feedback both about the code and
the associated explanations.
---------------------------------------------------------------------
|
by: Raman |
last post by:
Hi All,
Here is a small Code,
int main(void)
{
char *p=(char *) malloc(100);
strcpy(p,"Test1234567890");
p=p+10;
free(p);
|
by: Louis B. (ldb) |
last post by:
I have a long running program that eventually crashes when valloc()
returns a 0. This program is relatively non-trivial as it's written in
Ada, is multithreaded, has alot of SSE routines. A memory leak would
be the most obvious cause but this appears to be more sinister then a
simple memory leak.
After alot of running around and searching through the code I found an
anomaly that I'd like to explain and understand if it's the cause of...
|
by: anyone.anon |
last post by:
Let p=malloc(N) for some N>0. As far as I understand it, free(p+k) for
0<k<N causes undefined behavior, since only a pointer returned by (m|
re|c)alloc() can validly be passed to free().
This seems pretty silly. Wouldn't a better behavior of free() be to
assume that if it receives q, where q lies in some malloc()ated-and-
not-yet-free()d block starting at p, then it should interpret this as
free(p)?
Even better, the *alloc() routines...
|
by: desktop |
last post by:
I have read in Bjarne Stroustrup that using malloc and free should be
avoided in C++ because they deal with uninitialized memory and one
should instead use new and delete.
But why is that a problem? I cannot see why using malloc instead of new
does not give the same result.
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |