Visual C++ :: Const Char Returned By Various Functions
May 13, 2013
What is the programmers responsibility with respect to const char * returned by various functions, like the C++ string class c_str() function which returns a const char * to an c style string array? In VC++ I cannot delete a const char * which holds a string literal. Take the following code for example:
Code:
void func() //a useless function with illustrative code {
string s1("abcd");
string s2("efgh");
const char * cc1 = s1.c_str(); //c_str() returns a const char * c style string pointer
s2.c_str(); //this returns a const char *, which must be allocated on the heap right?
delete cc1; //produces run time error in Release mode in VC++
}
The problem with the above code snip is that space is allocated on the heap (or so I believe) for the const char *'s returned by the 2 calls to c_str(). The delete attempt fails and there is no opportunity to delete the space allocated by const char * because its not assigned to anything (however I see c_str() used this way extensively)
So, if I cannot delete a const char *, how does the memory get recovered? Perhaps the string objects s1 and s2 themselves have pointers to the items on the heap made by c_str() calls and they get deleted by the destructors of s1 and s2 when the function ends?
When I try to compile functionB in Visual C++ it gives me this error:-
glibmm/refptr.h(199) : error C2440: 'initializing' : cannot convert from 'const Gdk::Window *' to 'Gdk::Window *' Conversion loses qualifiers
And this is the code from glibmm/refptr.h
Code: // The templated ctor allows copy construction from any object that's castable. Thus, it does downcasts: // base_ref = derived_ref template <class T_CppObject> template <class T_CastFrom>
[Code] .....
I don't actually want to change anything in the member variable canvas_event_box. I just want to be able to call one of its functions from my 'const' member function. Is there any syntax I can use to tell VC++ that I'm not actually changing the variable - just using it.
I am a newbie to C++ and VS ++. I have created a windows form application by dragging and dropping button, label..etc. i wish label text to be appeared as return value from a function. The function returns ' const char* '.how this returned string pointer can be used to display label text.?
I have a file which contains a year and the name of an associated file to be read. I need to extract the data in the txt file and perform some calculations.
( year data file) 2004 2004data.txt 2005 2005data.txt 2006 2006data.txt
Here is what I do. I first declare "char yeardata" and then pass "2004data.txt" to it. Then I call yeardata in ifstream to extract the data inside the file "2004data.txt". The problem is that char yeardata is not constant so I cannot pass the file to it. It doesn't work if I change "char yeardata" to "const char yeardata".
Code: int oldnewcomp_temp(char* lcfile) { using namespace std;
I want to use a const char* as a buffer. I am reading values from a file and adding them to a buffer. How to extract the values is simple enough. I am reading through a filestream, reading each character into a char pointer and progressing that char pointer every time. I have another char pointer marking the start positon
eg.
char *mychar = new char; char *char1 = new char; char *char2 = new char; const char *constchar ; char2 = char1; while(filestream.read(mychar,1) { *char1 = *mychar; ++char1; }
Then I get this problem: constchar = mychar; // const char* = char*.
Constchar does not catch all the data in other words. At some stage some data is lost due to zeros in the data.. How can I put values into a const char and get around this problem? The const char* will //only record everything up until the first zero.
I'm currently finishing up an assignment that was half written by my professor. Below in the testGrades section of code there are two errors both are the same message.
Error: no matching function for call to Grades:: Grades(const char [15])
Test Grades
//Purpose: Test program for the class Grades // Create stu1 Grades object // Add 5 grades to stu1 - only 3 can be stored in stu1 - other 2 discarded // Create stu2 Grades object // Add only 2 grades
I have data that is coming into my buffer via popen (process data, not a file). Every seven records is a new set [0-6]. I am trying to 'print out the array line/element value' and 'change the value of element [2] to 0', but my loop appears to be looping through every character and not just every line?
I have data that is coming into my buffer via popen (process data, not a file). Every seven records is a new set [0-6]. I am trying to 'print out the array line/element value' and 'change the value of element [2] to 0', but my loop appears to be looping through every character and not just every line?
199729173 2014-11-16 10:09:34 Found String! 198397652 2014-11-14 15:10:10 Found String! 198397685 2014-11-14 15:10:13 Found String! 198398295 2014-11-14 15:11:14 Found String!
I have a class that defines a window (a popup dialog of sorts), and I want the name of that window to be constant. The only problem is that the name of the popup needs to match the title of the parent window, and I get the name of the parent in the constructor. So how do I go about defining this member variable to be constant and initializing it with a value in the constructor?
I want to do something like this, but I know this isn't allowed:
/* class.h */ class foo { public: foo(*parentWindowPtr);
[Code] .....
I should mention that yes the name of the parent window is const char *, and I would like to keep it this way.
Are there other ways of calling a const/non-const override? I want to defined some functions in terms of others, particularly accessors which might or might not require constness- in order to not copy & paste code. This is my current solution:
difference between const and static const, more effectively. I know the basic concept of const and static but I need clear explanation of declaring "const" and "static const"
Is there any way to cast a non-const variable to const one?
I want to read variable n from file and then use it to declare array "int arr[n]", but because n is non-const, the compiler doesn't allow me to do that.
For my project, my professor is going to test each of our functions. And the functions of type string, he is going to input a value from 1-1 billion(from our calling program) and it should work. However it is not working. I tried putting in a value greater than 9, but it only prints out 1 + x. It should print whatever value I decided to input.
// COverloadingProj.cpp : Defines the entry point for the console application. #include "stdafx.h" #include "OverLoadingHeader.h"
I'm having trouble with passing a character array between functions of the same class. I have a function, buildGraph, that calls function getNextLine. The getNextLine essentially just retrieves the next line of an input file and stores it into a "char line[80]". However when I try to use "line" in my buildGraph function, it has nothing in it.
Here's my code:
Class #define NUMNODES 10 using namespace std; #pragma once class Prog3Graph
I am working on my final project for my class and after finally getting it to compile with no errors to finding examples/tutorials and following skeleton code I cam encountering a problem.
The program runs, asks all the correct questions but when it displays the base pay and total pay for all 3 employees it comes back as ( -1.0743 blah blah )
When they work over 40 hours it works correctly but when they work under 40 hours it displays those weird numbers in those sections.
// Kevin Johnson -- Overtime Pay -- Final Assignment // Created 11/14/2013 // Edited 11/17/2013 #include "stdafx.h"
Why is it not okay to return void? Most compilers will probably let you (gcc does) but it gives you a warning that you aren't supposed to. Most languages allow you to return void.
Something like
Code: void log(const std::string & txt){ std::cout << txt << std::endl; } //C++ way to do it void bar(int i){
I have this int type function that returns a number. It returns the value 2 for now but later it will return more variety of values. How do I use the value it returned? I'm not sure of the proper syntax.
Code: #include <math.h> #include <stdio.h> int main(void) { float a,b,c,root_1,root_2; printf("Please enter value a from the quadratic equation
[Code] ......
And I keep getting this error:
Code: /tmp/ccgtUIun.o: In function `main': assign345.c:(.text+0xc7): undefined reference to `sqrt' assign345.c:(.text+0xef): undefined reference to `sqrt' collect2: ld returned 1 exit status
I've had with visual studio but nothing seems to be working. No matter what I do even with simple programs, like Hello World, I'm always getting an error about a .pch header file.fatal error C1083: Cannot open precompiled header file: 'DebugConsoleApplication1.pch': No such file or directory
This is only for one of the programs I've made but I'm pretty new to programming and I've not even used the header files for anything so I have no clue how to resolve this problem.
I have some struct which contains: void *elems (basically a pointer to an array of contiguous memory).
I want to use bsearch to return a pointer, and then somehow figure out where in the array that value is. Having a pointer in this case isn't enough, I need to know what the index is. I've tried a number of things:
int index; void *value = bsearch(key, start_ptr, cv->count, cv->elemsz, cmp); index = &value - &start_ptr; return index; [ Replacing the second line with:
// in the first instance index = (char*) value - (char*) start_ptr;
// in other instances... index = ((char*) value - start_ptr))/cv->elemsz)
I call a function that returns a string, and I can print it out fine, but I want to test the result of the function to see if it returns 0. But I can't just call the function again (GetNextToken(b)) because it will generate a different token. I can't allocate space for the string because I'm not sure what the size of the returned string is going to be.
Basically I want to see if the GetNextToken(b) returns 0, and if it doesn't then print the string. And running GetNextToken(b) again will give a different result.
Code: int main(int argc, char **argv) { SomeStruct* b = CreateStruct(argv[1],argv[2]); printf("HERE %s", GetNextToken(b));
So, I'm in the midst of implementing my own malloc() and free() functions, but I'm having a hard time with the syntax of getting the address that malloc returns. Whenever I check the address, it's 0 Here's the code:
Code: char *word = malloc(10); int address = *word; printf("%d",address);
The reason I want the address is so that I could store it in a data structure for further usage when I'm dealing with different cases for the free() function. Or is there another way to do this?