User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,798 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,489 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 7303 | Replies: 11
Reply
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Pointers

  #11  
Oct 30th, 2006
>>*pch = ch[0];
Example 1-1

Above line is incorrect because pointer pch is uninitialized.

>>ptr = &n[0];
Example 1-3.

This can also be simplified like below: The & operator is not necessary.
ptr = n;

>>ca[3] = '\0';
Figure 2-1
The above is wrong because array ca only has 3 elements, not 4.

>>ptr = &text[0]; // not recommen
Example 2-2

Why is this not recommended? You did not explain that statement.

Example 2-3: I disagree -- it seems to add a lot more complexity to such a simple and elementry operation. The example in Example 2-2 works just find and is the one most frequently used.

>>ptr = (char *)malloc(6);]
Code 2-1
malloc() should not be typecase in C, but is required in C++

Example 3-1 is a very good example of why not to use strcpy() -- because it can easily cause buffer overrun and scribble all over the program's memory.

>> ptr = (char **)malloc(rows * (sizeof *ptr)); // allocate room for rows
Code 3-1
sizeof(*ptr) will result most likely always return 1 becuase sizeof(char) is always 1. This is clearly not what you are attempting to do. The program needs to allocate room for rows number of pointers, like this:
	ptr = malloc(rows * sizeof (char*)) // allocate room for rows

>> ptr[i] = (char *)malloc(strlen(word[i]) + 1 * (sizeof **ptr));

Same problem as above. Should be this:
		ptr[i] = malloc(strlen(word[i]) + 1 * sizeof(char **)));

Figure 4-1 needs some rework to fix up the sizeof() errors, as identified above. sizeof(char*) is not the same as sizeof(char) on most operating systems. And the sizeof(*Book) is the same thing as sizeof(char) -- guarenteed by C standards to always be 1.


Overall, give you B+ for good tutorial. Correct the mistakes and it can be a great tutorial.
Last edited by Ancient Dragon : Oct 30th, 2006 at 1:17 pm.
Reply With Quote  
Join Date: Apr 2008
Posts: 1
Reputation: PeterPimmel is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
PeterPimmel PeterPimmel is offline Offline
Newbie Poster

Re: Pointers

  #12  
Apr 26th, 2008
Originally Posted by Ancient Dragon View Post
		ptr[i] = malloc(strlen(word[i]) + 1 * sizeof(char **)));

Overall, give you B+ for good tutorial. Correct the mistakes and it can be a great tutorial.

nah, this was wrong anyway.

SO BE CAREFUL FOLKS! it has to be:
		ptr[i] = malloc((strlen(word[i])+1) * sizeof(char **)));
instead of
		ptr[i] = malloc(strlen(word[i]) + 1 * sizeof(char **)));
you know * binds stronger than + ... good old math :-P

damn someone should change this in the tutorial... I took me some nerves to find out why my app was crashing :-P
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:18 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC