|
#121
|
|
|
|
|
arnuld wrote:
.... > I should have been more clear. Take a look st this: > > char* T; > T = calloc( 10, sizeof(char)); > > while your expression is: > > char* T; > T = calloc( 10, sizeof(*T)); >> Thats what I am talking about, I am looking for a place to store N > characters, not 10 pointers to pointers (in our original program). So why > create space for char** when we want to store char(s) in them. This is my > original question about calloc/malloc/realloc It really appears to be a problem with your understanding of how sizeof operates. The above statement reserves space for 10 characters; "*T" is an expression with type 'char', and therefore "sizeof *T" has exactly the same value as sizeof(char); In your original code, you had int get_single_word( char** pc ) and *pc = calloc(AVERAGE_SIZE-1, sizeof(char)); which Richard recommended that you change to: *pc = calloc(AVERAGE_SIZE - 1, sizeof **pc); "**pc" is an expression of type 'char', and therefore "sizeof **pc" means exactly the same as sizeof(char). The advantage of using "sizeof **pc" is that deciding whether or not sizeof(char) is correct requires looking back at the declaration of 'pc'. You can determine whether or not "sizeof **pc" is the right value, just by looking at this one line; it remains the the correct value, no matter what the declaration of pc is (unless pc has a type for which the calloc() call is inappropriate, in which case it's a constraint violation). |
|
|
|
#122
|
|
|
|
|
arnuld said:
>> I should have been more clear. Take a look st this: > > char* T; > T = calloc( 10, sizeof(char)); > > while your expression is: > > char* T; > T = calloc( 10, sizeof(*T)); Not quite. It's: T = calloc(10, sizeof *T); (Spot the difference.) Why you started using T as an object pointer name instead of as a type, I don't know, but hey, that's legal. But it means we need a new name for the type. Let's call it mytype. mytype *T; T = calloc(n, sizeof *T); > Thats what I am talking about, I am looking for a place to store N > characters, not 10 pointers to pointers (in our original program). Oh, okay, N, not 10. T = calloc(N, sizeof *T); Happy now? > So why > create space for char** when we want to store char(s) in them. If T is a char *, then *T is a char, not a char **, so sizeof *T is 1. |
|
#123
|
|
|
|
|
Richard Heathfield <rjh> writes:
> arnuld said: >> Not quite. It's: > > T = calloc(10, sizeof *T); > > (Spot the difference.) The first is more widely used in the real world in my experience. What are the problems with T = calloc( 10, sizeof(*T)); ? |
|
#124
|
|
|
|
|
Ron Ford <ron> writes:
> On Wed, 17 Sep 2008 07:38:29 +0000, Richard Heathfield posted: >> Ashcroft [...] Americans [...] extraconstitutionality. [...] > deviations [...] data mining [...] domestic terrorists [...]. Ron, I implore you, *please* knock off the political discussion. This is not the place for it. Many people, myself most emphatically included, have strong opinions on these things, and we could easily spend tremendous amounts of bandwidth discussing them -- and we'd lose the valuable *technical* resource that is this newsgroup. More likely, people will just assume you're a troll who's *trying* to disrupt this newsgroup, and will simply ignore you -- which is what I intend to do if you continue trying to bring US politics into this group. > You can say it has nothing to do with C, the house language of the CIA, but > I'll beg to differ. Beg all you want, but do it somewhere else. It has nothing to do with C. If you feel the need to discuss politics, there are plenty of forums (newsgroups, blogs, other web sites, your living room) where it's perfectly appropriate. |
|
#125
|
|
|
|
|
> On Wed, 17 Sep 2008 14:06:52 +0000, James Kuyper wrote:
> In context, arnuld was using the term "academic" in reference to > Richard's statement 'If this is a mere learning exercise and the > learning task is not error recovery, then yes, by all means bomb out. > That's the "student solution"...'. I think that the contextual meaning > was clearly pejorative ("mere learning exercise"), whether or not he > meant it to be more generally pejorative. I think you guys need to stop arguing and listen to me instead ;) . Forget about pejorative or bombing out. My meaning of academic means, "being not practical" . In Indian computer education system, clrscr() is the function that belongs to a language named TURBO C, which is better than C. There is *only* one good C and that is TURBO C. Same for VC++ which, of course , is much better than C++. Microsoft has created a better language than C. Thats *exactly* what I have been taught in my 3 years of graduation and thats *exactly* what academic means. I did not even told that TURBO C was actually a proprietary compiler. I was told it was the C. okay, enough of real rant. |
|
#126
|
|
|
|
|
> On Wed, 17 Sep 2008 14:27:39 +0000, James Kuyper wrote:
> It really appears to be a problem with your understanding of how sizeof > operates. > > The above statement reserves space for 10 characters; "*T" is an > expression with type 'char', and therefore "sizeof *T" has exactly the > same value as sizeof(char); > ....SNIP.... Oh.. no... I think I overwhelmed by the pointers and arrays. Now I got it, Ben's code also helped me a lot to clear up the confusion: char *ptr; ptr = calloc(10, sizeof *ptr); *ptr is the char actually. same for **ppc in my program. I don't understand why I was so much messed up in understating this little thing. |
|
#127
|
|
|
|
|
On 17 Sep, 14:10, Richard Heathfield <r...@see.sig.invalid> wrote:
> arnuld said: > > >> On Wed, 17 Sep 2008 09:37:47 +0000, Richard Heathfield wrote: > > >> Think about it. First satisfy yourself that this is correct: > > >> V = calloc(n, sizeof *V); <SNIP> > > Array elements in your example are of type V, not *V. This is what > > confusing me. > > V isn't a type. It's a pointer. Let them be of type T, instead. Okay? > > Yes, V points to n elements, each of which is sizeof *V bytes in size. V points to a single element, not n of them. The element that it points to is the first element of an array of n elements. |
|
#128
|
|
|
|
|
On 2008-09-17, arnuld <sunrise> wrote:
> > My meaning of academic means, "being not practical" . In Indian computer > education system, clrscr() is the function that belongs to a language > named TURBO C, which is better than C. There is *only* one good C and that > is TURBO C. Same for VC++ which, of course , is much better than C++. > Microsoft has created a better language than C. > Well, TURBO C is not C, and VC++ is not C++. Microsoft should not have named their products so similarly to those real languages. (On the other hand, Microsoft does have some real languages now, like C# and friends, which are perfectly fine languages in their own right, and don't go around masquerading as other ones.) > Thats *exactly* what I have been taught in my 3 years of graduation and > thats *exactly* what academic means. I did not even told that TURBO C was > actually a proprietary compiler. I was told it was the C. > Well, that's silly. > > okay, enough of real rant. > Yes - this entire article is off-topic. |
|
#129
|
|
|
|
|
> On Wed, 17 Sep 2008 09:37:47 +0000, Richard Heathfield wrote:
> Anyway, I finally got around to testing my code, and noticed some bugs. > Fixing those gives working code: > ...SNIP... I wrote the program without looking at your code and it works fine :) . > ..SNIP... > if(ch != EOF) > { > pc_begin[idx++] = ch; So it is C programming style to do that ? I mean, whenever we get a ** or a *** pointer in some function argument we should create a new * and reference it to the original value. e.g. we had a dual indirection pointer here char **pc, so we did: pc_begin = *ppc; or char*** ppc; pc_begin = **pppc; which gives single pointer and has 2 benefits: 1) We can manipulate that single pointer easily without any ambiguous and confusing pointer arithmetic. 2) It leaves the original pointer unchanged. Is this the lesson here ? |
|
#130
|
|
|
|
|
> On Wed, 17 Sep 2008 09:37:47 +0000, Richard Heathfield wrote:
> ...SNIP... > Anyway, I finally got around to testing my code, and noticed some bugs. > Fixing those gives working code: > > (Headers and #defines as before) > > int get_single_word( char** pc ) > { > int rc = GSW_ENOMEM; /* if we succeed, we'll update the status */ > size_t idx = 0; > int ch; > char *pc_begin = NULL; > size_t cursize = AVERAGE_SIZE; > char *new = NULL; > > *pc = calloc(cursize, sizeof **pc); > if(*pc != NULL) > { > rc = GSW_OK; /* so far so good */ > pc_begin = *pc; > .....SNIP... This whole program is based on that condition of *pc != NULL. Why don't we put a line like, *pc == NULL and then return from the program with the ENOMEM value right there ? > ....SNIP.. here is what the code of get_words() looks like, which is supposed to create a dynamic array of pointers pointing to different words: int get_words( char*** pw, size_t* cnt ) { int idx; int mem_check; char *pw_begin, *new_mem; size_t arr_size; arr_size = ARRSIZE; mem_check = GW_ENOMEM; **pw = calloc( arr_size, sizeof **pw ); if( **pw ) { mem_check = GW_OK; pw_begin = **pw; } else { mem_check = GW_ENOMEM; return mem_check; } if( idx == arr_size ) { new_mem = realloc( **pw, (2 * arr_size * sizeof new_mem)); if( new_mem ) { pw_begin = new_mem; } else { mem_check = GW_ENORESIZE; return mem_check; } Its nearly a duplicate of get_single_word, as the design is same. onlya few things are confusing me. calloc() will return a pointer to allocated space which we will assign to a pointer. But in this function we are working with pointer to arrays or pointer to pointers. So how come get the pointer to pointer as first element of the allocated space. Or calloc() does it for us and we can do something like: char*** pw; *pw_begin = calloc( arr_size, sizeof(*pw_begin) ); |
|
#131
|
|
|
|
|
William Pursell said:
> On 17 Sep, 14:10, Richard Heathfield <r...@see.sig.invalid> wrote: > <SNIP> >> V points to a single element, not n of them. The element that it > points to is the first element of an array of n elements. Yes, you're right, careless of me. Thank you. s/to n/to the first of n/ |
|
#132
|
|
|
|
|
arnuld said:
>> > I think you guys need to stop arguing and listen to me instead ;) . > Forget about pejorative or bombing out. >> My meaning of academic means, "being not practical" . In Indian computer > education system, clrscr() is the function that belongs to a language > named TURBO C, which is better than C. I've never heard such baloney. C is a language. Turbo C is an ancient implementation, no longer supported, which runs on only one tiny class of platforms. > There is *only* one good C and that is TURBO C. Try telling that to the OS390 guys. How many C implementations did your teachers use before arriving at this opinion? > Same for VC++ which, of course , is much better than > C++. There's no "of course" about it. VC++ is an implementation of C++, with extensions. I stopped bothering with MFC (a supposedly cool extension) when I discovered that you couldn't make a CList of CLists - a stupid restriction that forced me to come up with a workaround. C++ itself is a language, not an implementation, and there are plenty of implementations of C++. How many C++ implementations did your teachers use before arriving at this opinion? > Microsoft has created a better language than C. What makes your teachers think so? And on what criteria are they judging? And why do they dismiss the criteria on which they are not judging? > Thats *exactly* what I have been taught in my 3 years of graduation A load of baloney. > and thats *exactly* what academic means. No, it isn't. In academic circles, the truth is valued rather more highly than that. What you have experienced is not an academic system but a travesty. > I did not even told that TURBO C was > actually a proprietary compiler. I was told it was the C. You were told wrong. > okay, enough of real rant. It's hard to know what you're ranting against. If it's the apparent incompetence of the teaching you are receiving, I sympathise. |
|
#133
|
|
|
|
|
arnuld said:
>> On Wed, 17 Sep 2008 09:37:47 +0000, Richard Heathfield wrote: > >> pc_begin[idx++] = ch; > > So it is C programming style to do that ? I mean, whenever we get a ** or > a *** pointer in some function argument we should create a new * and > reference it to the original value. No. I was doing so because you seemed to like it that way. In my own code written for my own benefit, I would probably have used (*pc)[idx++] |
|
#134
|
|
|
|
|
arnuld said:
>> > > This whole program is based on that condition of *pc != NULL. Why don't > we put a line like, *pc == NULL and then return from the program with the > ENOMEM value right there ? Lots of people /would/ write it like that. In *my* code, I consider an "early return" like that to be a bug that I should fix, because in *my* code I want every function and every loop to have *one* entry point and *one* exit point. I find that this makes my programs much easier to read and thus to maintain. Clearly, lots of people disagree with that, including some whose opinion I value rather highly. Nevertheless, I have tried writing code both ways, and found "my" way to be best for me. > here is what the code of get_words() looks like, which is supposed to > create a dynamic array of pointers pointing to different words: >> int get_words( char*** pw, size_t* cnt ) > { > int idx; > int mem_check; > char *pw_begin, *new_mem; > size_t arr_size; > > arr_size = ARRSIZE; > mem_check = GW_ENOMEM; > > **pw = calloc( arr_size, sizeof **pw ); Surely you mean *pw = calloc( arr_size, sizeof **pw ); Oh, by the way - I don't see the bit where you point the array's elements to words. Why isn't get_words calling get_single_word? |
|
#135
|
|
|
|
|
> On Thu, 18 Sep 2008 09:04:49 +0000, Richard Heathfield wrote:
> I've never heard such baloney. C is a language. Turbo C is an ancient > implementation, no longer supported, which runs on only one tiny class of > platforms. I did not know that TURBO C is an ancient compiler. As of 2008, the Indian Computer Education system still rely on TURBO C compiler. and if you do write a program in standard C (ANSI, I mean), creating a function of your own to "clear the screen" instead of using clrscr() and #include <conio.h>, you will not get any marks. I did not get any :( IIRC, Question was: Write a C program that will .... and will clear the screen in the end. > Try telling that to the OS390 guys. How many C implementations did your > teachers use before arriving at this opinion? In our colleges TURBO C is the only thing we know, of course, as of 2008. > How many C++ implementations did your teachers use before arriving at this > opinion? We use 2: TURBO C++ and VC++. In the beginning of 2008, a 75% marks holder in MCA (Master of Computer Applications) told me that was taught by his professor that VC++ is better than C++ . he called me an idiot when I told him he is not using ISO C++ standard but fell silent when I asked him explain the difference between a vector and an array in C++. He apologized later after I told him the difference between the C++ and the VC++. Guess what .. he was from my college and was my senior and he is earning 2 times more than me :( > You were told wrong. I was even told that Windows is a prerequisite for UNIX .. okay wait... I was also told about the History of Programming Languages: ----- History Begins ------ First we had BASIC and all of the programs were written in BASIC. Then we had C which replaced BASIC, and now Java has replaced C. ----- History Ends ------ > It's hard to know what you're ranting against. If it's the apparent > incompetence of the teaching you are receiving, I sympathise. I paid a heck lot of money in 3 years for that qualification (2000 - 2003) but found later that just 3 months at USENET refute everything I learned. Those were one of the the most painful moments of my life when I learned that there are compilers and there are languages, ( one of the other painful truth was when I found that we can run our computer without installing Windows, using Red Hat UNIX: http://lispmachine.wordpress.com/200...e-unix-effect/ okay, its going very OT, won't reply anymore :) |
|
|
|
|
| Similar Threads | |
| Thread | Thread Starter |
| Best way to input from stdin? I'm writing a program that supports input from stdin. To be able to do that I tend to rely on a simple loop that tests the return of fgets(), such as the following... |
Rui Maciel |
| How to accept input from stdin? Hi, I try to make a wrapper around an existing program, which would behave exactly the same as the original one. But my following attempt was failed. Would you pleaes let me... |
PengYu.UT |
| Input using stdin How can I give input to a program using STDIN Suppose I want the program to take the value x=10 On some other site i found it as STDIN.read,but its not working. |
Nikhil Warade |
| getting input from stdin Hi Im new to unix scripting and now Im trying to get user input from stdin and this is what I did echo "enter your name: " read name and it will run with the pointer to... |
compboy |
| Checking available input on stdin I know this has probably come up frequently, but couldn't find a satisfactory reference... I have some code which needs to read from stdin but must not block waiting for... |
Lionel B |
|
Privacy Policy | All times are GMT. The time now is 04:23 PM.
|
|
|