dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Simple Char* questions

  1. #1

    Resolved Simple Char* questions

    Hi everyone, Got some simple questions.

    When you declare something such as

    char * hello = "Hello World"

    is this created on the stack as a Automatic variable so the compiler knows what offset its going to be referencing during compile time.. and if thats the case how do I edit the variable so I can replace the 'W' to another letter, I assume *(hello + 5) and I can convert that to a char but I get an access violation when i try setting it, Do I strcpy to another char buffer[50]?? then edit it from there?

    Also Im passing a string from

    Code:
    	void * tempbuffer = malloc(256);
    	Success = GetLogicalDriveStrings(255, (char *)tempbuffer);
    
    	void * DriveLetters = malloc(Success);
    
    	memcpy(DriveLetters, tempbuffer, Success);
    so now I have a dynamicly sized string with all the letters. is there a easy way to str.replace all the \0 in it before I send it through a function? im trying to avoid the string class right now but I will later get into that, im trying to learn as much low level stuff as possable..

    Thankyou!

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

    Re: Simple Char* questions

    Quote Originally Posted by AgentSmithers View Post
    how do I edit the variable so I can replace the 'W' to another letter,
    You can't edit it. It is a string-literal, and string-literals cannot be changed without incurring undefined behaviour.

    You should declare an array:
    Code:
    char hello[] = "Hello World";
    Now the string is modifiable.
    Also Im passing a string from

    Code:
    void * tempbuffer = malloc(256);
    	Success = GetLogicalDriveStrings(255, (char *)tempbuffer);
           
    	void * DriveLetters = malloc(Success);
    
    	memcpy(DriveLetters, tempbuffer, Success);
    Are you programming 'C' or C++? Why are you using malloc() and free() when in C++ you have new[] and delete[]? And why even go to the lengths of dynamically allocating memory?
    Code:
    	char tempbuffer[256];
    	Success = GetLogicalDriveStrings(255, tempbuffer);
    	char DriveLetters[256];
    	memcpy(DriveLetters, tempbuffer, Success);
    im trying to avoid the string class right now but I will later get into that, im trying to learn as much low level stuff as possable..
    There was a long thread as to why this approach is backwards. You must know when and where to use the high-level things before embarking on knowing the low-level things. What good does it do if you are trying to become a pilot, but instead start out learning how to put together an engine?

    Regards,

    Paul McKenzie

  3. #3

    Re: Simple Char* questions

    And why even go to the lengths of dynamically allocating memory?

    Well I just want to make my program clean and Fast, is it normal programming behaivor to Allocated more then needed?

    so when I use

    Code:
    Success = GetLogicalDriveStrings(255, (char *)tempbuffer);
    I should really have tempbuffer[256] and not bother cutting off the trailing Zeros when i pass it through a function?

    Ive replaced my code with this

    Code:
    char * DriveList(void)
    {
    	long Success,D,X,Y,Z;
    
    	char tempbuffer[256];
    	Success = GetLogicalDriveStrings(255, tempbuffer);
    
    	int NumDrivesFetched = Success / 4;
    
    
    	if(NumDrivesFetched > 1)
    	{
    		for (int i=3; i<Success;i+=4)
    		{
    			if(tempbuffer[i] == '\0'){tempbuffer[i] = ' ';}
    		}
    	}
    
    	return 0;
    }
    Is this more Ideal?
    Last edited by AgentSmithers; January 26th, 2009 at 12:46 AM.

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

    Re: Simple Char* questions

    Quote Originally Posted by AgentSmithers View Post
    And why even go to the lengths of dynamically allocating memory?

    Well I just want to make my program clean and Fast, is it normal programming behaivor to Allocated more then needed?

    so when I use

    Code:
    Success = GetLogicalDriveStrings(255, (char *)tempbuffer);
    I should really have tempbuffer[256] and not bother cutting off the trailing Zeros when i pass it through a function?
    Well, first off, the difference in speed and memory usage is going to be insignificant either way in this case. However, you need to understand: when you put a constant-sized array on the stack, all it does is increment the stack pointer. That's pretty much one operation, constant-time, extremely fast. If you allocate on the heap using new, there's *much* more complicated logic involved with choosing an appropriate block of memory to return to you. Probably logarithmic time rather than constant. It's still fairly fast, but make no mistake-----it's a bit slower. Fractionally so, but it'll add up if you get into the habit of using "new" more often than necessary.

    So do you want efficient fast, or efficient small? In 99&#37; of cases today, fast is more important than small. There aren't many systems left where every byte counts.

    And there's a third factor in play as well in addition to size and speed-----safety & readability. I call those one factor because they're often related----unreadable code is more likely to have safety issues. For safety and readability, your best bet is to use an STL container like std::string rather than bothering with a char array at all.
    Last edited by Lindley; January 26th, 2009 at 12:52 AM.

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

    Re: Simple Char* questions

    Quote Originally Posted by AgentSmithers View Post
    And why even go to the lengths of dynamically allocating memory?

    Well I just want to make my program clean and Fast, is it normal programming behaivor to Allocated more then needed?
    How does that explain the first allocation for tempbuffer? You knew it was 256 bytes, but you still allocated it using malloc(). Also, in the context of the code you posted, DriveLetters can be no longer than 256 bytes, which is a very small amount. All you needed to do was stick a NULL at position Success of the DriveLetters buffer. That is much faster than calling the allocator.

    Second, as Lindley pointed out, dynamically allocating memory using new[] or even malloc() incurs runtime overhead, and depending on what you're doing, it could be a significant amount of overhead. For example, if you're calling a member function of a class 10,000 times, and each time you're allocating a new buffer, and at the end deleting the buffer, that is a lot of time spent. Replacing the allocation/deallocation with a member that is a container classes (such as std::vector) , the speed increase is substantial.

    And lastly, you don't know what is "fast" unless you profile your code against different scenarios. Just guessing that something is fast, as you've done, is faulty. What you thought was "fast" is not fast at all.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jan 2009
    Posts
    18

    Re: Simple Char* questions

    i'm tryn to make this program work.but i keep on gettn my objects as null when i run it,can anyone help .this are the line of code


    import java.util.*;

    class Employees3
    {
    protected String name;

    protected int salary;

    protected Date birthDate;

    public String getDetails()
    {
    System.out.println("Name: " + name + "\n" + "Salary: " + salary);
    return "Name: " + name + "\n" + "Salary: " + salary;

    }
    }




    public class Manager3 extends Employees3
    {
    protected String department;

    public Manager3(String name,int salary,String department)
    {
    name="Sally";
    salary=45;
    department="Operations";
    }
    public String getDetails(String name,int salary,String department)
    {
    this.name=name;
    this.salary=salary;
    this.department=department;

    System.out.println("Name: " + name + "\n" + "Salary: " + salary+"\n"+"Manager of: "+department);
    return "Name: " + name + "\n" + "Salary: " + salary+"\n"+"Manager of: "+department;
    }
    public static void main(String[] args)
    {
    Manager3 m = new Manager3("dave",45,"oparetions");

    Employees3 e = new Employees3();

    e.getDetails();
    m.getDetails();


    }
    }

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

    Re: Simple Char* questions

    Quote Originally Posted by seken1 View Post
    i'm tryn to make this program work.but i keep on gettn my objects as null when i run it,can anyone help .this are the line of code
    1) Do not hijack threads.

    2) The code you posted is not C++, as this is the topic of this forum.

    Regards,

    Paul McKenzie

  8. #8

    Re: Simple Char* questions

    Thanks for the ADVICE!

  9. #9
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Simple Char* questions

    Quote Originally Posted by AgentSmithers View Post
    And why even go to the lengths of dynamically allocating memory?

    Well I just want to make my program clean and Fast, is it normal programming behaivor to Allocated more then needed?
    If you don't want to allocate more than needed, then why 256? 105 characters (26*4+1) would be enough.
    To be even more efficient, first call GetLogicalDriveStrings(0, 0) the return value will tell you exactly how much to allocate.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)