Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

100pt question malloc()

Posted on 2000-04-30
5
Medium Priority
?
176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

722 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