allocation on heap, scalar/non scalar types

This is a discussion on allocation on heap, scalar/non scalar types within the C++ Programming forums, part of the General Programming Boards category; When allocating memory using new on the heap (or free store) you must use a pointer correct. When attempting to ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    allocation on heap, scalar/non scalar types

    When allocating memory using new on the heap (or free store) you must use a pointer correct.

    When attempting to allocate to a basic type like a string variable I get an error about conversion to non-scalar type.

    From this I gather that a scalar type is a pointer.
    A non-scalar type pod. Correct?

    Thought I understood memory allocation using new completely and just took it for granted they where using pointers. From my expirments I want to conculde that this is a requirement, but am asking for some confirmation.

    I was expirmenting with passing the address of a pointer to a function that accepts type ptr. I discovered the obvious. Passing the address of a pointer is passing a refrence to some data. Derefrencing the pointer in the argument list of a function is a refrence to the data therefore pod or non-scalar hence the necessity for a pointer to pointer when using dynamic memory allocation. I'll post an example if anybody wants assuming I am correct.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >When allocating memory using new on the heap (or free store) you must use a pointer correct.
    No, but if you want to keep a reference to that memory you do.

    >When attempting to allocate to a basic type like a string variable I get an error about conversion to non-scalar type.
    Are you trying to assign the result of new to a non-pointer type? If so, that won't work, new gives you a pointer, and assigning an address to a non-pointer type is likely not a good idea.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Thanks, how would you allocate memory on the heap using new for a non-pointer type?

    Feel really stupid asking this it's probably really basic, and I am doing somthing stupid.

    I was attempting
    string word = new string("foo");

    which as you said was attempting to assing a pointer to 'non-scalar' type.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    well thats just plain stupid.....
    try this...

    string* ptr = new string("foo");

    remember new returns a pointer.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Reread Preludeīs post again, especially the second quote and you will understand that it should be
    Code:
    //I wouldnīt define a string object this way unless itīs REALLY necessary 
    string *word = new string("foo");
    also donīt forget to deallocate memory with delete.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Maybe I misunderstood the first part of the post.
    She said that it is not necessary to use a pointer, which is what I was failing to do in.
    string word = new string("foo");

    I understand that new returns a pointer and hence
    string * word = new string("foo");
    works

    but this is what confused me in the reply
    >When allocating memory using new on the heap (or free store) you must use a pointer correct.
    No, but if you want to keep a reference to that memory you do.
    I think what she thought I meant was somthing like this
    string * ptrword = new string("foo");
    string word=*ptrword;

    not sure though.

    ok I get she said that assigning to a non pointer type using new is simply assigning the address hence it would have had to been type double or somthing hence my error and misunderstanding and the fact that it is possible but probably not what was intended.

    Thanks for the replies
    string is non-scalar ie not a number and there fore can't accept an address. So now I understand the real meaning behind the error I think.
    Last edited by curlious; 12-29-2003 at 10:26 AM.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >She said that it is not necessary to use a pointer
    It isn't. The following is perfectly legal:
    Code:
    #include <string>
    
    int main()
    {
      new std::string();
    }
    You'll notice that not one pointer was used in that little program (explicitly at least). However, the above is really dumb. Why would you allocate anonymous memory from the heap and then discard your only reference to it? So technically you don't need a pointer to hold anonymous memory, but not using one makes non sense at all. That was what I meant.
    My best code is written with the delete key.

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    A memory leak! The horror!

    Code:
    #include <string>
    
    int main()
    {
      delete new std::string();
    }


    Okay, so the program returns right away... It's still leaky, I say!
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Bottom line is that new and malloc return the address of a chunk of memory. Whether you keep track of it is another issue altogether. If you're just needing a single object from the heap, you could use a reference like this:

    int & i = *new int;
    delete [] &i;
    Code:
    #include <ip.hpp>

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You would use plain delete here, not delete[].
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Interesting use of a refrence. This isn't what I needed but informative none the less.

    Thanks for the clarification prelude.

    Just creating a binary tree using a struct, pretty much have the creation routine down, before I can test it though I am going to have to free the nodes and am thinking of using postOrderTraversal to delete the nodes.

    This is mainly just a learning exercise so thanks for all of the input from everyone! If I can get some sleep I will write routine to free the nodes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack vs heap memory allocation
    By gongchengshi in forum C Programming
    Replies: 9
    Last Post: 11-18-2007, 11:17 AM
  2. Dinamic allocation for numberic types
    By Niara in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2007, 05:20 AM
  3. stl containers allocation in heap or stack?
    By sawer in forum C++ Programming
    Replies: 9
    Last Post: 08-06-2006, 03:08 PM
  4. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 04:03 AM
  5. Determining Heap Allocation
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-13-2002, 04:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21