Error while trying to read n images using a loop? [closed] - c

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
void main() {
int frame_number = 0;
do {
char *filename = "";
strcpy(filename, "frame_");
char *frame_id = "";
itoa(frame_number, frame_id, 10);
strcat(filename, frame_id);
strcat(filename, ".bmp");
FILE* f = fopen(filename, "rb");
if (!f) {
printf("Could not read!");
system("Pause");
}
else
printf("Read!");
fclose(f);
frame_number++;
} while (frame_number < 20);
}
Whenever I run this I get the error of access violation writing location !!!
There are around 40 images in the folder.
How to solve this ?

No memory has been allocated for filename and frame_id. Allocate memory before storing strings using malloc or calloc. Or just declare them as static arrays.
char filename[256]="";
char frame_id[256] = "";

Related

Word Extraction C [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I want to extract the words from a file (and later, from console input), count their appearances and store them in my Word structure:
typedef struct cell{
char *info; /* word itself */
int nr; /* number of appearances of the word *
}*Word;
This structure will be allocated dynamically for as many words are contained in the file. Consider this function:
void Word_Allocation (Word* a) /* The function that allocates space for one structure */
My questions are:
How do I correctly open a file and read it line by line?
How do I correctly store words and number of appearances in my structure?
As for file io, this is the basics.
As for the algorithm, since you are not using C++, so map is not available which is trivial for this problem. A straightforward solution in C might be:
Allocated an array of cell and read in words
sort the array on char *info.
count
Your allocator function should return a Word* and receive a size to allocate for the word itself. Something like this, perhaps:
Word * Word_Allocation (size_t size) {
Word *w = malloc(sizeof(*w));
if (w) w->info = malloc(size);
if (!w->info)
{
free(w);
w = NULL;
}
return w;
}
You can read a word at a time with:
#define STR(x) #x
enum {MAX_BUF = 100};
char buf[MAX_BUF];
fscanf(infile, "%" STR(MAX_BUF) "s", buf);
And then strlen(buf)+1 is the size to pass to Word_Allocation. Or you can pass buf and have Word_Allocation call strlen and copy the data over.

Trying to access menu names from array using structures ...getting error [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have a menu and sub-menu. I have created a structure where I have store menu and submenu names in an array. Trying to print them, but I think I am wrong somewhere.
#include <stdio.h>
#include <stdlib.h>
struct calculator
{
int x_loc;
int y_loc;
char main_menu[15];
char sub_menu[15];
char icon_title[100];
};
char *main_menu_names[3] = {"Link_1", "Link_2", "Link_3"};
char *sub_menu_names[3] = {"Sub_Link_1", "Sub_Link_2", "Sub_Link_3"};
struct calculator info = {219, 120, main_menu_names, sub_menu_names, "Title"};
int main()
{
printf("X location: %d\n",info.x_loc);
printf("Y location: %d\n",info.y_loc);
printf("Menu Name: %s\n",info.main_menu);
printf("sub menu: %s\n",info.sub_menu);
printf("icon_title: %s\n",info.icon_title);
system("PAUSE");
return 0;
}
Your code doesn't make any sense.
You can't initialize struct calculator's char main_menu[15] field with menu_names, which is undefined. I don't believe you could compile the above.
DEFINITION IS SINGLE ARRAY
char main_menu[15];
char sub_menu[15];
But u have equated to two dimensional array
char *main_menu_names[3] = {"Link_1", "Link_2", "Link_3"};
char *sub_menu_names[3] = {"Sub_Link_1", "Sub_Link_2", "Sub_Link_3"};
in
struct calculator info = {219, 120, menu_names, sub_menu_names, "Title"};
It should be like
struct calculator info = {219, 120, menu_names[0][0], sub_menu_names[0][0], "Title"};

Dynamically reallocating an array of structs in C [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
A part of my code will read in an unknown number of lines from a text file, parse that line into a structure (tempSomeStruct), resize the SomeStruct_Array, and then add that tempSomeStruct into the newly opened spot in memory.
However after a few times through the while loop, my program stops and says
myApplication.exe has triggered a breakpoint.
I did not set a breakpoint, and doing some digging, it LOOKS like the breakpoint is due to heap corruption from my call to realloc. I am pretty new to dynamic allocation, so while I have searched and found a few possible causes, so far no fixes have worked.
How am I corrupting the heap in this situation, and what do I do differently to avoid doing so?
I have a function like this:
int growArray(SomeStruct **SomeStruct_Array,int currentSize, int numNewElements)
{
const int totalSize = currentSize + numNewElements;
SomeStruct *temp = (SomeStruct*)realloc(*SomeStruct_Array,(totalSize * sizeof(SomeStruct)));
if (temp == NULL)
{
printf("Cannot allocate more memory.\n");
return 0;
}
else
{
*SomeStruct_Array = temp;
}
return totalSize;
}
and it is called in elsewhere like this:
SomeStruct* SomeStruct_Array = (SomeStruct *) calloc(1,sizeof(SomeStruct));
int Error_Array_Size = 0;
if(SomeStruct_Array == NULL)
{
printf("Cannot allocate initial memory for data\n");
return;
}
while(fgets(line,sizeof(line), file) != NULL)
{
parseTextIntoSomeStruct(line, &tempSomeStruct);
SomeStruct_Array_Size = growArray(&SomeStruct_Array,SomeStruct_Array_Size,1);
if(SomeStruct_Array_Size > 0)
{
SomeStruct_Array[SomeStruct_Array_Size] = tempSomeStruct;
}
}
Your new array's size is SomeStruct_Array_Size, and you immediately write to SomeStruct_Array[SomeStruct_Array_Size] which is one past the end of the array! Remember, C arrays are zero-indexed.
Use
SomeStruct_Array[SomeStruct_Array_Size-1] = tempSomeStruct;
instead.

Array of files and value assigning [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have 2 questions.
I want to create an array of files in C. But I'm not sure whether I have to malloc the size before or not.Can I just use FILE** files as an array or do I have to malloc them before. And if I have to make space, do I need to reserve 4 bytes (x86)?
I have the variable "char extra[8] = { 0xAE00AF00B000B100 };" and I want to assign it to the end of another char array[24]. Is there a faster way of doing that without having to type in every value by hand or using a for loop.
char extra[8] = { 0xAE00AF00B000B100 };
// index is a random place in the string
name[index] = '\0';
i = 0;
if (index > 16) {
for (i = 24-index; i < 8; i++) {
index++;
name[index] = extra[i];
}
}
else {
name[17] = 0xAE;
name[18] = 0x00;
name[19] = 0xAF;
name[20] = 0x00;
name[21] = 0xB0;
name[22] = 0x00;
name[23] = 0xB1;
name[24] = 0x00;
}
I need to add those extra bytes btw.
I want to create an array of files in C. But I'm not sure whether I
have to malloc the size before or not.Can I just use FILE** files as
an array or do I have to malloc them before. And if I have to make
space, do I need to reserve 4 bytes (x86)?
If you need to have an array of files, it is possible to use an array of pointers as follow:
#include <stdio.h>
FILE *array[NB_FILES];
Or you can do it dynamically if NB_FILES is only known at runtime.
#include <stdio.h>
#include <stdlib.h>
FILE **array = malloc(nb_files * sizeof *array);
I have the variable "char extra[8] = { 0xAE00AF00B000B100 };" and I want to assign it to the end of another char array[24]. Is there a faster way of doing that without having to type in every value by hand or using a for loop.
The standard C library provides the function memcpy, which is a builtin on many compiler (so it will be faster than a for loop).
#include <string.h>
char array[24];
char extra[8];
memcpy(array + sizeof array - sizeof extra - 1, extra, sizeof extra);

What var type you use on growing buffers [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I need to read some bytes from a socket stream.
No i do a expanding buffer like this:
long curbufsize = 1024*24;
long expand = 1024*24;
void *buf = xmalloc(curbufsize);
void *buf_ptr_start = buf;
char mem[1024*24];
while (rc = my_read_from_stream(handle, mem, sizeof(men)) {
len = (int)buf-((int)buf_ptr_start+rc);
if(curbufsize < len) {
curbufsize+=expand;
xrealloc(buf_ptr_start, curbufsize);
}
memcpy(buf, mem, rc);
}
where should i use size_t and long/int? Should the buffersize be a size_t?
Should i better write for the new len calculation:
len = (size_t)buf-((size_t)buf_ptr_start+rc);
Any other optimization?
Thanks
Using int this way is incorrect since int may be smaller than the pointer size of your system and will thus lead to truncation. I'd use size_t to keep track of your current buffer size and there's no need for any pointer arithmetic.
The reallocation is also completely broken. Why are you calling xrealloc() and then ignoring the return value. That's like a leaking version of free()!
You could write it something like this:
size_t len = 0;
size_t size = 0;
size_t expand = 1024*24;
char *buf = NULL;
char *newbuf;
char mem[1024*24];
while (rc = my_read_from_stream(handle, mem, sizeof(men)) {
if (size < len+rc) {
while (size < len+rc)
{
size += expand;
}
newbuf = xrealloc(buf, size);
if (!newbuf)
{
free(buf);
return ERROR_MEMORY_ALLOCATION_FAILED;
}
buf = newbuf;
}
memcpy(buf+len, mem, rc);
len += rc;
}
You can look in the header file that declares xmalloc; the type of its parameter must be right there. Though by the look of your code you are not trying to write a portable application, so you probably don't need to worry about such choices.
Just fix the bugs.
Edit: seeing that you talk about optimization, take into account that int might be faster than size_t (most probably it isn't) - profile the two variants and choose the fastest (or acknowledge that they have the same efficiency).

Resources