Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > Visual C++
Visual C++ Questions specific to Microsoft's Visual C++. For questions not specific to this Microsoft version, use the C++ Programming forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual C++ section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 24th, 2009, 04:14 PM
Friend of Wrox
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default Strings, pointers and memory

I am trying to use strings of characters where I do not know the lenght before hand; I need dynamic-length strings.

I don't understand why this fails:
Code                               My understanding
char *rep_nm = "ThisIsRepName"; // Allocates & fills memory. rep_nm
                                // holds the addr. of 1st char.
                                // String is \0 terminated.
char *msg = "";                 // Allocate & fills mem for the
                                // terminating \0.
msg = strcat(msg, "Test\n");    // Determines size of the 2 args,
                                // allocates new mem large enough
                                // for str1 & str2 & a \0.
                                // Should rtrn ptr to that addr.
msg = strcat(msg, rep_nm); 
msg = strcat(msg, "Test\n") raises an access error. I presume I am running off the end of legitimate storage space, but I don't understand why.

Last edited by BrianWren; April 24th, 2009 at 04:16 PM.. Reason: Shove “My Understanding” over for formatting
Old April 28th, 2009, 10:51 AM
Friend of Wrox
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts

I found out (elsewhere) that strcpy and strcat do not allocate memory.

In C++, to do the actions I was trying to do, space for the strings should be allocated with new, and the memory thus obtained freed up with free when you’re done with it.

If it is to be dynamic, the required space would need to be ascertained in some way.

But also in C++, using the variable type string will eliminate many of the issues involved.

Finally, if you create a string with string myVar; to pass the value to functions that require a poiter to char, use myVar.c_str(). For instance, where you would use:
  char* myVar = "C:\\Temp\\MyFile.txt";
  FILE *fp    = {0};
  fp = fopen(fName.c_str(), "r");
you would use:
  string myVar;
  myVar    = "C:\\Temp\\MyFile.txt";
  FILE *fp = {0};
  fp = fopen(fName.c_str(), "r");

Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ Pointers reality_42000 C++ Programming 1 March 4th, 2007 01:26 AM
array and pointers amrinder C++ Programming 1 November 10th, 2006 07:58 AM
A little clueless, need some pointers bmg181 ASP.NET 2.0 Basics 3 December 23rd, 2005 01:21 PM
pointers Stuby085 Visual C++ 1 August 30th, 2003 11:58 PM
Pointers jake VB How-To 12 June 21st, 2003 11:30 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.