Skip to content

Instantly share code, notes, and snippets.

@atamrawi
Created September 25, 2020 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atamrawi/e5a8170e96f4a2dc71598c80d6be9344 to your computer and use it in GitHub Desktop.
Save atamrawi/e5a8170e96f4a2dc71598c80d6be9344 to your computer and use it in GitHub Desktop.
Assignment 2 - Problem 1 Source Code
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
// maximum number of integers stored in buffer
const int MAX_BUF_SZ = 1024;
int srcBuffer[MAX_BUF_SZ]; // source buffer
int dstBuffer[MAX_BUF_SZ]; // destination buffer
// randomly fill buffer
void fillBuffer_srcBuffer() {
for(int i = 0; i < MAX_BUF_SZ; ++i) {
srcBuffer[i] = rand() * 100;
}
}
int main(int argc, char** argv) {
if(argc < 2) {
printf("Usage - ./bufferOverflow [Number of Integers to Copy]\n");
return -1;
}
assert(sizeof(int)==4); // make sure we are working on machine with (sizeof(int) = 4)
fillBuffer_srcBuffer();
int copySize = atoi(argv[1]);
if(copySize > MAX_BUF_SZ) {
printf("You cannot copy more than (%d) integers from our buffer\n", MAX_BUF_SZ);
return -1;
}
printf("About to copy (%u) integers from our buffer\n", copySize);
memcpy(dstBuffer, srcBuffer, copySize * sizeof(int));
return 0;
}
@atamrawi
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment