473,379 Members | 1,123 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,379 software developers and data experts.

malloc->free->damage: after normal block

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
Nov 14 '05 #1
6 5515
boa
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(PrgName) + 1);
boa

[snip]
Nov 14 '05 #2
> 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
Nov 14 '05 #3
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
Nov 14 '05 #4
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.
Nov 14 '05 #5
>>
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
Nov 14 '05 #6
Anuradha wrote:
return (ProgramName == NULL ? NULL : ProgramName);

return ProgramName;

Not an error, though... See the other replys for that.

-- Thomas
Nov 14 '05 #7

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

Similar topics

16
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...
5
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...
74
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...
42
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...
3
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...
41
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...
171
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);
7
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...
17
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...
71
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.