Solved

100pt question malloc()

Posted on 2000-04-30
5
169 Views
Last Modified: 2010-04-02
Here is the question for 100 pts! help :)
 
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cprog&qid=10336579
0
Comment
Question by:cisk
  • 3
5 Comments
 
LVL 5

Accepted Solution

by:
pitonyak earned 50 total points
Comment Utility

I looked at your code and you are freeing the code malloced code, but
I suspect that this is not what you intended to do.
The problem is that all that you are doing is simply freeing memory.
For example, your code there are the following three lines:

   printf("Before free %s --- ", tbp->entry[j].name);
   free(tbp->entry[j].name);
   printf("After free %s\n", tbp->entry[j].name);

The first line is OK assuming that you do not have a null pointer.
The second line is ok because the memory was malloced.
The third line is WRONG because you printed memory that you just deleted.
What you really want to do is to swap the names.
I assume that nothing else points to the memory so you could do this as follows:


for(i=0; i < n; i++) {
   for(j=0; j< n-1; j++) {
      if(strcmp(tbp->entry[j].name, tbp->entry[j+1].name) > 0) {
         // switch names
         char* temp = tbp->entry[j].name;
         tbp->entry[j].name = tbp->entry[j+1].name;
         tbp->entry[j+1].name = temp;
         // switch the phone numbers
         temp = tbp->entry[j].telNum;
         tbp->entry[j].name = tbp->entry[j+1].telNum;
         tbp->entry[j+1].telNum = temp;
      }
   }
}

0
 

Expert Comment

by:843988
Comment Utility
I agree your answer, pitonyak
0
 
LVL 1

Author Comment

by:cisk
Comment Utility
Adjusted points from 0 to 50
0
 
LVL 1

Author Comment

by:cisk
Comment Utility
Comment accepted as answer
0
 
LVL 1

Author Comment

by:cisk
Comment Utility
I'm sorry but i canceled this question a long time ago and i guess i thought it was going to delete this one too.. i figured out why a few minutes after i pointed..

 BUT i'm going to ago ahead and ive 50 points to pitonyak because he was right and he answered in 2 hours
=)
cool?
Cisk
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now