Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

100pt question malloc()

Posted on 2000-04-30
5
Medium Priority
?
177 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 200 total points
ID: 2764929

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
ID: 2928069
I agree your answer, pitonyak
0
 
LVL 1

Author Comment

by:cisk
ID: 2929257
Adjusted points from 0 to 50
0
 
LVL 1

Author Comment

by:cisk
ID: 2929260
Comment accepted as answer
0
 
LVL 1

Author Comment

by:cisk
ID: 2929261
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

783 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