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

(part 2) Han from China answers your C questions

Returning int pointer ?

Mahendra said:
I have following code snippet -

int *createIncidenceMatrix(int numEdges, int numVertices) {
int *p = (int *) calloc((numEdges*numVertices), sizeof(int));
if(p == NULL) {
printf("Could not allocate memory");
exit(0);
}
return p;

}

void createIncidencePerView(int numEdges, int numVertices, int viewId) {
int *pnew = createIncidenceMatrix(numEdges, numVertices);

}

This code compiles fine. I am trying to understand if this will run as
intended. Obviously, I tried running the code also. My question is -

When createIncidenceMatrix() return p which is local variable, will p
get deallocated at return call ?
No, it's impossible to say.

If your target system is a pre-C99 implementation, only 6 significant
characters, case insensitive, are guaranteed in external identifiers.
For all we know, your implementation may see two functions named
"create()".

Next, if stdout is line-buffered, your call to printf() won't inform you
that you're out of memory. What if a signal handler gets invoked between
printf() and exit() and enters an infinite loop like for(;;);? You'll be
stuck in an infinite loop, with no indication that you couldn't allocate
memory.

Next, since memory is at premium, your call to printf() could fail if,
for instance, it wants to allocate a copy of the output string. On some
Unix and Unix-like systems, printf() can fail with a return of ENOMEM,
meaning insufficient memory. So it would be best to conserve your bytes
as follows:

printf("Couldn't alloc. mem.\n");

Actually, if you're working in the Linux tradition, you can adopt one
of the Linux defensive programming techniques:

printf("culdnt aloc mem\n");

Next, I just want to commend you for using stdout as opposed to stderr
for your error message. You see, people on Unix-like systems are wont to
use shell redirects like "2>/dev/null" to dodge having to deal with error
messages. This is unwise. Your program ensures they have to use the more
inconvenient "grep -v 'culdnt aloc mem'", so you've made an excellent design
decision there for your end users.

I also commend you on casting the return from calloc(). If you forget to
include stdlib.h, at least your program may still compile.

However, I don't want to hurt your feelings or anything, but the expression
(numEdges*numVertices) strikes me as a bit of a wimpy expression, pardon me for
saying so. C is a real man's language, and I often find that reading another's C
code allows me to plumb the depths of his psyche. Your unnecessary parentheses
and compact tokens seem to indicate fear and lack of confidence (with the language,
I mean).

Yours,
Han from China

Il mittente di questo messaggio|The sender address of this
non corrisponde ad un utente |message is not related to a real
reale ma all'indirizzo fittizio|person but to a fake address of an
di un sistema anonimizzatore |anonymous system
Per maggiori informazioni |For more info
https://www.mixmaster.it

Oct 29 '08 #1
1 1235
George Orwell wrote:
>>
When createIncidenceMatrix() return p which is local variable, will p
get deallocated at return call ?

No, it's impossible to say.
As usual Nomen Nescio is talking rubbish.
Han from China
No you're not.
Oct 30 '08 #2

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

Similar topics

1
by: Nomen Nescio | last post by:
A new learner's question Flash Gordon wrote: To the OP, please be aware that Flash Gordon has a history of either deliberately misleading and "miss-informing" posters to this newsgroup or...
0
by: Nomen Nescio | last post by:
what is the type for this call? Neo said: Neo, buddy, I've decided to give you a little summary for reference so that you don't have to sift through excess verbiage, cryptic meanderings,...
0
by: Nomen Nescio | last post by:
No format string passed to variable argument list function Adam said: Then Adam laters says: Then Adam later says:
2
by: Chris Dollin | last post by:
Joachim Schmitz wrote: I think that remark is an ill-considered and unhelpful generalisation. -- "We dance, and the worlds melt away." - Curved Air, /Metamorphosis/ ...
2
by: Borked Pseudo Mailed | last post by:
float and integers c.lang.myself@gmail.com said I will be making assumptions here about your system that will infuriate the pedants, but I don't care. Given your X-HTTP-UserAgent string, it...
1
by: George Orwell | last post by:
its urgent.pls help me. mona said: mona said: Hey again, mona. Please check the thread "(part 21) Han from China answers your C questions", in which I gave you a program with
0
by: George Orwell | last post by:
fread breaks file descriptors opened in "w" mode. ... Yes, once you break out of ANSI/ISO C, all things are possible. You have posted an on-topic question to comp.lang.c. See version 2.0 of...
16
by: Borked Pseudo Mailed | last post by:
Error handling in C Nick Keighley said: ... ... To the OP, please be aware that Nick Keighley is a known troll and is deliberately providing you with unsound advice. You asked for error...
2
by: Nomen Nescio | last post by:
What is this? Eric said: That looks like either someone's poor coding or someone's poor pasting for an implementation of offsetof() from stddef.h. The (char *) leads me to think that there's...
1
by: George Orwell | last post by:
File Seeking / Overwriting bytes Martien Verbruggen said: And this is, of course, also not right. fseek() does support SEEK_END on text streams. If you want to know why, please start a...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
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...

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.