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

realloc() / free() BYTE* immediately after malloc() fails

Hi,

I'm having an odd problem with a project using VisualStudio 2005 Prof.

When I execute the program below I get this message:

Debug Assertion Failed
Program: ...
File: dbgheap.c
Line: 1252
Expression: _CrtIsValidHeapPointer(pUserData)

---------- START ----------
#include "stdafx.h"

#include <iostream>
using namespace std;

int main( int argc, char * argv[] )
{
unsigned long lU_tile_size = 2956;
// read the tile data (jpeg file) into a buffer
unsigned char * byP_jpeg_file = (unsigned char *)
malloc( lU_tile_size );
if( byP_jpeg_file == NULL )
{
wcout << L"eek - allocating memory failed";
return 1;
}

// PROBLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE M
realloc( byP_jpeg_file , 0 );

return 0;
}
---------- END----------

VS2005 settings:
win32 command line executable
ATL is linked statically
Multithreaded-Debug (/MTd)
runtime checking: both
optimisation is turned off (/Od)

Please help!

~ Attila

Jun 6 '07 #1
14 4303
addition:

this
------------------------------------------------------------------
int i_error;
errno = 0; // reset
free( byP_jpeg_file );
if( errno =! 0 )
{
wchar_t wct_error[256];
i_error = _wcserror_s( wct_error , 256 , errno );
if( i_error != 0 )
{
wcout << L"_wcserror_s() failed";
return 3;
}
wcout << wct_error;
return 2;
}
------------------------------------------------------------------
instead of realloc() makes the program print

Operation not permitted.
means: errno == 1 == EPERM

Jun 6 '07 #2
attibln wrote:
Hi,

I'm having an odd problem with a project using VisualStudio 2005 Prof.

When I execute the program below I get this message:

Debug Assertion Failed
Program: ...
File: dbgheap.c
Line: 1252
Expression: _CrtIsValidHeapPointer(pUserData)
Consider asking questions about debugging in VC++ in the newsgroup
dedicated to VC++. Take a look at "microsoft.public.vc.*" hierarchy.
>
---------- START ----------
#include "stdafx.h"
That's a non-standard header. Consider removing it (at least when
posting the code here).
>
#include <iostream>
using namespace std;

int main( int argc, char * argv[] )
You don't seem to be using 'argc' or 'argv'. Why declare them?
{
unsigned long lU_tile_size = 2956;
// read the tile data (jpeg file) into a buffer
unsigned char * byP_jpeg_file = (unsigned char *)
malloc( lU_tile_size );
'malloc' is undefined. Consider including <cstdlib>.
if( byP_jpeg_file == NULL )
{
wcout << L"eek - allocating memory failed";
return 1;
}

// PROBLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE M
realloc( byP_jpeg_file , 0 );
'realloc' is undefined. Consider including <cstdlib>.

The behaviour of 'realloc' with 'size' passed as 0 is implementation
defined. You should consider asking in the newsgroup for your C++
compiler (see above).
>
return 0;
}
---------- END----------

VS2005 settings:
win32 command line executable
ATL is linked statically
Multithreaded-Debug (/MTd)
runtime checking: both
optimisation is turned off (/Od)

Please help!

~ Attila
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 6 '07 #3
On 2007-06-06 16:03, attibln wrote:
Hi,

I'm having an odd problem with a project using VisualStudio 2005 Prof.

When I execute the program below I get this message:

Debug Assertion Failed
Program: ...
File: dbgheap.c
Line: 1252
Expression: _CrtIsValidHeapPointer(pUserData)

---------- START ----------
#include "stdafx.h"

#include <iostream>
using namespace std;

int main( int argc, char * argv[] )
{
unsigned long lU_tile_size = 2956;
// read the tile data (jpeg file) into a buffer
unsigned char * byP_jpeg_file = (unsigned char *)
malloc( lU_tile_size );
if( byP_jpeg_file == NULL )
{
wcout << L"eek - allocating memory failed";
return 1;
}

// PROBLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE M
realloc( byP_jpeg_file , 0 );

return 0;
}
---------- END----------

VS2005 settings:
win32 command line executable
ATL is linked statically
Multithreaded-Debug (/MTd)
runtime checking: both
optimisation is turned off (/Od)

Please help!
Either you have some weird settings somewhere else (why do you link ATL
statically when you don't use it?) or you have not shown us all the
code. This I surmise from the fact that it compiles and runs perfectly
fine for me on the same version of VS.

--
Erik Wikström
Jun 6 '07 #4
attibln wrote:
addition:

this
------------------------------------------------------------------
int i_error;
errno = 0; // reset
free( byP_jpeg_file );
if( errno =! 0 )
:-)

You just wrote here

if (errno = !0)

which is the same as

if ((errno = 1) != 0)

.. Why are you surprised? The equality operator you're supposed to
use is spelled "not equal" or "!=". Fix it.
{
wchar_t wct_error[256];
i_error = _wcserror_s( wct_error , 256 , errno );
if( i_error != 0 )
{
wcout << L"_wcserror_s() failed";
return 3;
}
wcout << wct_error;
return 2;
}
------------------------------------------------------------------
instead of realloc() makes the program print

Operation not permitted.
means: errno == 1 == EPERM
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 6 '07 #5

"attibln" <At*****@gmx.netwrote in message
news:11**********************@q69g2000hsb.googlegr oups.com...
Hi,

I'm having an odd problem with a project using VisualStudio 2005 Prof.

When I execute the program below I get this message:

Debug Assertion Failed
Program: ...
File: dbgheap.c
Line: 1252
Expression: _CrtIsValidHeapPointer(pUserData)

---------- START ----------
#include "stdafx.h"

#include <iostream>
using namespace std;

int main( int argc, char * argv[] )
{
unsigned long lU_tile_size = 2956;
// read the tile data (jpeg file) into a buffer
unsigned char * byP_jpeg_file = (unsigned char *)
malloc( lU_tile_size );
if( byP_jpeg_file == NULL )
{
wcout << L"eek - allocating memory failed";
return 1;
}

// PROBLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE M
realloc( byP_jpeg_file , 0 );

return 0;
}
---------- END----------

VS2005 settings:
win32 command line executable
ATL is linked statically
Multithreaded-Debug (/MTd)
runtime checking: both
optimisation is turned off (/Od)
Your debugger is broken. there are not 1252 lines in this code.
Also, you don't assign the result of realloc to anything.
--
Fred L. Kleinschmidt
Jun 6 '07 #6
On 6 Jun., 16:25, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Consider asking questions about debugging in VC++ in the newsgroup
dedicated to VC++. Take a look at "microsoft.public.vc.*" hierarchy.
ok, sry
int main( int argc, char * argv[] )

You don't seem to be using 'argc' or 'argv'. Why declare them?
That was just an example which contains the code where the problem
occurs. The example runs fine - so the problem must be connected with
the other stuff I do in the (real) project I'm working in. But I have
no idea what that could be. :(
'malloc' is undefined. Consider including <cstdlib>.
'realloc' is undefined. Consider including <cstdlib>.
these are probably included by #include "stdafx.h"
The behaviour of 'realloc' with 'size' passed as 0 is implementation
defined. You should consider asking in the newsgroup for your C++
compiler (see above).
ok

Jun 6 '07 #7
On 6 Jun., 16:31, Erik Wikström <Erik-wikst...@telia.comwrote:
>
Either you have some weird settings somewhere else
(why do you link ATL statically when you don't use it?)
or you have not shown us all the code.
That's right, the source is about 179 KB big - 11 header and 11 cpp
files plus self-made libraries.
This I surmise from the fact that it compiles and runs perfectly
fine for me on the same version of VS.
Jun 6 '07 #8
On 6 Jun., 16:37, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
attibln wrote:
addition:
this
------------------------------------------------------------------
int i_error;
errno = 0; // reset
free( byP_jpeg_file );
if( errno =! 0 )

:-)

You just wrote here

if (errno = !0)
Ouch, that was a (bad) typo.

Thanks Victor!
Thanks everyone! :)

Jun 6 '07 #9
On 6 Jun., 16:37, "Fred Kleinschmidt"
<fred.l.kleinmschm...@boeing.comwrote:
>
Your debugger is broken. there are not 1252 lines in this code.
File: dbgheap.c
Line: 1252

The line information refers to dbgheap.c which is part of the C
RunTime library VisualStudio uses.
Also, you don't assign the result of realloc to anything.
That's right - my fault again. Now doing this:

byP_jpeg_file = (BYTE *) realloc( byP_jpeg_file , 0 );

results in:

Debug Assertion Failed!
Program: ...
File: fread.c
Line: 93
Expression: (buffer != NULL)

that line (93) is
_VALIDATE_RETURN((buffer != NULL), EINVAL, 0);

Jun 6 '07 #10
attibln wrote:
On 6 Jun., 16:37, "Fred Kleinschmidt"
<fred.l.kleinmschm...@boeing.comwrote:
>>
Your debugger is broken. there are not 1252 lines in this code.

File: dbgheap.c
Line: 1252

The line information refers to dbgheap.c which is part of the C
RunTime library VisualStudio uses.
>Also, you don't assign the result of realloc to anything.

That's right - my fault again. Now doing this:

byP_jpeg_file = (BYTE *) realloc( byP_jpeg_file , 0 );

results in:

Debug Assertion Failed!
Program: ...
File: fread.c
Line: 93
Expression: (buffer != NULL)

that line (93) is
_VALIDATE_RETURN((buffer != NULL), EINVAL, 0);
That's a good one! I didn't even see any 'fread' in your code.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 6 '07 #11
On 6 Jun., 19:24, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>
Debug Assertion Failed!
Program: ...
File: fread.c
Line: 93
Expression: (buffer != NULL)
that line (93) is
_VALIDATE_RETURN((buffer != NULL), EINVAL, 0);

That's a good one! I didn't even see any 'fread' in your code.
fread.c is probably included by stdafx.h or <iostreamor VisualStudio
automatically.

Anyway, I wanted the buffer to be freed - which only didn't work
because of my typo.

(Fyi: When the free() didn't work because of the typo I tried to
deallocate the space by making it 0 bytes big.)

Jun 6 '07 #12
attibln wrote:
[..]
(Fyi: When the free() didn't work because of the typo I tried to
deallocate the space by making it 0 bytes big.)
Fyi: making previously allocated array "0 bytes big" does not
necessarily perform actual deallocation. You're correct to use
'free' for that.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 6 '07 #13
On Jun 6, 4:25 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
attibln wrote:
I'm having an odd problem with a project using VisualStudio 2005 Prof.
When I execute the program below I get this message:
Debug Assertion Failed
Program: ...
File: dbgheap.c
Line: 1252
Expression: _CrtIsValidHeapPointer(pUserData)
Consider asking questions about debugging in VC++ in the newsgroup
dedicated to VC++. Take a look at "microsoft.public.vc.*" hierarchy.
He's asking about a specific C++ problem, though.

[...]
realloc( byP_jpeg_file , 0 );
'realloc' is undefined. Consider including <cstdlib>.
The behaviour of 'realloc' with 'size' passed as 0 is implementation
defined. You should consider asking in the newsgroup for your C++
compiler (see above).
The behavior he is seeing is not among the allowed behaviors;
all that is allowed is that either realloc return NULL, or that
it return a value "as if" the argument wasn't 0.

It's hard to say what the problem is, but most of the time,
these sort of errors are due to the free space arena being
corrupted---writing beyond the end of allocated memory, or
something like that. And finding such problems is difficult,
because the symptoms don't appear until long after the actual
error has occurred.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 7 '07 #14
On Jun 6, 7:37 pm, attibln <Atti...@gmx.netwrote:
On 6 Jun., 19:24, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Debug Assertion Failed!
Program: ...
File: fread.c
Line: 93
Expression: (buffer != NULL)
that line (93) is
_VALIDATE_RETURN((buffer != NULL), EINVAL, 0);
That's a good one! I didn't even see any 'fread' in your code.
fread.c is probably included by stdafx.h or <iostreamor VisualStudio
automatically.
Anyway, I wanted the buffer to be freed - which only didn't work
because of my typo.
(Fyi: When the free() didn't work because of the typo I tried to
deallocate the space by making it 0 bytes big.)
In practice, there are only two reasons why free will fail:

-- the pointer you pass to it was not allocated by malloc, or
has been freed since, or

-- you've corrupted the free space arena.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 7 '07 #15

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

Similar topics

9
by: WL | last post by:
Hey, all. I'm creating an array of strings (char **argv style) on the fly, and using realloc to create string pointers, and malloc for the strings itself (if that makes any sense). I'm using the...
7
by: Marlene Stebbins | last post by:
The bigint struct defines a big integer and represents it as a string of characters: typedef struct bigint { int sign; int size; int initflag; char *number; } bigint;
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
4
by: alex323 | last post by:
Hey. I must have an array that can be resized dynamically. I have coded an implementation of it using malloc/realloc, but I am getting a runtime error as seen below in GDB: *** glibc detected...
28
by: bwaichu | last post by:
Is it generally better to set-up a buffer (fixed sized array) and read and write to that buffer even if it is larger than what is being written to it? Or is it better to allocate memory and...
19
by: ivan.leben | last post by:
Let's say I have a piece of allocated memory which I want to expand and reuse if possible or allocate in a different part of RAM if resizing is not possible, however, in the latter case I don't...
3
by: anirbid.banerjee | last post by:
#include <stdlib.h> #include <stdio.h> int main(){ char *ptr = "hello"; ptr = (char *)realloc (ptr,(size_t) 10 * sizeof (char )); printf ("\n %s", ptr); return 0; }...
31
by: banansol | last post by:
Hi, I just want to get this right. A call to realloc() will return NULL on error and the original memory is left untouched, both when requesting a larger or a smaller size that the original,...
27
by: Kislay | last post by:
How is realloc implemented internally ? If there is not enough memory in place to allocate , is new memory allocated somewhere else and the 2 regions linked via a pointer , OR , is the old region...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
tracyyun
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...
0
agi2029
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,...

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.