C/C++ :: Accessing Array At A Certain Memory Location Results In Seg Fault
Aug 4, 2014
I have a contiguous sequence of section headers in a file (all the data in the file is stored in void * data), where each section header is the same size and has the same fields in the same order. So it's laid out like an array.
I have to:
- Use an offset value and the number of section headers variables to identify the location and length of the section header table (these are hdr.offset and hdr.length respectively).
- The offset value shows the distance between the start of the file and the start of the first section header (so I need a pointer to the start of the first section header)
- Apply a typecast to location of the section header table to process it as an array of section headers.
Here is what I have done, but I am getting a segmentation fault. What am I doing wrong? How do I fix this?
typedef struct {
unsigned int name;
unsigned int type;
} SectionHeader;
[Code] ....
View 1 Replies
ADVERTISEMENT
Aug 27, 2014
Write a program that reads in the names and the ages of ten people. Store these data in two arrays (make sure that the entered names are not longer than the array size you choose). Then produce a table of ten lines, with each line giving the name and age of a person along with the (positive or negative) deviation of that person's age from the average age. The code I wrote to fill in the arrays looks as follows
Code:
#include<stdlib.h>
#include<stdio.h>
int main()
{ // begin main()
// array length
[Code]....
When I compile it, it gives me warnings that few of the variables I declared aren't used, but that is not affecting the program at all. When I run the program, it allows me to pass the names and ages into the arrays, but when it comes to displaying them, I'm getting a 'Segmentation fault'. I used very similar code in Java to write the program, and it worked fine so what is the problem with C then?
View 8 Replies
View Related
Oct 21, 2014
I have these two functions listed below. DoEvent() gets called from a window procedure whenever some event occurs. SetEvent() sets an element of the m_Events array a pointer to a Command object to handle the event. The array is zeroed in the constructor.
The error occurs on line 16 in the code in DoEvent(). Why the array is suddenly unreadable. When I call SetEvent() it works just fine, no problem.
When I step through the debugger in SetEvent i see that each element begins as 0 and the element I set is set properly. However, when I step through DoEvent() all elements in m_Events show "?????" "Unable to read memory".
uw::Command* uw::WindowBase::SetEvent(uw::WindowEventsEnum event, uw::Command* command) {
if (event < 0 || event >= FINAL_EVENT_ENTRY)
return NULL;
Command* ret = m_Events[event];
m_Events[event] = command;
return ret;
[Code] ....
View 2 Replies
View Related
Oct 28, 2014
Below is my sample code of a C program. I am doing lot of processing, but my issue is I am getting a segmentation fault error in my main when I am putting the file names at the command line and using them. The program works great in Windows, but its giving error in Unix.
Below is the code of my Main function.
Code:
int main(int argc, char *argv[])
{
FILE *user, *requests;
char c;
user = fopen(argv[1], "r");
requests = fopen(argv[2], "r");
//For users
struct userStorage *USHead = NULL;
//struct users *userHead = NULL;
[Code]...
View 6 Replies
View Related
Jul 12, 2013
In the code below. I believe I am returning the memory location of the value I am looking for. How can get the value?
main.cpp
int choice = 0;
PlayerMenu *newPM = new PlayerMenu;
File *file = new File;
// Menu for loading, creating or exiting the game
choice = newPM->menuChoices();
PlayerMenu.cpp
[Code] ....
I am not sure how to deference the value so I can get at the value instead of the memory location.
View 5 Replies
View Related
Oct 23, 2014
I just started deal with pointers in C, and one stuff confusing me...
Here is simple code:
int main (){
int var1 = 10;
int var2 = 20;
long ms = 10000000;
printf("Address of var1 variable: %p
", &var1 );
[Code] ...
But - when I run pmap - I don't see this addresses there:
Code:
# pmap -x 16496
16496: ./address
Address Kbytes RSS Dirty Mode Mapping
0000000000400000 4 4 0 r-x-- address
0000000000600000 4 4 4 rw--- address
00007f79cf8b8000 1576 260 0 r-x-- libc-2.12.so
00007f79cfa42000 2048 0 0 ----- libc-2.12.so
00007f79cfc42000 16 16 16 r---- libc-2.12.so
I guess - it's is inside stack:
Code:
# cat /proc/16496/maps | grep stack
7fff0d7e5000-7fff0d7fa000 rw-p 00000000 00:00 0 [stack]
But - how I can obtain this variables addreses, to see them with `pmap`? If I put them as "global":
Code: ...
#include <stdlib.h>
#include <sys/types.h>
int var1 = 10;
int var2 = 20;
int millisleep(unsigned ms);
int retpid(void);
[Code] ......
I have odd addresses in result:
Code:
Address of var1 variable: 0x600a94
Address of var2 variable: 0x600a98
View 1 Replies
View Related
Jan 11, 2013
Under Microsoft, I use GlobalAlloc and GlobalFree to dynamically allocate and free memory. Intermittently GlobalFree is reporting error 998 (GetLastError() - Invalid access to memory location). I've checked my logs of addresses returned by GlobalAlloc against those being freed by GlobalFree and when the problem occurs the address causing the error has been allocated by GlobalAlloc and has not been freed before since allocation.
I can't reproduce it. Currently I'm checking for error 998 from GlobalFree and ignoring it if it occurs - which is not satisfactory. I have one function (AllocMem) that calls GlobalAlloc and checks for errors and another function (FreeMem) which calls GlobalFree and checks for errors.
My program uses AllocMem and FreeMem everywhere memory is required or freed. AllocMem and FreeMem are in a DLL. The program is heavily threaded and a different thread may call AllocMem to the one that calls FreeMem. I don't believe I've got a race condition etc as all the code in the program containing the AllocMem and FreeMem calls are part of a CriticalSection using the same variable.
What is a possible cause of this error?
View 14 Replies
View Related
Jun 26, 2014
If I have a pointer variable indicating memory location in which we have stored what user entered and the pointer is of type volatile if the user gives the character 'a' twice , then this character will not be fetched twice from the memory but only when the character is changed???
This is the one meaning of the volatile? the other is that the value will be changed without the program itself change it?
View 4 Replies
View Related
Mar 24, 2014
It is been several days that I am stucked with the segmentation error and I do not know anymore what to do I try to do a loop over my function but it is working only for my first iteration. Here the code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
[Code].....
I would like to iterate over solvep (since h is changing at each iteration..I usually add other fct but now I try to debug why the loop is not working.).The first iteration is working fine but not the other iterations.
View 4 Replies
View Related
Apr 6, 2013
5. Four experiments are performed and each experiment produces four test results. The results are tabulated as shown below. Write a program to store these results in a two-dimensional array, A[4][5], then use a nested for-loop to compute the average of the test results for each experiment and store it in the 5th column of the array. Print the array.
1st experiment results: | 23.2 | 34.8 | 53.7 | 19.5 |
2nd experiment results: | 34.8 | 42.9 | 28.6 | 35.4 |
3rd experiment results: | 24.5 | 29.5 | 32.0 | 19.3 |
4th experiment results: | 36.8 | 31.6 | 43.7 | 29.5 |
6. Using the srand( ) and rand ( ) C++ library functions, fill an array of 1000 numbers with random numbers that have been scaled to the range of 1 to 100 inclusive. Then determine and display the number of random numbers having bvalues between 1 and 50 and the number having values greater than 50. What do you expect the output counts to be?
View 2 Replies
View Related
Nov 8, 2013
All my code is good, except the functions: findMacLoc and findMinLoc .They don't give me the correct array location of the max/min number in the array.I know is something simple I need to tweak in those two functions.
#include <iostream>
#include <time.h>
using namespace std;
void printArray(int a [], int & size)
{
int i;
for (i = 0; i < size; i++)
cout << a[i] << " ";
[code].....
View 2 Replies
View Related
Jul 26, 2014
I want to access the elements of my array dynamically. So far I've only figured out how to do this manually. if I tried it like this my code would work but there should be a better way right?
View 10 Replies
View Related
Jan 16, 2015
I am trying to retrieve the elements in an array to print them. I am pretty sure I have everything right, but it is not working. There are 5 values in the array {3, 4, 10, 5, 6}, but the first two elements (supposed to be 3,4) are coming out a 0,-2654238590. The last three elements are coming out correct.
void addDynArr(DynArr *v, TYPE val) {
/* FIXME: You will write this function */
assert(v != 0);
//resize if necessary
if(v->size >= v->capacity)
[Code] ....
View 6 Replies
View Related
Dec 9, 2014
I've been working for some number of days on this code to take information about movies from both a file and the user, store the infos in an array of structs, and at the end, write all the info out to a file. I'm having some problems with an error message reading:
"prog.c:102:11: error: subscripted value is neither array nor pointer nor vector"
this error occurs in many lines (which I will label specifically below -- they are everywhere where I am trying to access/modify an individual element of a struct element of the array).
A few examples of where I am having the problems are lines:
39, 52-55, 70, 72, and 86 (and more of the same exact variety).
I am obviously rather systematically doing something wrong, but I am quite certain all of these are the exact same mistakes.
I pull up also 2 or 3 other errors, but I don't think they are related and should be able to fix them quickly once I work out this conundrum.
#include <string.h>
#include<stdlib.h>
#include <stdio.h>
int moviecount =0;
typedef struct{
int year;
[Code] .....
View 5 Replies
View Related
Nov 22, 2012
I was wondering if it was possible to access data from an array inside a threaded function after it has finished executing?
Take this example:
Code:
#include <iostream>
#include <boost/thread.hpp>
using namespace std;
[Code]....
After workerFunc has finished, how could I access the array arr? Is there any way to get (3,4,5,6,7) back into main()?
View 5 Replies
View Related
Mar 31, 2013
Code:
int arr[10];
int* p = arr;
int (*p2)[10] = &arr;
So, pointer p is a pointer to an array, I can use it to access elements of arr as in *(p+5).
Pointer p2 is a pointer to an array of ten integers. What is it for, how can I use it to access elements of arr?
View 4 Replies
View Related
Mar 7, 2013
I am writing a C program to access a string into an array from a pointer array in Visual Studio 2010. Program is given below:-
Code:
#include <stdio.h>
#include <string.h>
void main() {
char data_a[6],data_b[6]="ABcde",*ptr;
ptr=&data_b[0];
data_a[0]=*ptr;
printf("%s",a);
}
I am getting some junk character first and then my actual data on console window. I want to where I am getting wrong and how to resolve it.
View 4 Replies
View Related
Feb 8, 2014
I'm okay with 2-dimensional arrays, but when I get to 3 or more, I can't seem to wrap my head around how to assign and/or pull values from specific parts.
To give an example, let's take the following example:
We know that a player can take up to 5 total quests, and each quest can have a max of 5 tasks.
Let's assume I have the following multi-dimensional array holding all of a players quest data:
quests[0] = 78;// Store the questID
quests[0][0] = 3945;// Store the 1st creature ID
quests[0][1] = 2230;// Store the 2nd creature ID
quests[0][2] = 3045;// Store the 3rd creature ID
quests[0][0][0] = 2;// Store how many needed of the 1st creature
quests[0][1][0] = 5;// Store how many needed of the 2nd creature
quests[0][2][0] = 13;// Store how many needed of the 3rd creature
As we know, the above code can't be done. How do I assign certain values to each specific dimension?
View 6 Replies
View Related
Jun 4, 2013
i want to enter a URL in as a string, and extract just the URL part.
eg: Code: input: URL....
output: URL....
Input: URL....
output: URL....
part of our header file which we cannot edit (the url is always less than 140 characters):
Code:
char url[140];
function i am having problems with
Code: void init_url(char new_url[])
{
/*
This function sets the variable url to new_url and url_size to the number of characters in the url
It also initializes url_error_flag
}
[code]....
whenever i try a URL like the second one above with no '?' i'm getting a segmentation fault error which is becase i'm trying to access an invalid index. i'm not sure what an array is initialized to when i do not explicitly assign it anything, for example
Code:
char values[140] = {0};
i've read it depends on whether it is a global vs local array or something? i know this could all be fixed with simply assigning the array values to 0 but i'm not allowed to edit the header file.
View 11 Replies
View Related
Apr 20, 2013
This is a homework assignment where I have to read a file into a dynamically allocated 2d array. The file format is
10
Jim 3.6
Jake 4.0
Will 3.0
Sara 3.4
Mike 2.5
Ellen 2.9
Melissa 3.9
Eric 3.8
John 3.5
Beth 3.9
where 10 is the number of students followed by the students and the gpa's. There is more to the program but I have not implemented it yet because I am getting a segmentation fault. The output I am getting when I print the array is
Jim 3.6
Jake 4.0
Will 3.0
Sara 3.4
Segmentation fault
I can see where the problem lies. If I raise value for row when I am allocating the rows of the array, all of the names print. I just do not see why I need to. From my understanding the row * sizeof(char*) should give me enough room for 10 entrie.
Code:
#include <stdio.h>
#include <stdlib.h>
void sort();
int main()
[Code] .....
View 6 Replies
View Related
Jun 19, 2014
I am having trouble reading in a file of 10 names into an array. Ive already allocated the memory, I just keep getting a seg fault when I try and read in the names.
Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_STRING_LEN 25
void insert_data(char **strings, const char *filename, int size);
void allocate(char ***strings, int size);
[Code] ....
And then the list that I have to read in is as follows:
matt
susan
mark
david
aden
phil
erik
john
caden
mycah
View 1 Replies
View Related
Jan 25, 2013
This function should replace all instances of a character in a given character array, while returning the amount of characters changed, but I keep getting a segmentation fault at the highlighted area.
I'm only supposed to use pointers so arrays are out of the question, and I don't think we are allowed to use the string.h library as well. How I could avoid something the segmentation fault or ways to fix it?
Code:
int replaceChars(char replace, char find, char *input) { int i, j;
//Finds length
for(i = 0; *(input + (i + 1)) != '