Like Tree3Likes

Pointers to pointers

This is a discussion on Pointers to pointers within the C++ Programming forums, part of the General Programming Boards category; Hello guys!!! It's been a long time since I have been here but I ran into a problem today. I ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    Pointers to pointers

    Hello guys!!! It's been a long time since I have been here but I ran into a problem today. I am trying to populate a list of strings (I am not using the string library here, pointers. I know strings are easier but some of the libraries I am using are not compatible!!!) So here is what I am trying to accomplish:

    Dynamic list of strings:

    Hello
    World
    Test
    String
    List

    Here is how I am trying to load my dynamic list:

    Code:
    char **stringList;
    int count;
    
    int LoadList () {
    
         stringList = (char **) malloc (1);
    
         for (int i = 0; i < 10; i++) {
    
              char *string;
              scanf ("%s", string);  // i know scanf is bad, this is an example
                                          // also, i know the string would be
                                          // allocated first, but just imagine
                                          // that scanf takes care of that for this
                                          // example
    
              stringList[i] = (char *) malloc (strlen (string) + 1);
              strcpy (stringList[i], string);
    
              temp = (char **) realloc (stringList, i+2);
              if (temp == NULL)
                   break;
    
              stringList = temp;
         }     
    }
    This is a crude example of what I am doing. Scanf of course does not allocate space but in my example imagine that it does. If this is not how I am supposed to accomplish this, can anyone give me an idea how to fix it? Thanks!!!
    HA! I WIN!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by xixpsychoxix
    I know strings are easier but some of the libraries I am using are not compatible!
    Make use of the c_str() member function. If the library needs to write a null terminated string, create a std::string s of sufficient length (remember about the null character), then pass &s[0] to the library function.
    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

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    150
    I do know alot about the c++ library... <string> does not seem to be at all compatible with the graphics library I am using, DarkGDK. I cannot use it because under Visual C++ I have to omit libraries that the <string> library rely on in order to use DarkGDK. Hence, it is incompatible. I am well aware of the c_str function but it wont work in this case. Thanks for the try though...
    HA! I WIN!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by xixpsychoxix
    I cannot use it because under Visual C++ I have to omit libraries that the <string> library rely on in order to use DarkGDK.
    What libraries might they be? I can understand not using certain features of the standard library together with a particular third party library, but not being able to separately use certain features of the standard library in order to use a particular third party library means that the third party library is broken in a brain damaged way.
    VirtualAce likes this.
    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

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    150
    Here are the specific libraries that get ignored with this toolkit:

    Code:
    libcmtd, msvcrt, atls
    I suppose that they feel that their toolkit contains enough functionality to deal with whatever problem there may be. They are wrong since I have a problem that I can't deal with through the use of their libraries but it doesn't matter the problem is the same either way.
    HA! I WIN!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    If you cannot use msvcrt, then I believe that you cannot use the standard library at all*, not even the C library functions that deal with string handling. In other words, you're on your own. Design and implement your own string class. No using malloc either: you have to use whatever memory allocation functionality is provided by the library.

    * okay, maybe you can use some header-only parts of the standard library, like those from <algorithm>, but whether you can or cannot I cannot say for sure.
    Last edited by laserlight; 01-24-2012 at 08:32 PM.
    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
    Registered User
    Join Date
    Mar 2006
    Posts
    150
    I know that the c standard library is working; I have other functions that rely on it and they are working. The conflicting library is libcmt.lib. I definitely have use of malloc, realloc, free, and all of the string functions and they are working fine. I posted a thread on their forums too in case they have any information there.
    HA! I WIN!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by xixpsychoxix
    I know that the c standard library is working; I have other functions that rely on it and they are working. The conflicting library is libcmt.lib. I definitely have use of malloc, realloc, free, and all of the string functions and they are working fine.
    Ah, that sounds like the documentation about ignoring msvcrt is wrong. That is good news nonetheless.

    Does the library provide a string class?

    Quote Originally Posted by xixpsychoxix
    I posted a thread on their forums too in case they have any information there.
    Good idea.

    Anyway, the thing is, if you really need to C memory management and string handling functions, then your best option is probably to write your own string class. Otherwise, you have to deal with manual memory management in the face of possible exceptions, and be very careful about the null character and string length lest you allow a buffer overrun. In your example, you also have the problem where you want to read in strings, but if you do not know the length of the longest possible string, then you have to expand the string's capacity while reading.

    Oh, and if you cannot use std::vector, writing your own dynamic array class template would likely prove useful. This could then be used to implement your string class, if needed.
    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

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    12
    You really should be using new and delete for dynamically allocating memory in C++. Using an STL container like vector to store your string list would also be good. If the library you are using does not allow you to use large parts of the C++ standard library, then that is very bizarre.

    Looking at your quite rough example code, your use of pointers to pointers seems to have the right kind of idea. You seem to be allocating too little memory to store the pointers, though (since a pointer is bigger than just 1 byte). For example, your first malloc should be something like:
    Code:
    malloc( sizeof(char *) )
    Last edited by Time Traveler; 01-24-2012 at 09:13 PM.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    150
    I think it has to do with the fact that the DarkGDK is really more based on C than C++. It is built based on Windows APIs and the Directx toolkit. I can use c++ classes and new and delete keywords, but apparently the standard libraries conflict. I don't know enough about STL to do anything with it... I will have to get my C++ book out Thanks though guys!!! I will see if the other forum has any additional info.
    HA! I WIN!

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    First
    I do know alot about the c++ library
    then
    I don't know enough about STL to do anything with it
    There's your problem: pretending to know more than you do. Highly counterproductive, my friendly.

    I think it has to do with the fact that the DarkGDK is really more based on C than C++.
    From the DarkGDK website
    The Game Creators are pleased to announce that their flagship C++ game development package, Dark GDK
    laserlight likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by xixpsychoxix
    I can use c++ classes and new and delete keywords, but apparently the standard libraries conflict. I don't know enough about STL to do anything with it...
    Ah, if you can use new[] and delete[] but not std::vector and std::string, then you would have an easier time writing a naive replacement for std::vector, though you would only need malloc and placement new to write a replacement for std::vector that more closely resembles its performance. But let's not jump the gun: get advice from the library's forum community first.
    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

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    150
    I didn't PRETEND to know anything. I know most of the standard library functions but I took a C++ course alongside a Java course that took most of my focus. I am still in school and have not reached c++ classes yet. I do know how to use the stuff but I don't have it committed to memory. Maybe a clarification of what I meant was in order. I haven't exactly written much C++ because I took Java in school and only learned a little C++ there. I studied it on my own and am fairly comfortable writing C++ code using most of the standard libraries. Furthermore, just because the DarkGDK was written to target the Visual C++ 2008 package does NOT mean that it is completely compatible with all of the C++ libraries contained in the compiler. Which is obviously the case. I am neither a. an idiot or b. a liar. I am not great with C++, but I know my way around it. Not to start a war here, just trying to clarify what I meant. Sorry if I sound oversensitive but I don't want you guys to think I am misinformed or lying about the stuff that I have covered. It is not that I have never used STL, it has just been so long since I have written anything but trivial c++ that I don't remember alot of it. I will look it up and though you guys may not think it after my little rant here I do appreciate the help you are giving!
    HA! I WIN!

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    You're allocating 1 byte for a pointer:
    Code:
    stringList = (char **) malloc (1);
    Given pointers will be more than one byte in size on your system, that is a clear bug.
    The realloc is equally wrong for the same reason.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    150
    Quote Originally Posted by iMalc View Post
    You're allocating 1 byte for a pointer:
    Code:
    stringList = (char **) malloc (1);
    Given pointers will be more than one byte in size on your system, that is a clear bug.
    The realloc is equally wrong for the same reason.
    MAN OF THE HOUR!!! I LOVE YOU!!!!! Haha sry if you are the woman of the hour it doesn't say. Anyways replaced

    Code:
    stringList = (char **) malloc (1);
    
    // and later
    
    char **temp = (char**) realloc (stringList, count);
    with

    Code:
    stringList = (char **) malloc (sizeof (char**));
    
    // and later
    
    stringList = (char **) realloc (count*sizeof(char**));
    Thank you very much!! See, I am much more comfortable with C than C++ and I'm sorry about my rant it is very late at night . You guys really do alot to help people with questions and I didn't mean to get so heated . Thank you so much!!!
    HA! I WIN!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  2. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  3. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  4. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  5. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM

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