Dynamic memory allocation

This is a discussion on Dynamic memory allocation within the C++ Programming forums, part of the General Programming Boards category; Hey! I was reading about dynamic memory allocation, and I don't quite get the purpose of it. It says, you ...

  1. #1
    Imperator of Darkness Luciferek's Avatar
    Join Date
    Jun 2008
    Location
    Detroit, MI
    Posts
    38

    Dynamic memory allocation

    Hey!
    I was reading about dynamic memory allocation, and I don't quite get the purpose of it. It says, you can create pointer to an array you don't know the size
    of yet. Why not create an array during rutime, using a variable as a size?
    Below is an example of what I mean.

    With dynamic array:
    Code:
    int v, *dynarr;
    cin >> v;
    cin.ignore();
    dynarr = new int [v];
    cout<<"\n";
    for (int x = 0; x<v; x++){
        dynarr[x]=v;
        cout << dynarr[x];
    }
    Without:
    Code:
    int v;
    cin >> v;
    cin.ignore();
    int dynarr[v];
    cout<<"\n";
    for (int x = 0; x<v; x++){
        dynarr[x]=v;
        cout << dynarr[x];
    }
    Those two pieces of code do the same thing, so what is the advantage of creating a pointer to an array again?
    We're all Fu##ed aren't we?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    They don't do the same thing.
    The code at the bottom is illegal (though some compilers may provide it as an extension).
    Even if it compiles, don't do it, because it's non-standard code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Ada does something similar to that. Within a certain scope, using a variable to declare the size of the array and its not the same thing as using a "new" and thus doesn't need to be "deleted" explicitly.

    I believe that something like that might be useful, but would confuse the issue since the "new" operation is enough to create unknowns at runtime.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    But we don't need it.
    A vector can do the same thing and is standard.
    Code:
    int v;
    cin >> v;
    cin.ignore();
    std::vector<int> dynarr(v);
    cout<<"\n";
    for (int x = 0; x<v; x++){
        dynarr[x]=v;
        cout << dynarr[x];
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by Elysia View Post
    But we don't need it.
    A vector can do the same thing and is standard.
    this is an example of encapsulation of the "new". Not really the same thing. But you are correct in that similar if not identical syntax can be obtained by use of encapsulation.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,718
    Though to get the types, right:
    Code:
    std::vector<int>::size_type v;
    cin >> v;
    cin.ignore();
    cout<<"\n";
    std::vector<int> dynarr(v);
    for (std::vector<int>::size_type x = 0; x<v; ++x){
        dynarr[x]=v;
        cout << dynarr[x];
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Imperator of Darkness Luciferek's Avatar
    Join Date
    Jun 2008
    Location
    Detroit, MI
    Posts
    38

    illegal huh?

    Could you elaborate on illegal and what do you mean by that?
    If it works, it works... i don't think i'll get arrested!
    Besides, are there standards related to C++ code? Where do i find those?
    We're all Fu##ed aren't we?

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    illegal according to the specification. And I'm surprised that it worked for you. What compiler?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    strange, I just tried g++ and it worked. Learn something new every day I guess.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Quote Originally Posted by Luciferek View Post
    Could you elaborate on illegal and what do you mean by that?
    If it works, it works... i don't think i'll get arrested!
    Besides, are there standards related to C++ code? Where do i find those?
    Just because it works doesn't mean it's right. Remember that.
    It may work, but it may be undefined.
    It may work, but it may be implementation defined.
    It may work because it's an extension.
    In all of these cases, it may work, but it may fail to compile on other compilers and it may just crash or cause unpredictable things elsewhere because it's undefined.

    This forum specializes in teaching portable code. That means that you don't use any extensions.

    Quote Originally Posted by FillYourBrain View Post
    strange, I just tried g++ and it worked. Learn something new every day I guess.
    It's an extension.
    I don't know if this is 100% correct, but compile with
    -Wall -pendantic -ansi
    Or something along the lines.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    yep, that stops it.

    kinda disappointing that gcc allows that by default
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  12. #12
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    It works without warning with -Wall -Wextra -ansi (-pendantic isn't recognised by my g++)

    I know that this wasn't allowed in C89-C90 but was allowed in C99.
    So you are saying this in non-standard in C++?

    In any case lets see what are the difference.
    Is the space in both cases allocated from the heap?
    Since the compiler doesn't know the value of a variable then the space cannot be allocated in compile time. So if you time a[v] then the space I assume is allocated at run time.
    My point it, that the "illegal" way function like a new and an automatic delete when the variable goes out of scope, or it is something different?

  13. #13
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    its "pedantic"
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  14. #14
    Imperator of Darkness Luciferek's Avatar
    Join Date
    Jun 2008
    Location
    Detroit, MI
    Posts
    38
    So, In other words... There is no real advantage to using a new and delete keywords except
    that it is accepted as a standard?
    Besides why are compilers picky in what they allow and what they do and don't allow? Isn't C++ a C++?
    So, if I create a compiler that will bastardize the C++ syntax to the maximum it will be ok?

    More I learn C++, more i get angered by it!
    Bjarne must have been smoking crack or something!

    By the way, the compiler I used to compile it is Dev C++.
    Last edited by Luciferek; 10-01-2008 at 08:58 AM.
    We're all Fu##ed aren't we?

  15. #15
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by Luciferek View Post
    So, In other words... There is no real advantage to using a new and delete keywords except
    that it is accepted as a standard?
    That is incorrect. The syntax you are talking about (which doesn't use new) is for variables scoped locally. It does not allow for something such as a linked list or a binary tree, where dynamic memory must be created and then passed along somehow.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Page 1 of 8 12345678 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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