CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Join Date
    Jul 2009
    Posts
    14

    Angry allocating bidimensional matrix

    hi all!! I need to create a function that by reference reallocate an asymmetrical bidimensional
    character matrix...
    I've done something like that but it crash...

    ================================CODE================================

    #include <iostream>

    using namespace std;

    void add(char ***list, int n, char *person)
    {
    static int GlobStrLen = 0;
    *list = (char**) realloc(*list, GlobStrLen * sizeof(char**));
    *list[n] = (char*) malloc (strlen(person) + 1);
    strcpy(*list[n], person);
    GlobStrLen += strlen(person) + 1;
    }

    int main()
    {
    char **list = NULL;
    int n = 0;

    add(&list, 0, "jack");
    printf("list[0]: %s\n", list[0]);

    n++;

    add(&list, 1, "jeff");
    printf("list[1]: %s\n", list[1]);

    return 0;
    }

    ================================END CODE============================

    thank you for yours replies!!!

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: allocating bidimensional matrix

    1. Please, use Code tags. See http://www.codeguru.com/forum/misc.php?do=bbcode#code

    2.
    Quote Originally Posted by EvIl_DeViL;1865712[CODE
    ]#include <iostream>

    using namespace std;

    void add(char ***list, int n, char *person)
    {
    static int GlobStrLen = 0;
    *list = (char**) realloc(*list, GlobStrLen * sizeof(char**));
    *list[n] = (char*) malloc (strlen(person) + 1);
    strcpy(*list[n], person);
    GlobStrLen += strlen(person) + 1;
    }[/CODE]
    So, reallocate some buffer with the size zero?

    3. Is there some reason to use malloc/realloc rather than new/delete?
    or it is just a plain C (not a C++) code?
    Victor Nijegorodov

  3. #3
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: allocating bidimensional matrix

    'list' is NULL, so how do you want to REalloc that ?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: allocating bidimensional matrix

    Quote Originally Posted by VictorN View Post
    Or it is just a plain C (not a C++) code?
    It's definitely supposed to be C++, as <iostream> is included. The problem is that the code is written as if it was 'C'.

    Regards,

    Paul McKenzie

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: allocating bidimensional matrix

    Quote Originally Posted by Skizmo View Post
    'list' is NULL, so how do you want to REalloc that ?
    realloc behaves like malloc if its first argument is NULL.

    I would advise using a vector<string> for this problem.

  6. #6
    Join Date
    Jul 2009
    Posts
    14

    Re: allocating bidimensional matrix

    thank you for your many replies!
    sorry for the CODE tag... I didn't find it :P
    quoting lindley for the behaving of the realloc.
    I used realloc instead new/delete beacause with the C++ operator you can't "realloc" memory unless you create another memory area of the same size of the one you want to realloc to be able to store your data and then re-copy in the "resized" original "area".
    basically I can't see to much difference from the C operator and the C++ operator to manage the memory (talking about char type) so I thought I'd better use realloc instead creating a self-made one with new/delete.
    I think it doesn't matter if I use C or C++. the only thing to pay attention for is use only malloc/realloc/free or new/delete.
    for the goal i'm trying to reach i think I can do the same thing with the C dynmem stuff or the C++ dynmem stuff...
    about this line:
    Code:
    *list = (char**) realloc(*list, GlobStrLen * sizeof(char**));
    i made it for keeping the memory contiguous.
    anyway all this code work only for the first element...
    p.s. I'd prefer a C style solution for the problem... i feel it much lower and manageable instead a C++ style one.

    THANK YOU!!

  7. #7
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: allocating bidimensional matrix

    realloc behaves like malloc if its first argument is NULL.
    Ah... never used it, hence the confusion

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: allocating bidimensional matrix

    Quote Originally Posted by EvIl_DeViL View Post
    hi all!! I need to create a function that by reference reallocate an asymmetrical bidimensional
    character matrix...
    1) What do you mean "by reference"?

    2) Do not name your variable "list", as there already is a std::list class in C++.

    3) Using vector<string>
    Code:
    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <vector>
    
    typedef std::vector<std::string> StringArray;
    
    int main()
    {
        StringArray theList;
        theList.push_back("jack");
        printf("list[0]: %s\n", theList[0].c_str());
    
        theList.push_back("jeff");
        printf("list[1]: %s\n", theList[1].c_str());
        return 0;
    }
    This code does the same as yours, and there are no pointers involved.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: allocating bidimensional matrix

    Quote Originally Posted by EvIl_DeViL View Post
    p.s. I'd prefer a C style solution for the problem... i feel it much lower and manageable instead a C++ style one.
    If you take a look at my post, and your first post, which one seems more manageable to you (or to most other C++ programmers)?

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: allocating bidimensional matrix

    Quote Originally Posted by EvIl_DeViL View Post
    the only thing to pay attention for is use only malloc/realloc/free or new/delete.
    Why? What are the reasons for this requirement?

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Jul 2009
    Posts
    14

    Re: allocating bidimensional matrix

    interesting solution... I stopped studing C++ after class... so i miss all the strange template stuff, the std library, and so on... good excuse for getting an upgraded!!! many thanks!!!
    (it's still be interesting viewing the realloc version :P)

    Quote Originally Posted by Paul McKenzie View Post
    Why? What are the reasons for this requirement?

    Regards,

    Paul McKenzie
    don't use delete for unlocating an area allocated by malloc/calloc/realloc and don't "free()" an area allocated by new because they manage memory in different ways!
    Last edited by EvIl_DeViL; July 31st, 2009 at 01:09 PM.

  12. #12
    Join Date
    Nov 2006
    Posts
    1,611

    Re: allocating bidimensional matrix

    (it's still be interesting viewing the realloc version :P)
    Same code sample.

    Vector will automatically expand as required.

    Great stuff!
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  13. #13
    Join Date
    Jul 2009
    Posts
    14

    Re: allocating bidimensional matrix

    Quote Originally Posted by JVene View Post
    Same code sample.

    Vector will automatically expand as required.

    Great stuff!
    can you post some example code please? I can't figure...

  14. #14
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: allocating bidimensional matrix

    Quote Originally Posted by EvIl_DeViL View Post
    I used realloc instead new/delete beacause with the C++ operator you can't "realloc" memory unless you create another memory area of the same size of the one you want to realloc to be able to store your data and then re-copy in the "resized" original "area".
    A good reason, right up until you realize that std::vector::resize() is C++'s answer to realloc----plus it makes things even easier by completely hiding the fact that you're working with dynamic memory at all.

    Unless you're working with strings specifically, in which case std::string has some behaviors which are more intuitive than std::vector<char>.

  15. #15
    Join Date
    Apr 1999
    Posts
    27,449

    Re: allocating bidimensional matrix

    Quote Originally Posted by EvIl_DeViL View Post
    interesting solution... I stopped studing C++ after class... so i miss all the strange template stuff, the std library, and so on... good excuse for getting an upgraded!!! many thanks!!!
    (it's still be interesting viewing the realloc version :P)
    There are several threads that have full source to routines that create dynamically allocated 2D arrays.
    don't use delete for unlocating an area allocated by malloc/calloc/realloc and don't "free()" an area allocated by new because they manage memory in different ways!
    What I meant by "what were your reasons for this requirement" is to see why you needed to create a multidimensional array using 'C' methods instead of just using vector<string>.

    Regards,

    Paul McKenzie

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured