GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-Jul-2005, 18:07
Atomical Atomical is offline
New Member
 
Join Date: Jul 2005
Posts: 25
Atomical has a little shameless behaviour in the past

pointers to pointers


I've run this through the debugger and the problem I continue to have is that the pointers inside my function are not passed back to the main program. I took screenshots of it to you show you what I mean.

harddrivecaddy.com

After I learned about pointers to pointers I rewrote my function but got the same result. I've tried a hundred variations but all return the same result. Here's my code:


CPP / C++ / C Code:
int hostfromurl ( char *url, char **getpath ,int& port,char **host, char **query) {

string s_url = reinterpret_cast<const char*>(url);

url_string foo(s_url);

if ( foo.get_scheme() != "http" )
return -1;


//alright, so it works inside this function
host = (char **)tochar(foo.get_net_path());

}

char ** tochar ( string s ) {
char **retptr;
retptr = (char **)malloc(sizeof(char)*2000);
char *t = strdup (s.c_str() );
*retptr = t;
return retptr;
}

Last edited by LuciWiz : 01-Aug-2005 at 00:26. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 30-Jul-2005, 19:06
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Please read the Guidelines and pay close attention to guideline #1.

You really need to explain what you're trying to do because without the background of what and why, the how is unknowable.

It looks like you have some serious memory leakage if you ever do get this to work the way you're going. Using a convoluted pointer to pointer hodgepodge like this can cause a lot of grief in the long run.

Also, because strdup() is non-standard it's compiler-dependant. Information I have is it already does a malloc() and you must remember to free it before you lose track of the buffer it creates. You probably should not use functions that internally malloc buffers because it's easy to lose control of your memory.
__________________

Age is unimportant -- except in cheese
  #3  
Old 30-Jul-2005, 19:25
Atomical Atomical is offline
New Member
 
Join Date: Jul 2005
Posts: 25
Atomical has a little shameless behaviour in the past
My whole program is written with character arrays. The opensource URL class I downloaded is written with the string library. I'm trying to convert the string values to character arrays.
  #4  
Old 30-Jul-2005, 19:56
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Atomical
My whole program is written with character arrays. The opensource URL class I downloaded is written with the string library. I'm trying to convert the string values to character arrays.
This tells us nothing. What is the code supposed to do? What is the purpose for this code's existence?
__________________

Age is unimportant -- except in cheese
  #5  
Old 30-Jul-2005, 20:02
Zorachus's Avatar
Zorachus Zorachus is offline
Junior Member
 
Join Date: Jul 2005
Posts: 58
Zorachus will become famous soon enough
I don't know much about pointers, but the following code does what you are trying to do, as long as the string is not more than one line long. As I do not know the commands to create files in c++, I used the system command.

CPP / C++ / C Code:
//*Converts string Example to char converted[100] without pointers*//
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
string example="Example text";
char converted[100];
system ("echo a > String.txt");
fstream file ("String.txt");
cout << "Input a string value: ";
cin >> example;
file << example << endl;
file.close();
fstream readfile ("String.txt");
readfile.getline (converted,100);
file.close();
cout << converted << endl;
system ("pause");
return 0;
}
  #6  
Old 30-Jul-2005, 20:39
Atomical Atomical is offline
New Member
 
Join Date: Jul 2005
Posts: 25
Atomical has a little shameless behaviour in the past
Quote:
Originally Posted by WaltP
This tells us nothing. What is the code supposed to do? What is the purpose for this code's existence?

The code's purpose is to parse a URL and update the host, get, and query pointer arguments. Then I'm going to use that data to create a socket and perform a request to a web server. I want this function to act as a wrapper to the string class.

Class is here: freshmeat.net
  #7  
Old 30-Jul-2005, 20:55
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Atomical
The code's purpose is to parse a URL and update the host, get, and query pointer arguments. Then I'm going to use that data to create a socket and perform a request to a web server. I want this function to act as a wrapper to the string class.

Class is here: http://freshmeat.net/projects/liburl/
It does? I don't see any parsing what-so-ever. All I see is 2000 pointers to pointers being malloc()ed and one string being duplicated and returned somewhere in this buffer of pointers.

Let me rephrase the question. What is
CPP / C++ / C Code:
char ** tochar ( string s ) 
{
    char **retptr;
    retptr = (char **)malloc(sizeof(char)*2000);
    char *t = strdup (s.c_str() );
    *retptr = t;
    return retptr;
}
supposed to be doing? Given the string s, what does it return? This is after all what you asked about...
__________________

Age is unimportant -- except in cheese
  #8  
Old 30-Jul-2005, 21:13
Atomical Atomical is offline
New Member
 
Join Date: Jul 2005
Posts: 25
Atomical has a little shameless behaviour in the past
Quote:
Originally Posted by WaltP
It does? I don't see any parsing what-so-ever. All I see is 2000 pointers to pointers being malloc()ed and one string being duplicated and returned somewhere in this buffer of pointers.

Let me rephrase the question. What is
CPP / C++ / C Code:
char ** tochar ( string s ) 
{
    char **retptr;
    retptr = (char **)malloc(sizeof(char)*2000);
    char *t = strdup (s.c_str() );
    *retptr = t;
    return retptr;
}
supposed to be doing? Given the string s, what does it return? This is after all what you asked about...

string -> const char * -> char *
I want it to return a pointer to the converted string that is now a character array. In other words, takes a string and returns a char.
  #9  
Old 30-Jul-2005, 21:38
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
So just use the c_str() method in the main routine and forget all this pointer business. if foo.get_net_path() returns a string type
CPP / C++ / C Code:
foo.get_net_path().c_str();
should return a character buffer. Use strcpy() to put it where you want it. Simple is better.

Or better yet, find a character version of the function in C instead of using the C++ string version.
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogMeeting the populace by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with an array using pointers glulu76 C++ Forum 1 01-May-2005 11:21
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36
array of pointers to ...... kai85 C++ Forum 1 18-Apr-2005 17:06
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 18:35
Help with C++ pointers Mjkramer21 C++ Forum 23 18-Apr-2004 07:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:52.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.