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

Are enormous malloc()s conforming?

Consider the following program:

#include <stdlib.h>
#include <limits.h>

int main(void)
{
size_t s=SIZE_MAX;
size_t i;
char* p;
while(s--)
{
p=malloc(s);
if(p)
{
i=s;
while(i--) p[i]=42;
free(p);
}
}
return 0;
}

This program would be incredibly slow to run, but I'm interested in
whether or not it causes UB. There seem to be implementations (I
include the operating system as part of the implementation) which would
crash if they tried to run this, due to malloc returning non-NULL when
the system doesn't actually have the memory available (which is
non-conforming behaviour AFAICT). So: Is this UB, or is it merely a
common implementation non-conformance?
--
ais523

Dec 13 '06 #1
3 1227
ais523 said:
Consider the following program:

#include <stdlib.h>
#include <limits.h>

int main(void)
{
size_t s=SIZE_MAX;
size_t i;
char* p;
while(s--)
{
p=malloc(s);
if(p)
{
i=s;
while(i--) p[i]=42;
free(p);
}
}
return 0;
}

This program would be incredibly slow to run, but I'm interested in
whether or not it causes UB.
It doesn't, as far as I can see.
There seem to be implementations (I
include the operating system as part of the implementation) which would
crash if they tried to run this, due to malloc returning non-NULL when
the system doesn't actually have the memory available (which is
non-conforming behaviour AFAICT). So: Is this UB, or is it merely a
common implementation non-conformance?
The latter.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 13 '06 #2
Richard Heathfield wrote:
ais523 said:
Consider the following program:

#include <stdlib.h>
#include <limits.h>

int main(void)
{
size_t s=SIZE_MAX;
size_t i;
char* p;
while(s--)
{
p=malloc(s);
if(p)
{
i=s;
while(i--) p[i]=42;
free(p);
}
}
return 0;
}

This program would be incredibly slow to run, but I'm interested in
whether or not it causes UB.

It doesn't, as far as I can see.
There seem to be implementations (I
include the operating system as part of the implementation) which would
crash if they tried to run this, due to malloc returning non-NULL when
the system doesn't actually have the memory available (which is
non-conforming behaviour AFAICT).
So: Is this UB, or is it merely a
common implementation non-conformance?

The latter.
As usual, Richard giving both sides of the Möbius fence. ;-)

Michael Wojcik once wrote:

"Oh good, the lazy-allocation religious war has broken out again.

"If no implementation is allowed to fail under the conditions "malloc

returns non-null and later the program tries to use the returned
memory", then no real implementations are not broken. Memory access
can fail for reasons beyond the implementation's control.

"Lazy allocation has its proponents and opponents, and both have
reasonable arguments at their disposal, but this "lazy allocation
breaks conformance" one is not among them. Even if it's true (and no

one has presented a convincing proof of that, IMO), it's pointless,
since many of us have to use implementations on platforms which use
lazy allocation. If those implementations conform except in using
lazy allocation, that will have to be good enough, since there is no
alternative."

--
Peter

Dec 14 '06 #3
Peter Nilsson wrote:
Richard Heathfield wrote:
ais523 said:
Consider the following program:
>
#include <stdlib.h>
#include <limits.h>
>
int main(void)
{
size_t s=SIZE_MAX;
size_t i;
char* p;
while(s--)
{
p=malloc(s);
if(p)
{
i=s;
while(i--) p[i]=42;
free(p);
}
}
return 0;
}
>
This program would be incredibly slow to run, but I'm interested in
whether or not it causes UB.
It doesn't, as far as I can see.
There seem to be implementations (I
include the operating system as part of the implementation) which would
crash if they tried to run this, due to malloc returning non-NULL when
the system doesn't actually have the memory available (which is
non-conforming behaviour AFAICT).
So: Is this UB, or is it merely a
common implementation non-conformance?
The latter.

As usual, Richard giving both sides of the Möbius fence. ;-)

Michael Wojcik once wrote:

"Oh good, the lazy-allocation religious war has broken out again.

"If no implementation is allowed to fail under the conditions "malloc

returns non-null and later the program tries to use the returned
memory", then no real implementations are not broken. Memory access
can fail for reasons beyond the implementation's control.
Well yes but the opening poster specified that he considers
the operating system part of the implementation. Without
that condition perhaps Richard's answer would have been
different.

Dec 14 '06 #4

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

Similar topics

11
by: Aire | last post by:
On some small-memory systems, is it true that "malloc()" is not supported by the C library? Why is 'dynamic memory allocation' so avoided on such systems? What are major problems of using...
54
by: Neo | last post by:
Hi Folks, I've a simple qestion related to dynamic memory allocation in C here is the code: #include <stdio.h> int main() {
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
96
by: pavan | last post by:
Is this code correct char *str = (char *)malloc(10 * sizeof(char)); char = ... char = ... .... .... ... char = '\0';
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...
35
by: =?utf-8?b?QXNiasO4cm4gU8OmYsO4?= | last post by:
This topic is a FAQ. But I have read the faq and spent a couple of hours browsing the group archives, and still have a few questions that I hope you can answer. My understanding is that...
17
by: arnuld | last post by:
I have checked the FAQ: http://c-faq.com/malloc/mallocnocast.html FAQ discusses a special case when programmer has forgotten to do #include <stdlib.h>. I am including this header and I am not...
158
by: jty0734 | last post by:
i don't know what input size of string is. so i can't gets inputsize before malloc function. i want determine the size of malloc without get inputsize in advance. how to deal with it?
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
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?
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...

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.