Operating a global buffer in C

I have a simple program with a couple of functions. I need the program to return a buffer that is being built in the functions and in the main function. I decided to do so with a global buffer. Supposing I know the maximum size of the final string, what is the best way to do so - creating the buffer, appending the strings and returning it? the program looks somewhat like this:

// Declaring the global buffer int funcB() { // Append string to buffer } int funcA() { // Append string to buffer from funcA return } main() { // Append string to buffer from main funcA() // Append string to buffer from main funcB() // Append string to buffer from main // Return the buffer }

each comment is not a comment on the code, it's the action that the program need to do.

Thank you!

-------------Problems Reply------------

int funcA(char *add, char *string)
return 1;

int main(){
//Buffer available to set
char buffer[] = "Hello";
char txt[] = "Fires";

//Call function to change buffer at its address


return 0;

As someone mentioned it would be a idea to use parameters and declare global buffer in main. You also have to worry about how big your strings are that you are appending them. Cause you could possibly write over the next value.

You also have the option to declare variables in .h file and include that file in your main. http://stackoverflow.com/a/3010672/2980929

Category:c# Views:1 Time:2019-03-13
Tags: winapi

Related post

  • Global buffer of 4096 defined in kernel module causing performance issues while doing a get from a storage device 2012-04-28

    We had a kernel module which writes into a file and we read the proc file to get some information regarding hardware operation. The hardware operation statistics data became more than 3072 bytes (PROC_BLOCK_SIZE) ; so we locally buffer the data by de

  • Are atomic operations on global memory in CUDA performed in parallel across a warp? 2010-06-30

    I need to do an atomic FP add operation on global memory on a CC 2.0 device. If the global data referenced in a warp fit into an aligned 128-byte sector, will these operations be done in parallel or will they be executed one at a time? My guess would

  • Using SFINAE to check for global operator 2011-04-24

    I want to have several overloaded, global to_string() functions that take some type T and convert it to its string representation. For the general case, I want to be able to write: template<typename T,class OutputStringType> inline typename ena

  • Global typecast operator overload? 2011-12-21

    I'm writing some 'portable' code (meaning that it targets 32- and 64-bit MSVC2k10 and GCC on Linux) in which I have, more or less: typedef unsigned char uint8; C-strings are always uint8; this is for string-processing reasons. Legacy code needs char

  • an operation could not be completed because a socket was full or there was not enough buffer 2015-01-06

    I seem to be having major issues with connections I have Vista Premium on my machine and about every 6 hours i cannot connect to the internet and have to reboot I use Bittorrent and can see the message below on the status an operation could not be co

  • Is there a way that I can force mod_perl to re-use buffer memory? 2010-03-16

    I have a Perl script running in mod_perl that needs to write a large amount of data to the client, possibly over a long period. The behavior that I observe is that once I print and flush something, the buffer memory is not reclaimed even though I rfl

  • How to safely flush a buffer from a different thread, without synchronized methods? 2011-02-12

    There are multiple threads, say B, C and D, each writing small packets of data to a buffer at a high frequency. They own their buffer and nobody else ever writes to it. Writing must be as fast as possible, and I've determined that using synchronized

  • Why can't I access my array subscript in global scope 2012-04-18

    If I have the following code in a function, I will not get an error and I can compile no problem, however, once I put it in global scope I will get an error for "cannot allocate an array of size zero", along with several other errors. Why does this h

  • Meaning of :: Operator in C# and PhP 2008-12-30

    In my work I have come across syntax like global:: (C#) or G:Loading($value) in Php, what do they mean? --------------Solutions------------- In C# :: is the namespace alias qualifier operator, and global indicates the global namespace. It is necessar

  • What is a buffer overflow and how do I cause one? 2009-02-22

    I have heard about a buffer overflow and I would like to know how to cause one. Can someone show me a small buffer overflow example? New(And what they are used for?) --------------Solutions------------- A buffer overflow is basically when a crafted s

  • Saving stack by directly writing into the send buffer below 2010-07-21

    Imaging having a stack of protocols and some c/cpp code that neatly covers sending on each layer. Each send function uses the layer below to add another header until the whole message is eventually placed into a continuous global buffer on layer 0: v

  • "backup and restore operations for static class attributes" in PHP 2010-11-24

    I saw this phrase in the PHPUnit Documentation: The implementation of the backup and restore operations for static attributes of classes requires PHP 5.3 (or greater). The implementation of the backup and restore operations for global variables and s

  • Why do DBS not adapt/tune their buffer sizes automatically? 2010-12-10

    Not sure whether there isn't a DBS that does and whether this is indeed a useful feature, but: There are a lot of suggestions on how to speed up DB operations by tuning buffer sizes. One example is importing Open Street Map data (the planet file) int

  • impose restrictions on buffer size 2011-01-17

    Is there any way I can impose a size constraint on a character buffer such that no kind of operation on the buffer would lead to an out-of-bounds access? --------------Solutions------------- In short: No C does not perform any runtime checks, unless

  • How to define a global variable template in C++? 2011-02-05

    Suppose you have to write a template library that operates on a client-supplied type. Also, suppose that this library really needs to access a global variable parameterized by the client-supplied type. How would you go about implementing this pattern

  • User's new and SDK's delete operator 2011-04-27

    guys, If I override the global new and delete in my dll code, will this override the user code new and delete? I made a test, and it shows the user new and delete will not be impacted. If I want to make user code new and delete be replaced by mine sd

  • Thread-safe string buffer variables in C++ DLL 2011-06-22

    I am writing a Win32 DLL in MSVC++2010 with exported functions. Some of those functions return filenames as LPCSTR. Because I sometimes need to fiddle with strings before, I am currently using a global buffer variable of length 32184 which should cov

  • What default implementation of new/delete from the std-rtl should I use when overriding new/delete in global namespace? 2011-07-15

    I'm using the RTL as a dynamic library. This allows for an overridden new/delete operator in global namespace, because the linker would find my implementation first. I could rely on malloc() and free() to do the allocation stuff, but there are things

  • How to perform bitwise operations on files in linux? 2011-07-31

    I wanna do some bitwise operations (for example xor two files) on files in Linux , and I have no idea how I can do that. Is there any command for that or not? any help will be appreciated. --------------Solutions------------- You can map the file wit

  • C++: Operator, (comma) doesn't seem to work 2011-12-01

    I'm writing my class now and testing it. It seems that the comma operator (operator,) refuses to work, the program simply skips it. This is the code I'm running fileObj << buffer,40; I wrote the following operators (only prototypes shown, the c

  • No match operator for 'operator *' in Matrix 2011-12-21

    I have a matrix class like below: template <size_t M, size_t N, typename T> class Matrix { public: Matrix<M, N, T> operator +(const Matrix<M, N, T>& B) const; template <size_t P> Matrix<M,P,T> operator*(const Matrix

  • ring buffer with numpy/ctypes 2012-01-18

    I'm developing a client which will receive the [EEG] data over tcp and write it to the ring buffer. I thought it can be very convenient to have the buffer as a ctypes or numpy array because it's possible to create a numpy 'view' to any location of su

  • Best way to write series of images to buffer memory 2012-01-24

    Is there a way of using imencode and imdecode to write and read series of images generated in the program into a global buffer? I'm using cv::Mat to read my images. I don't want to use vector<Mat> and arrays of IplImage* since I'm having some i

  • Copying buffer contents of different data types 2012-03-22

    I am writing a code for modbus protocol, that runs on a MSP430 controller. The response buffer(global) is an array of 8bit data, through which the response to the processed request is to be sent on serial UART. Now, my problem is that the response ge

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.208 (s). 11 q(s)