Thread: Integers on the heap

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174

    Integers on the heap

    For a task I'm supposed to read in integers from the command line and then sum them up after putting them onto the heap.Now, I'm quite rusty with using malloc and strings and whatnot because we're pretty new to them, but I've had some experience with putting string of characters onto the heap.

    This is my first time putting integers onto the heap though, and I'm very stuck.

    What I have so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int *heap(int argc, char *argv[]);
    int sum();
    
    int main(int argc, char *argv[]) {
        //int sum = 0;
        int *value = NULL;
        
        if (argc > 1) {
            value = heap(argc, argv);
            if (value == NULL) {
                fprintf(stderr, "Memory Error\n");
                return EXIT_FAILURE;
            }
        }
        
        return EXIT_SUCCESS;
    }
    
    int *heap(int length, char *argv[]) {
        int *value = NULL;
        
        value = malloc(length * sizeof(int));
        if (value != NULL) {
            //INSERT HERE
        }
        return value;
    }
    I'm completely clueless with what to do in the comment that says INSERT HERE. The argv values are strings of characters (even if integers are input) so I can't just put them onto the heap... Any advice?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,390
    Therefore, you need to access argv and convert argv[1] to argv[argc - 1] to int, writing the result to consecutive elements of the dynamic array. Also, note that the length should be argc - 1, not argc, since argv[0] is not going to be a numeric string to convert.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Quote Originally Posted by laserlight View Post
    Therefore, you need to access argv and convert argv[1] to argv[argc - 1] to int, writing the result to consecutive elements of the dynamic array. Also, note that the length should be argc - 1, not argc, since argv[0] is not going to be a numeric string to convert.
    Thanks for the tip I was then able to move on and get the program working by using the atoi function.
    Oh and I understand that the number of integers I would have would be argc-1 but for the malloc I needed to put in length=argc to make room for the null terminator.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,390
    Quote Originally Posted by Mentallic
    but for the malloc I needed to put in length=argc to make room for the null terminator.
    There is no need for a null terminator since you are keeping track of the number of elements of the array that is in use. Furthermore, 0 is a valid integer value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by Mentallic View Post
    [...]make room for the null terminator.
    Why would you think it necessary to have a null terminator in this case?
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Sorry about the late reply. I can see your point of the null terminator being unnecessary, but I suppose it's good practice to use it regardless. At least, that's what we're expected in class.
    We are also supposed to declare all our variables before they're used, even though we'll soon be changing the variable's value with a scanf and such, so giving the malloc a little extra room doesn't seem like such a big deal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-26-2012, 10:50 AM
  2. Min-Heap help
    By Uni616 in forum C++ Programming
    Replies: 1
    Last Post: 04-29-2010, 02:00 AM
  3. Heap?
    By audinue in forum Tech Board
    Replies: 8
    Last Post: 01-12-2009, 11:18 PM
  4. The heap
    By Frantic- in forum C++ Programming
    Replies: 2
    Last Post: 06-17-2005, 03:42 AM
  5. Anyone Know HEAP ?
    By Kam in forum C Programming
    Replies: 5
    Last Post: 09-14-2002, 07:47 AM