C++ :: Strings Always Require Extra Memory Buffer?
Dec 24, 2014
I am trying to add some functionality to this project: [URL] ...
Under this [URL] ...
This latest comment based on;
host.data = (u_char *) "112.168.231.19:80";
for (i = 0; i < peer->name.len; i++) {
peer->name.data[i] = host.data[i];
}
or more simpler:
peer->name.data = host.data;
States: "Because strings always require an extra memory buffer (and the burden of memory management) while primitive values like booleans and numbers don't."
Which makes sense since a boolean change (down/up) does actually changes a runtime value, however the string replacement shows via '/upstreams' that the value has changed but at runtime it is still using the old value.
So in short it looks like I do need a buffer to change the string value.
My question is why? if 'peer->down' works on a memory fragment where its value resides then why doesn't a 'peer->name.data = host.data' do the same thing? can this be done without a buffer? once you allocate memory and store host.data in to this where do I copy it to?
View 5 Replies
ADVERTISEMENT
Jul 8, 2014
When declaring char array[10], memory is allocated for 10 1-bit memory locations. Is extra memory allocated for storing the address of array[0]? In expressions, is array equivalent to a pointer constant or is it an identifier for a memory cell containing the address of array[0]? In other words, is array a variable or an alias for &array[0]?
View 6 Replies
View Related
Jan 20, 2014
I have a C program. Within it, I would like to embed either a file or a pre-assigned variable. I would prefer a method that is platform independent.
I am using the data type "RSA" from <openssl/rsa.h>. I have a key file in PEM format, but I would like to embed an RSA object in the program instead. I tried creating a char array and then casting the pointer, but that caused some sort of illegal casting issue. I tried using memcpy but I haven't been able to get it working that way. What is the best way of going about this? Is it possible to read a file directly from a memory buffer?
View 1 Replies
View Related
Mar 1, 2014
I'm writing a program that communicates with another program over TCP/IP. I need to assemble contiguous buffers of mixed data to send to the server. For example, one of the messages must contain multiple 32-bit integers and 64-bit floats, each in its own appropriate field. I also need to receive and decode similar buffers.
The Windows TCP function "send(socket s,char *buf, int len, flags)" takes a pointer to a character array to send. Likewise, the TCP function "recv(socket s,char *buf,int len,flags)" takes a pointer to a character array to fill. I've tried creating structures describing the send and receive messages, with the fields appropriately laid out. But the CODE::BLOCKS compiler complains when I try to hand send and recv the pointers to the structure variables. Am I on the right track, or is there a better way to do this?
View 2 Replies
View Related
Jan 27, 2014
I have a data buffer project in Windows 7 x64 embedded OS (using Visual Studio 2008), that would work simply like that:
One writer application will send data (no network protocols, just procedure call on the same machine) to my app like 20 packages per second, each data packages will be approximately 3 MB size and comes with a timestamp tag.
My application will store each data item for 100 minutes and then remove. (So I can calculate the total size from beginnig no need for dynamic allocation etc...)
Meanwhile there will be up to 5 reader applications which will query data from my app via Timestamp tag and retreive data (no updates or deletitions on data by reader apps).
So since the data in my buffer app can grow over 50GB I don't think that shared memory is going to work for my case.
I'm thinking about using Boost Memory Mapped Files or Windows API Memory Mapped Files.
So theoratically I will crate a fixed size File on harddisk (lets say 50GB) and then map some portion to RAM write the data to this portion, if the reader applications wants to use the data which is mapped currently on memory, then they will use directly, otherwise they will map some other potion of the file to their address spaces and use from there...
My problem is I haven't use Map File concept at all and I'm not a C++ developer so I'm not very familiar with the language fundementals. I've searched tutorials of boost and msdn for windows as well but there is not too much code example.
So I don't know how to map some portion of the data to memory and manage it, also how to search data in file or memory according to the timestamp tag. Yes there are codes for creating files and mapping the whole file to memory but none for dividing it into regions, aligning or padding the data which I need for my case.
View 2 Replies
View Related
Sep 21, 2013
preventing a buffer overflow when dealing with strings being passed as arguments.
If I have a function prototype such as:
Code:
void foobar(char *bar);
That argument bar - is intended to take a pointer to a buffer of x characters in length. Inside that function, I can't get the size of that buffer, as bar is now just a pointer to a char. I COULD just make the user of this function pass a length parameter, but there is no guarantee that would be correct. Is there a bullet proof way of detecting that the user has provided a buffer that is too small?
View 11 Replies
View Related
May 17, 2013
Saw a thread about a program to add an extra 'b' before 'B' in a file containing 'ABC'. I tried it. Here's my code:
("file1.txt" contains "ABC")
#include<iostream>
#include<fstream>
using namespace std;
int main(){
ifstream in;
ofstream out;
[Code] ...
It works except an extra C appears at the end.
O/P in the file:
"AbBCC"
View 4 Replies
View Related
Nov 15, 2013
My file has these lines.I try to remove extra line in the end, in notepad/gedit, however, after saving it automatically adds up there.
one
two
three
Code:
void help::read_ref(const char* file_name){
std::ifstream file_handle;
file_handle.open(file_name);
std::string line,name;
getline(file_handle,line);
while(!file_handle.eof()){
[code]......
when I print size of map, it comes out to be an extra then what is should be.
std::map<std::String,std::string> ->is my map
on the contrary if I use Code: file_handle >> line; I get correct number of entries in map.
My doubt: getline, automatically finds and in last, it finds a , why it doesn't end the iteration?
I have faced this issue many number of times.
Code: line[0]=='>' I am making this check, then also, I get a null string as key in my map.
I have printed key with their index, using Code: map iterator .
View 7 Replies
View Related
Jun 12, 2013
I pretty much got the assignment done. All it asked for was that you make a C program that displays which manufacturer owned a disk drive based on the code entered. Pretty simple. Just enter 1, 2, 3, or 4 and get the associated manufacturer. Though I am trying to implement an error messege to it for any interger that isn't 1-4.
Code: #include <stdio.h>
int main()
{
[Code]...
o errors are given and it works fine as long as you enter 1-4, but when you enter any other didgit it just stops the program without any messeges.
View 5 Replies
View Related
Mar 2, 2013
While running it gives the runtime error: "Extra parameter in call to factorial."
#include<iostream.h>
#include<conio.h>
int factorial(long int);
void main() {
clrscr();
long int a;
cout<<"This program displays the factorial of the number you enter."<<endl;
[Code] .....
View 15 Replies
View Related
Jul 7, 2013
I got this code from a wiki that taught me how to make C code that plays audio, and with a few changes I was able to get it to take a command-line argument as the music file name. Here it is
Code:
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
int main(int argc, char *argv[])
[Code] ....
The only problem is, even though I guess it needs to create a window to work with SDL, is there any way you could hide it or something so the user can't see it? I don't really want to see it ( I'm planning on running it from Batch files or C applications as an easy way to play an audio file once ), because if I did, I might as well just use a default audio player instead.
View 3 Replies
View Related
Aug 10, 2014
I am new to c++ and have started creating a dummy program to perform a simple task of writing a few strings to the console, which are first generated using random generation. When I execute the code, an extra line for each generated object appears in the console.
int gemNum;
cout << "This program calculates the value and type of gems
";
cout << "Please enter the number of gems: ";
cin >> gemNum;
vector<int> gemVector(gemNum);
vector<int> gemCollection = fillGemCollection(gemNum);
vector<int> gemSort = sortGems(gemCollection);
vector<string> gemResult = finalGemValue(gemNum, gemSort);
for(int i = 0; i<gemResult.size();i++){
string gemString = gemResult[i];
cout << gemString << endl;
}
This program calculates the value and type of gems..Please enter the number of gems: 4
10 hematite
40 carnelian
60 onyx
120 silver pearl
Press "e" and enter to end program
View 1 Replies
View Related
Jul 29, 2013
I have a class and I would like to be able to pass an extra parameter to the function that is executed.
BigInt operator / (BigInt N,BigInt D) {
...
}
is what I have now. but I would like to do something like this. so the default value for a is 10. and if the user does something like N/D (12) Then the value of a is 12.
BigInt operator / (BigInt N,BigInt D, int a=10) {
...
}
View 2 Replies
View Related
Mar 22, 2013
The program works, other than if I place the cursor below the last line in my merch file, the program outputs a line of garbage. The only solution I could find is to leave the cursor on the last line.
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct vRecord {
string venue, item;
float price;
[Code]...
View 2 Replies
View Related
Jan 12, 2014
private void open(object sender, EventArgs e) {
OpenFileDialog openDialog = new OpenFileDialog();
if (openDialog.ShowDialog() == DialogResult.OK) {
string[] lines = File.ReadAllLines(openDialog.FileName);
int k = 0;
while (k < lines.Length)
[Code] ....
So in this code, the user basically opens a text file from the dialog. Then the file gets read on all the lines and gets stored in an array. Now the reason I did this is because there are more fields of text boxes I have to fill in after I fill in the table (6x3). I'm getting an out of bounds error however.
19
19
19
0
95
0
0
0
0
5
0
0
5
1
0
0
51
1
110
Warrior
This is a sample text file that is being used. As you can see all the lines from start up until (excluding "110") will be used in the table. Now I want to read the last lines in this text file and put it in another text box. How do I do that?
View 2 Replies
View Related
Sep 8, 2014
Does uses of pointer in char type cause extra overhead?
View 7 Replies
View Related
Mar 1, 2015
So I'm having trouble with my code. Got it to work, but having issues in terms of indenting my increment. For this example, I will use * as whitespaces.
Expected output:
0
*1
**2
***3
Your output:
0
*1
**2
***3
*****
The expected output has no extra whitespaces, but mine does. What am I doing wrong?
#include <iostream>
using namespace std;
int main() {
int userNum = 0;
int i = 0;
int j = 0;
for(i =0; i <= userNum; i++){
[Code] ....
View 5 Replies
View Related
Oct 18, 2014
This code is printing out "hello worldProgr-¡'*/"
Why is it adding those extra characters? And how can I free "lowerString" while still being able to return the value?
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
char* tokenToLowerCase(char*);
[Code] ....
View 8 Replies
View Related
Feb 9, 2012
Why this code outputs 7 lines (the last line twice) while the file contains 6 lines?
cout << "read msgfile
";
ifstream msgfile ("script1.msg");
while (msgfile.good()) //if not at end of file, continue reading {
// load vector with deffile
msgfile >> line;
vectormsgfile.push_back (line);
cout << line << "
";
}
msgfile.close();
View 2 Replies
View Related
Feb 7, 2014
I've been having this odd problem when I write a file byte by byte, when I write a byte (dec)10 it adds a (dec)13 in front of it, and as far as I can tell, this is the only byte value that does this.
and most annoyingly, I can't read that (dec)13 with fstream, the only reason I know it's adds it, is because I used an external hex editor.
here is the code that causes the problem:
Code:
#include <fstream>
using namespace std;
int main()
{
[Code]...
but the output file reads '0D 0A' (that's in hex, '13 10' in decimal) when viewed in a hex editor. no other number (that I've tried) adds that extra 13.
View 2 Replies
View Related
May 21, 2012
When I read a text file. I'm reading a list of strings in text file, with one string per line. The first line has extra characters in the string, the rest of the lines read are fine, and I can't understand where the extra characters come from. The file format is this...
A
AA
AAN
AAP
AAPL
AAWW
AAXJ
....
...
Code:
std::vector<std::string> strSymbolList;
std::string s = "";
std::ifstream infile( m_strFileSymbols );
while( std::getline(infile, s) ) {
strSymbolList.push_back(s);
}
infile.close();
View 3 Replies
View Related
Aug 18, 2013
I'm working on a cross-platform threading project, the aim is to start 1 worker-thread that has a queue of worker-items which are serviced by the thread.
The threading framework works fine, both under Windows and LINUX, albeit as designed under Windows, and in a modified form under LINUX.
I have this CSnmpTrapd class which is designed as a worker-item for the worker-thread, it listens for incoming SNMP traps and decodes the packets, this is implemented in csnmptrapd.cpp and csnmptrapd.hpp.
1) When I add this piece of code to my makefile and re-compile the app successful... the app will start (or not?) but generates no console or file logging as is expected, it will not even show printf() output which was added as first statement in main(). My app handles CTRL-C to terminate and this works !?!? But I'm actually not sure my app works at that point.
2) When I compile the app without csnmptrapd.cpp and start it, it will generate console and logging output and behave as expected.
3) When I add the CSnmpTrapd class implementation to an existing cpp file in the project, it will compile and run as expected... the worker-thread handles the CSnmpTrapd worker-item as it should, incoming SNMP traps are captured and decoded, logging generated
View 6 Replies
View Related
Feb 3, 2013
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void count();
void main() {
char c[100];
cout<<"Enter a string upto 99 characters(spaces included):"<<endl;
[Code] ....
The thing is, when I don't create a prototype of count and straightaway define it before void main() my program runs, but it gives error in this program that "extra parameter in call to count()" But what to do then?
View 2 Replies
View Related
Jun 11, 2013
I have a class matrixType that has some overloaded operators (+, -, *, and <<). With a view to having clearly-delineated, perfectly-formatted, four-sided matrices, as shown below:
A = 1 2 3
4 5 6
7 8 9
or
A + B = 1 2 3
4 5 6
7 8 9
and NOT this jagged ones shown below:
A = 1 2 3
4 5 6
7 8 9
or
A + B = 1 2 3
4 5 6
7 8 9
,
I want a scheme in which the string literals (A, A+B, etc.) could be passed as parameters to the overloaded stream insertion (<<) operator function so that I could use the string’s length to determine how much offset from the display screen’s left to apply to each matrix’s row (by using the setw() function). However, I do know that the << operator is a binary operator, meaning the function cannot take more than two parameters: that is what compounds my problem!
View 10 Replies
View Related
Apr 7, 2013
I am programming a translator, and I have it so that it detects words with spaces both in front of and behind them, so I did "string.append(space);" where space equals " ". That added a space to the end, but I still need a space added to the front.
View 1 Replies
View Related
Feb 12, 2014
I have a problem who must print the sentences who have lenght more than 20 characters. I dont know why, but it prints just the first words. Look what i made.
#include<stdio.h>
#include<conio.h>
int main()
[Code]....
For instance :
Give the number of sentences : 3
First sentence : I like the website bytes.com
Second sentence : I like more the website bytes.com
Third sentence : bytes.com
After I compile the program it should print the first two sentences.
View 2 Replies
View Related