473,806 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

malloc call not returning


Hi NG,

I've the problem, that a malloc call is not returning.

<source-snip>

printf("a\n");
my_pointer = (struct_pointer )malloc(struct_ size); /*size 1420*/
printf("b\n");
</source-snip>

I'm almost sure, that my program frees some memory, but if there is no
memory, the malloc should return some NULL-pointer.

The 'normal' output of the function is 'a' and 'b'. But sometimes (after
a while of running), the program stops after printing the 'a'. The other
threads of the programm are still responding to commands, but the one
who called the malloc does not reacts.

Have you somewhere had a similar problem?
Compiler: gcc-3.3.3-7 on Fedora Core 2
Greeting:
Norbert
May 2 '07 #1
24 1981
Norbert Leister wrote:
>
Hi NG,

I've the problem, that a malloc call is not returning.

<source-snip>

printf("a\n");
my_pointer = (struct_pointer )malloc(struct_ size); /*size 1420*/
printf("b\n");
</source-snip>

I'm almost sure, that my program frees some memory, but if there is no
memory, the malloc should return some NULL-pointer.

The 'normal' output of the function is 'a' and 'b'. But sometimes (after
a while of running), the program stops after printing the 'a'. The other
threads of the programm are still responding to commands, but the one
who called the malloc does not reacts.

Have you somewhere had a similar problem?
Compiler: gcc-3.3.3-7 on Fedora Core 2
Attach your debugger and see what the application is doing.

--
Ian Collins.
May 2 '07 #2
Ian Collins wrote:
Attach your debugger and see what the application is doing.
it is some remotely executed code - that's why I'm using the printf to
see where the code is stopping

Greetings:
Norbert
May 2 '07 #3
In article <f1**********@a nderson.hrz.tu-chemnitz.de>,
Norbert Leister <ma******@web.d ewrote:
>I've the problem, that a malloc call is not returning.
If malloc() is really not returning, the most likely problem is that
you've got a malloc()-related error somewhere else in the program that
is messing up malloc()'s data structures.

Run your program under some kind of memory-checking tool such as
valgrind.

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
May 2 '07 #4
Norbert Leister wrote:
>
I've the problem, that a malloc call is not returning.

<source-snip>

printf("a\n");
my_pointer = (struct_pointer )malloc(struct_ size); /*size 1420*/
printf("b\n");
</source-snip>
Two fundamental errors. First, don't cast the return value from
malloc. Doing so may have hidden compiler error reports, such as
caused by failure to #include <stdlib.h>. Second, test the
returned result - if it is NULL the malloc failed.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com

May 2 '07 #5
I did not knew, that I shoud'nt cast the pointer - because malloc is
returning some (void*) while my pointer is of some other type ... (and I
don't like implicite casts).

The 'if(NULL == my_pointer) ...' is straight after the output :) I did
not wanted to bore the NG with uninteresting code.

Greetings:
Makarius

CBFalconer wrote:
Two fundamental errors. First, don't cast the return value from
malloc. Doing so may have hidden compiler error reports, such as
caused by failure to #include <stdlib.h>. Second, test the
returned result - if it is NULL the malloc failed.
May 2 '07 #6
Norbert Leister wrote:
I did not knew, that I shoud'nt cast the pointer - because malloc is
returning some (void*) while my pointer is of some other type ... (and I
don't like implicite casts).
There are no implicit casts.

That's because casts are the name for the expressions `(Type) Expr`.

/Conversions/ can be implicit.

Implicit conversions can be very useful, reducing the verbosity of your
code and making it differently robusted against changes in the types of
its component variables.

For pointer types, it's important to note that a cast has much the same
effect as "shut up, I know what I'm doing", which is all very well
except when you make a mistake: classically, when you fail to declare
`malloc`, the compiler assumes it returns `int`, your cast to `Spoo*`
is silently accepted, and the assignment of the complete garbage value
to its pointer destination goes undetected until the Most Embarrasing
Moment.

[Yes, a friendly compiler will tell you that you've forgotten to
declare `malloc`, that you've cast an integer to a pointer, did
you mean that, sir? etc; but friendliness isn't required by the
Standard, which is quite happy if your implementation is an axe
weilding psychopath in its spare time, viz, whenever there's un
defined behaviour to fall into.]

--
"You're not supposed to /think/ about it, /The Beiderbeck Connection/
you're supposed to say NO!" Jill Swinburn

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

May 2 '07 #7
hi,

Chris Dollin wrote:
[Yes, a friendly compiler will tell you that you've forgotten to
declare `malloc`, that you've cast an integer to a pointer, did
you mean that, sir? etc; but friendliness isn't required by the
Standard, which is quite happy if your implementation is an axe
weilding psychopath in its spare time, viz, whenever there's un
defined behaviour to fall into.]


when I write

my_pointer = malloc(struct_s ize);

i get an error compiling the code with g++. I sometimes use compiling my
c-code in g++ go get some extendend compiler warnings. So i use a
typecast for all mallocs I'm using...

Greeting:
Norbert
May 2 '07 #8
In article <f1**********@a nderson.hrz.tu-chemnitz.de>,
Norbert Leister <ma******@web.d ewrote:
>when I write

my_pointer = malloc(struct_s ize);

i get an error compiling the code with g++. I sometimes use compiling my
c-code in g++ go get some extendend compiler warnings. So i use a
typecast for all mallocs I'm using...
So you're using a C++ compiler to get more warnings about C code
despite the fact that some of them are wrong and stop your program
compiling? This seems a dubious trade-off to me.

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
May 2 '07 #9
Norbert Leister wrote:
hi,

Chris Dollin wrote:
[Yes, a friendly compiler will tell you that you've forgotten to
declare `malloc`, that you've cast an integer to a pointer, did
you mean that, sir? etc; but friendliness isn't required by the
Standard, which is quite happy if your implementation is an axe
weilding psychopath in its spare time, viz, whenever there's un
defined behaviour to fall into.]
>
when I write

my_pointer = malloc(struct_s ize);

i get an error compiling the code with g++.
g++ isn't a C compiler.
I sometimes use compiling my
c-code in g++ go get some extendend compiler warnings.
Turn up the warnings for gcc, rather than use g++.

--
"It's just the beginning we've seen" - Colosseum, /Tomorrow's Blues/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

May 2 '07 #10

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

Similar topics

59
5205
by: Steve Zimmerman | last post by:
This program compiles fine, but are there any hidden dangers in it, or is it ok? Experiment 1 ################################################## #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <string.h>
3
2582
by: Hassan Iqbal | last post by:
Hi, (1)if i do the memory allocation like: int **p; p= malloc(n*sizeof p); for(i=0;i<n;i++) /* i and n are integers */ p= malloc(n*sizeof p); then in that case is this command sufficient to free all the allocated memory:
7
669
by: Ramprasad A Padmanabhan | last post by:
Hello all, If I assign one pointer to another , I had assumed I can call them interchangably. I can explain by the example below. I am doing a malloc to char* a which is the same as char* b. But when I free(b) the memory allocated to a does not get freed Can someone explain this.
36
6698
by: MSG | last post by:
The answer is neither. Use macros. #define ALLOC(size, type) ((type) *) malloc((size) * sizeof(type)) #define NEW(type, name, size) (type) * (name) = ALLOC((size), (type)) They are both type-safe and concise. Compare: NEW(int, x, 1000);
54
7875
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() {
40
540
by: ramu | last post by:
Hi, what happens when i run the below code? main() { int *p; while(1) p= (int *)malloc(1000); } Do i get segmentation fault?
58
4698
by: Jorge Peixoto de Morais Neto | last post by:
I was reading the code of FFmpeg and it seems that they use malloc just too much. The problems and dangers of malloc are widely known. Malloc also has some overhead (although I don't know what is the overhead of automatic variable sized arrays, I suspect it is smaller than that of malloc), although I'm not too worried about it. I was thinking that, with C99's variable length arrays, malloc shouldn't be needed most of the time. But I'm...
10
1914
by: somenath | last post by:
Hi All, I have one question regarding return value cast of malloc. I learned that we should not cast the return value of malloc because it is bug hider. But my question is as mentioned bellow . Lets say I have not included stdlib.h in my program still I am using malloc so compiler will throw warring because with out prototype
173
8207
by: Marty James | last post by:
Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there's no point putting in a check on the return value of malloc. OTOH, if you're allocating a gigabyte for a large array, this might fail, so you should definitely check for a NULL return.
0
10618
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10366
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 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...
0
10110
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 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...
0
9187
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, 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...
1
7649
isladogs
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...
0
6877
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();...
1
4329
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
2
3850
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.